merge heads
This commit is contained in:
commit
8c76c53019
|
@ -266,20 +266,6 @@ Buffer related functions
|
|||
:cdata:`~Py_buffer.format`.
|
||||
|
||||
|
||||
.. cfunction:: int PyObject_CopyToObject(PyObject *obj, void *buf, Py_ssize_t len, char fortran)
|
||||
|
||||
Copy *len* bytes of data pointed to by the contiguous chunk of memory
|
||||
pointed to by *buf* into the buffer exported by obj. The buffer must of
|
||||
course be writable. Return 0 on success and return -1 and raise an error
|
||||
on failure. If the object does not have a writable buffer, then an error
|
||||
is raised. If *fortran* is ``'F'``, then if the object is
|
||||
multi-dimensional, then the data will be copied into the array in
|
||||
Fortran-style (first dimension varies the fastest). If *fortran* is
|
||||
``'C'``, then the data will be copied into the array in C-style (last
|
||||
dimension varies the fastest). If *fortran* is ``'A'``, then it does not
|
||||
matter and the copy will be made in whatever way is more efficient.
|
||||
|
||||
|
||||
.. cfunction:: int PyBuffer_IsContiguous(Py_buffer *view, char fortran)
|
||||
|
||||
Return 1 if the memory defined by the *view* is C-style (*fortran* is
|
||||
|
|
|
@ -442,41 +442,44 @@ Examples
|
|||
The simplest example of reading a CSV file::
|
||||
|
||||
import csv
|
||||
reader = csv.reader(open("some.csv", "rb"))
|
||||
for row in reader:
|
||||
print row
|
||||
with open('some.csv', 'rb') as f:
|
||||
reader = csv.reader(f)
|
||||
for row in reader:
|
||||
print row
|
||||
|
||||
Reading a file with an alternate format::
|
||||
|
||||
import csv
|
||||
reader = csv.reader(open("passwd", "rb"), delimiter=':', quoting=csv.QUOTE_NONE)
|
||||
for row in reader:
|
||||
print row
|
||||
with open('passwd', 'rb') as f:
|
||||
reader = csv.reader(f, delimiter=':', quoting=csv.QUOTE_NONE)
|
||||
for row in reader:
|
||||
print row
|
||||
|
||||
The corresponding simplest possible writing example is::
|
||||
|
||||
import csv
|
||||
writer = csv.writer(open("some.csv", "wb"))
|
||||
writer.writerows(someiterable)
|
||||
with open('some.csv', 'wb') as f:
|
||||
writer = csv.writer(f)
|
||||
writer.writerows(someiterable)
|
||||
|
||||
Registering a new dialect::
|
||||
|
||||
import csv
|
||||
|
||||
csv.register_dialect('unixpwd', delimiter=':', quoting=csv.QUOTE_NONE)
|
||||
|
||||
reader = csv.reader(open("passwd", "rb"), 'unixpwd')
|
||||
with open('passwd', 'rb') as f:
|
||||
reader = csv.reader(f, 'unixpwd')
|
||||
|
||||
A slightly more advanced use of the reader --- catching and reporting errors::
|
||||
|
||||
import csv, sys
|
||||
filename = "some.csv"
|
||||
reader = csv.reader(open(filename, "rb"))
|
||||
try:
|
||||
for row in reader:
|
||||
print row
|
||||
except csv.Error, e:
|
||||
sys.exit('file %s, line %d: %s' % (filename, reader.line_num, e))
|
||||
filename = 'some.csv'
|
||||
with open(filename, 'rb') as f:
|
||||
reader = csv.reader(f)
|
||||
try:
|
||||
for row in reader:
|
||||
print row
|
||||
except csv.Error, e:
|
||||
sys.exit('file %s, line %d: %s' % (filename, reader.line_num, e))
|
||||
|
||||
And while the module doesn't directly support parsing strings, it can easily be
|
||||
done::
|
||||
|
|
|
@ -63,18 +63,6 @@ The following exceptions are only used as base classes for other exceptions.
|
|||
assign a special meaning to the elements of this tuple, while others are
|
||||
usually called only with a single string giving an error message.
|
||||
|
||||
.. method:: with_traceback(tb)
|
||||
|
||||
This method sets *tb* as the new traceback for the exception and returns
|
||||
the exception object. It is usually used in exception handling code like
|
||||
this::
|
||||
|
||||
try:
|
||||
...
|
||||
except SomeException:
|
||||
tb = sys.exc_info()[2]
|
||||
raise OtherException(...).with_traceback(tb)
|
||||
|
||||
|
||||
.. exception:: Exception
|
||||
|
||||
|
|
|
@ -1895,6 +1895,8 @@ and :meth:`flush` methods).
|
|||
specified, the instance will use it for logging output; otherwise, *sys.stderr*
|
||||
will be used.
|
||||
|
||||
.. versionchanged:: 2.7
|
||||
The ``stream`` parameter was called ``strm`` in earlier versions.
|
||||
|
||||
.. method:: emit(record)
|
||||
|
||||
|
|
|
@ -47,7 +47,7 @@ The numeric tower
|
|||
To :class:`Complex`, :class:`Real` adds the operations that work on real
|
||||
numbers.
|
||||
|
||||
In short, those are: a conversion to :class:`float`, :func:`trunc`,
|
||||
In short, those are: a conversion to :class:`float`, :func:`math.trunc`,
|
||||
:func:`round`, :func:`math.floor`, :func:`math.ceil`, :func:`divmod`, ``//``,
|
||||
``%``, ``<``, ``<=``, ``>``, and ``>=``.
|
||||
|
||||
|
|
|
@ -2388,11 +2388,12 @@ Files have the following methods:
|
|||
|
||||
.. method:: file.readline([size])
|
||||
|
||||
Read one entire line from the file. A trailing newline character is kept in the
|
||||
string (but may be absent when a file ends with an incomplete line). [#]_ If
|
||||
the *size* argument is present and non-negative, it is a maximum byte count
|
||||
(including the trailing newline) and an incomplete line may be returned. An
|
||||
empty string is returned *only* when EOF is encountered immediately.
|
||||
Read one entire line from the file. A trailing newline character is kept in
|
||||
the string (but may be absent when a file ends with an incomplete line). [#]_
|
||||
If the *size* argument is present and non-negative, it is a maximum byte
|
||||
count (including the trailing newline) and an incomplete line may be
|
||||
returned. When *size* is not 0, an empty string is returned *only* when EOF
|
||||
is encountered immediately.
|
||||
|
||||
.. note::
|
||||
|
||||
|
|
|
@ -194,7 +194,7 @@ documentation explores the full feature set from first principles.
|
|||
|
||||
.. _unittest-command-line-interface:
|
||||
|
||||
Command Line Interface
|
||||
Command-Line Interface
|
||||
----------------------
|
||||
|
||||
The unittest module can be used from the command line to run tests from
|
||||
|
|
|
@ -103,17 +103,18 @@ Here are two ways to write a table of squares and cubes::
|
|||
(Note that in the first example, one space between each column was added by the
|
||||
way :keyword:`print` works: it always adds spaces between its arguments.)
|
||||
|
||||
This example demonstrates the :meth:`rjust` method of string objects, which
|
||||
right-justifies a string in a field of a given width by padding it with spaces
|
||||
on the left. There are similar methods :meth:`ljust` and :meth:`center`. These
|
||||
methods do not write anything, they just return a new string. If the input
|
||||
string is too long, they don't truncate it, but return it unchanged; this will
|
||||
mess up your column lay-out but that's usually better than the alternative,
|
||||
which would be lying about a value. (If you really want truncation you can
|
||||
always add a slice operation, as in ``x.ljust(n)[:n]``.)
|
||||
This example demonstrates the :meth:`str.rjust` method of string
|
||||
objects, which right-justifies a string in a field of a given width by padding
|
||||
it with spaces on the left. There are similar methods :meth:`str.ljust` and
|
||||
:meth:`str.center`. These methods do not write anything, they just return a
|
||||
new string. If the input string is too long, they don't truncate it, but
|
||||
return it unchanged; this will mess up your column lay-out but that's usually
|
||||
better than the alternative, which would be lying about a value. (If you
|
||||
really want truncation you can always add a slice operation, as in
|
||||
``x.ljust(n)[:n]``.)
|
||||
|
||||
There is another method, :meth:`zfill`, which pads a numeric string on the left
|
||||
with zeros. It understands about plus and minus signs::
|
||||
There is another method, :meth:`str.zfill`, which pads a numeric string on the
|
||||
left with zeros. It understands about plus and minus signs::
|
||||
|
||||
>>> '12'.zfill(5)
|
||||
'00012'
|
||||
|
@ -128,16 +129,16 @@ Basic usage of the :meth:`str.format` method looks like this::
|
|||
We are the knights who say "Ni!"
|
||||
|
||||
The brackets and characters within them (called format fields) are replaced with
|
||||
the objects passed into the :meth:`~str.format` method. A number in the
|
||||
the objects passed into the :meth:`str.format` method. A number in the
|
||||
brackets refers to the position of the object passed into the
|
||||
:meth:`~str.format` method. ::
|
||||
:meth:`str.format` method. ::
|
||||
|
||||
>>> print '{0} and {1}'.format('spam', 'eggs')
|
||||
spam and eggs
|
||||
>>> print '{1} and {0}'.format('spam', 'eggs')
|
||||
eggs and spam
|
||||
|
||||
If keyword arguments are used in the :meth:`~str.format` method, their values
|
||||
If keyword arguments are used in the :meth:`str.format` method, their values
|
||||
are referred to by using the name of the argument. ::
|
||||
|
||||
>>> print 'This {food} is {adjective}.'.format(
|
||||
|
@ -195,8 +196,8 @@ notation. ::
|
|||
>>> print 'Jack: {Jack:d}; Sjoerd: {Sjoerd:d}; Dcab: {Dcab:d}'.format(**table)
|
||||
Jack: 4098; Sjoerd: 4127; Dcab: 8637678
|
||||
|
||||
This is particularly useful in combination with the new built-in :func:`vars`
|
||||
function, which returns a dictionary containing all local variables.
|
||||
This is particularly useful in combination with the built-in function
|
||||
:func:`vars`, which returns a dictionary containing all local variables.
|
||||
|
||||
For a complete overview of string formatting with :meth:`str.format`, see
|
||||
:ref:`formatstrings`.
|
||||
|
|
|
@ -1068,14 +1068,16 @@ class Decimal(object):
|
|||
if ans:
|
||||
return ans
|
||||
|
||||
if not self:
|
||||
# -Decimal('0') is Decimal('0'), not Decimal('-0')
|
||||
if context is None:
|
||||
context = getcontext()
|
||||
|
||||
if not self and context.rounding != ROUND_FLOOR:
|
||||
# -Decimal('0') is Decimal('0'), not Decimal('-0'), except
|
||||
# in ROUND_FLOOR rounding mode.
|
||||
ans = self.copy_abs()
|
||||
else:
|
||||
ans = self.copy_negate()
|
||||
|
||||
if context is None:
|
||||
context = getcontext()
|
||||
return ans._fix(context)
|
||||
|
||||
def __pos__(self, context=None):
|
||||
|
@ -1088,14 +1090,15 @@ class Decimal(object):
|
|||
if ans:
|
||||
return ans
|
||||
|
||||
if not self:
|
||||
# + (-0) = 0
|
||||
if context is None:
|
||||
context = getcontext()
|
||||
|
||||
if not self and context.rounding != ROUND_FLOOR:
|
||||
# + (-0) = 0, except in ROUND_FLOOR rounding mode.
|
||||
ans = self.copy_abs()
|
||||
else:
|
||||
ans = Decimal(self)
|
||||
|
||||
if context is None:
|
||||
context = getcontext()
|
||||
return ans._fix(context)
|
||||
|
||||
def __abs__(self, round=True, context=None):
|
||||
|
|
|
@ -2745,3 +2745,73 @@ pwmx437 power 17 1728 1729 -> 1
|
|||
pwmx438 power 18 1728 1729 -> 1
|
||||
pwmx439 power 19 1728 1729 -> 456
|
||||
pwmx440 power 20 1728 1729 -> 1
|
||||
|
||||
-- plus and minus zero in various rounding modes (see issue 11131)
|
||||
extended: 1
|
||||
precision: 9
|
||||
maxexponent: 384
|
||||
minexponent: -383
|
||||
|
||||
rounding: half_even
|
||||
plux1000 plus 0.0 -> 0.0
|
||||
plux1001 plus -0.0 -> 0.0
|
||||
minx1000 minus 0.0 -> 0.0
|
||||
minx1001 minus -0.0 -> 0.0
|
||||
absx1000 abs 0.0 -> 0.0
|
||||
absx1001 abs -0.0 -> 0.0
|
||||
|
||||
rounding: half_up
|
||||
plux1010 plus 0.0 -> 0.0
|
||||
minx1010 minus 0.0 -> 0.0
|
||||
plux1011 plus -0.0 -> 0.0
|
||||
minx1011 minus -0.0 -> 0.0
|
||||
absx1010 abs 0.0 -> 0.0
|
||||
absx1011 abs -0.0 -> 0.0
|
||||
|
||||
rounding: ceiling
|
||||
plux1020 plus 0.0 -> 0.0
|
||||
minx1020 minus 0.0 -> 0.0
|
||||
plux1021 plus -0.0 -> 0.0
|
||||
minx1021 minus -0.0 -> 0.0
|
||||
absx1020 abs 0.0 -> 0.0
|
||||
absx1021 abs -0.0 -> 0.0
|
||||
|
||||
rounding: floor
|
||||
plux1030 plus 0.0 -> 0.0
|
||||
minx1030 minus 0.0 -> -0.0
|
||||
plux1031 plus -0.0 -> -0.0
|
||||
minx1031 minus -0.0 -> 0.0
|
||||
absx1030 abs 0.0 -> 0.0
|
||||
absx1031 abs -0.0 -> 0.0
|
||||
|
||||
rounding: down
|
||||
plux1040 plus 0.0 -> 0.0
|
||||
minx1040 minus 0.0 -> 0.0
|
||||
plux1041 plus -0.0 -> 0.0
|
||||
minx1041 minus -0.0 -> 0.0
|
||||
absx1040 abs 0.0 -> 0.0
|
||||
absx1041 abs -0.0 -> 0.0
|
||||
|
||||
rounding: up
|
||||
plux1050 plus 0.0 -> 0.0
|
||||
minx1050 minus 0.0 -> 0.0
|
||||
plux1051 plus -0.0 -> 0.0
|
||||
minx1051 minus -0.0 -> 0.0
|
||||
absx1050 abs 0.0 -> 0.0
|
||||
absx1051 abs -0.0 -> 0.0
|
||||
|
||||
rounding: half_down
|
||||
plux1060 plus 0.0 -> 0.0
|
||||
minx1060 minus 0.0 -> 0.0
|
||||
plux1061 plus -0.0 -> 0.0
|
||||
minx1061 minus -0.0 -> 0.0
|
||||
absx1060 abs 0.0 -> 0.0
|
||||
absx1061 abs -0.0 -> 0.0
|
||||
|
||||
rounding: 05up
|
||||
plux1070 plus 0.0 -> 0.0
|
||||
minx1070 minus 0.0 -> 0.0
|
||||
plux1071 plus -0.0 -> 0.0
|
||||
minx1071 minus -0.0 -> 0.0
|
||||
absx1070 abs 0.0 -> 0.0
|
||||
absx1071 abs -0.0 -> 0.0
|
||||
|
|
Loading…
Reference in New Issue