diff options
Diffstat (limited to 'Doc')
-rw-r--r-- | Doc/c-api/method.rst | 6 | ||||
-rw-r--r-- | Doc/c-api/set.rst | 4 | ||||
-rw-r--r-- | Doc/c-api/tuple.rst | 4 | ||||
-rw-r--r-- | Doc/c-api/type.rst | 2 | ||||
-rw-r--r-- | Doc/c-api/unicode.rst | 5 | ||||
-rw-r--r-- | Doc/library/ctypes.rst | 1 | ||||
-rw-r--r-- | Doc/library/decimal.rst | 63 | ||||
-rw-r--r-- | Doc/whatsnew/2.6.rst | 2 |
8 files changed, 62 insertions, 25 deletions
diff --git a/Doc/c-api/method.rst b/Doc/c-api/method.rst index 9d25571..9ee49ba 100644 --- a/Doc/c-api/method.rst +++ b/Doc/c-api/method.rst @@ -92,3 +92,9 @@ no longer available. .. cfunction:: PyObject* PyMethod_GET_SELF(PyObject *meth) Macro version of :cfunc:`PyMethod_Self` which avoids error checking. + + +.. cfunction:: int PyMethod_ClearFreeList(void) + + Clear the free list. Return the total number of freed items. + diff --git a/Doc/c-api/set.rst b/Doc/c-api/set.rst index 14d8eab..1560717 100644 --- a/Doc/c-api/set.rst +++ b/Doc/c-api/set.rst @@ -54,15 +54,11 @@ the constructor functions work with any iterable Python object. Return true if *p* is a :class:`set` object or an instance of a subtype. - .. versionadded:: 2.6 - .. cfunction:: int PyFrozenSet_Check(PyObject *p) Return true if *p* is a :class:`frozenset` object or an instance of a subtype. - .. versionadded:: 2.6 - .. cfunction:: int PyAnySet_Check(PyObject *p) Return true if *p* is a :class:`set` object, a :class:`frozenset` object, or an diff --git a/Doc/c-api/tuple.rst b/Doc/c-api/tuple.rst index eb661fb..c0e53fb 100644 --- a/Doc/c-api/tuple.rst +++ b/Doc/c-api/tuple.rst @@ -105,3 +105,7 @@ Tuple Objects this function. If the object referenced by ``*p`` is replaced, the original ``*p`` is destroyed. On failure, returns ``-1`` and sets ``*p`` to *NULL*, and raises :exc:`MemoryError` or :exc:`SystemError`. + +.. cfunction:: int PyMethod_ClearFreeList(void) + + Clear the free list. Return the total number of freed items. diff --git a/Doc/c-api/type.rst b/Doc/c-api/type.rst index d1d4e45..bc0eeef 100644 --- a/Doc/c-api/type.rst +++ b/Doc/c-api/type.rst @@ -37,8 +37,6 @@ Type Objects Clears the internal lookup cache. Return the current version tag. - .. versionadded:: 2.6 - .. cfunction:: int PyType_HasFeature(PyObject *o, int feature) diff --git a/Doc/c-api/unicode.rst b/Doc/c-api/unicode.rst index 886ba65..448cf68 100644 --- a/Doc/c-api/unicode.rst +++ b/Doc/c-api/unicode.rst @@ -83,6 +83,11 @@ access internal read-only data of Unicode objects: Return a pointer to the internal buffer of the object. *o* has to be a :ctype:`PyUnicodeObject` (not checked). + +.. cfunction:: int PyUnicode_ClearFreeList(void) + + Clear the free list. Return the total number of freed items. + Unicode provides many different character properties. The most often needed ones are available through these macros which are mapped to C functions depending on the Python configuration. diff --git a/Doc/library/ctypes.rst b/Doc/library/ctypes.rst index 3c305ed..b2d672f 100644 --- a/Doc/library/ctypes.rst +++ b/Doc/library/ctypes.rst @@ -2010,7 +2010,6 @@ Fundamental data types their methods and attributes. .. versionchanged:: 2.6 - ctypes data types that are not and do not contain pointers can now be pickled. diff --git a/Doc/library/decimal.rst b/Doc/library/decimal.rst index afba964..b0845e9 100644 --- a/Doc/library/decimal.rst +++ b/Doc/library/decimal.rst @@ -1352,19 +1352,15 @@ to work with the :class:`Decimal` class:: '<.02>' """ - q = Decimal((0, (1,), -places)) # 2 places --> '0.01' - sign, digits, exp = value.quantize(q).as_tuple() - assert exp == -places + q = Decimal(10) ** -places # 2 places --> '0.01' + sign, digits, exp = value.quantize(q).as_tuple() result = [] digits = map(str, digits) build, next = result.append, digits.pop if sign: build(trailneg) for i in range(places): - if digits: - build(next()) - else: - build('0') + build(next() if digits else '0') build(dp) i = 0 while digits: @@ -1374,12 +1370,8 @@ to work with the :class:`Decimal` class:: i = 0 build(sep) build(curr) - if sign: - build(neg) - else: - build(pos) - result.reverse() - return ''.join(result) + build(neg if sign else pos) + return ''.join(reversed(result)) def pi(): """Compute Pi to the current precision. @@ -1482,7 +1474,7 @@ Decimal FAQ Q. It is cumbersome to type ``decimal.Decimal('1234.5')``. Is there a way to minimize typing when using the interactive interpreter? -\A. Some users abbreviate the constructor to just a single letter:: +A. Some users abbreviate the constructor to just a single letter:: >>> D = decimal.Decimal >>> D('1.23') + D('3.45') @@ -1513,9 +1505,36 @@ the :const:`Inexact` trap is set, it is also useful for validation:: Q. Once I have valid two place inputs, how do I maintain that invariant throughout an application? -A. Some operations like addition and subtraction automatically preserve fixed -point. Others, like multiplication and division, change the number of decimal -places and need to be followed-up with a :meth:`quantize` step. +A. Some operations like addition, subtraction, and multiplication by an integer +will automatically preserve fixed point. Others operations, like division and +non-integer multiplication, will change the number of decimal places and need to +be followed-up with a :meth:`quantize` step:: + + >>> a = Decimal('102.72') # Initial fixed-point values + >>> b = Decimal('3.17') + >>> a + b # Addition preserves fixed-point + Decimal('105.89') + >>> a - b + Decimal('99.55') + >>> a * 42 # So does integer multiplication + Decimal('4314.24') + >>> (a * b).quantize(TWOPLACES) # Must quantize non-integer multiplication + Decimal('325.62') + >>> (b / a).quantize(TWOPLACES) # And quantize division + Decimal('0.03') + +In developing fixed-point applications, it is convenient to define functions +to handle the :meth:`quantize` step:: + + >>> def mul(x, y, fp=TWOPLACES): + ... return (x * y).quantize(fp) + >>> def div(x, y, fp=TWOPLACES): + ... return (x / y).quantize(fp) + + >>> mul(a, b) # Automatically preserve fixed-point + Decimal('325.62') + >>> div(b, a) + Decimal('0.03') Q. There are many ways to express the same value. The numbers :const:`200`, :const:`200.000`, :const:`2E2`, and :const:`.02E+4` all have the same value at @@ -1537,6 +1556,16 @@ of significant places in the coefficient. For example, expressing :const:`5.0E+3` as :const:`5000` keeps the value constant but cannot show the original's two-place significance. +If an application does not care about tracking significance, it is easy to +remove the exponent and trailing zeroes, losing signficance, but keeping the +value unchanged:: + + >>> def remove_exponent(d): + ... return d.quantize(Decimal(1)) if d == d.to_integral() else d.normalize() + + >>> remove_exponent(Decimal('5E+3')) + Decimal('5000') + Q. Is there a way to convert a regular float to a :class:`Decimal`? A. Yes, all binary floating point numbers can be exactly expressed as a diff --git a/Doc/whatsnew/2.6.rst b/Doc/whatsnew/2.6.rst index d37c5ac..dae9b09 100644 --- a/Doc/whatsnew/2.6.rst +++ b/Doc/whatsnew/2.6.rst @@ -825,7 +825,7 @@ complete list of changes, or look through the CVS logs for all the details. int int >>> var._asdict() {'size': 4, 'type': 'int', 'id': 1, 'name': 'frequency'} - >>> v2 = var._replace('name', 'amplitude') + >>> v2 = var._replace(name='amplitude') >>> v2 variable(id=1, name='amplitude', type='int', size=4) |