mirror of
https://github.com/sphinx-doc/sphinx.git
synced 2025-02-25 18:55:22 -06:00
Fixes for bugs found by Tim Hatch.
This commit is contained in:
parent
d06a0780b2
commit
c62294f360
@ -66,6 +66,7 @@ docs@python.org), and we'll be glad to correct the problem.
|
|||||||
* Manus Hand
|
* Manus Hand
|
||||||
* Gerhard Häring
|
* Gerhard Häring
|
||||||
* Travis B. Hartwell
|
* Travis B. Hartwell
|
||||||
|
* Tim Hatch
|
||||||
* Janko Hauser
|
* Janko Hauser
|
||||||
* Bernhard Herzog
|
* Bernhard Herzog
|
||||||
* Magnus L. Hetland
|
* Magnus L. Hetland
|
||||||
|
@ -162,10 +162,10 @@ The following data items and methods are also supported:
|
|||||||
|
|
||||||
.. method:: array.fromunicode(s)
|
.. method:: array.fromunicode(s)
|
||||||
|
|
||||||
Extends this array with data from the given unicode string. The array must be a
|
Extends this array with data from the given unicode string. The array must
|
||||||
type ``'u'`` array; otherwise a :exc:`ValueError` is raised. Use
|
be a type ``'u'`` array; otherwise a :exc:`ValueError` is raised. Use
|
||||||
``array.fromstring(ustr.decode(enc))`` to append Unicode data to an array of
|
``array.fromstring(unicodestring.encode(enc))`` to append Unicode data to an
|
||||||
some other type.
|
array of some other type.
|
||||||
|
|
||||||
|
|
||||||
.. method:: array.index(x)
|
.. method:: array.index(x)
|
||||||
@ -244,13 +244,13 @@ When an array object is printed or converted to a string, it is represented as
|
|||||||
``array(typecode, initializer)``. The *initializer* is omitted if the array is
|
``array(typecode, initializer)``. The *initializer* is omitted if the array is
|
||||||
empty, otherwise it is a string if the *typecode* is ``'c'``, otherwise it is a
|
empty, otherwise it is a string if the *typecode* is ``'c'``, otherwise it is a
|
||||||
list of numbers. The string is guaranteed to be able to be converted back to an
|
list of numbers. The string is guaranteed to be able to be converted back to an
|
||||||
array with the same type and value using reverse quotes (``````), so long as the
|
array with the same type and value using :func:`eval`, so long as the
|
||||||
:func:`array` function has been imported using ``from array import array``.
|
:func:`array` function has been imported using ``from array import array``.
|
||||||
Examples::
|
Examples::
|
||||||
|
|
||||||
array('l')
|
array('l')
|
||||||
array('c', 'hello world')
|
array('c', 'hello world')
|
||||||
array('u', u'hello \textbackslash u2641')
|
array('u', u'hello \u2641')
|
||||||
array('l', [1, 2, 3, 4, 5])
|
array('l', [1, 2, 3, 4, 5])
|
||||||
array('d', [1.0, 2.0, 3.14])
|
array('d', [1.0, 2.0, 3.14])
|
||||||
|
|
||||||
|
@ -220,17 +220,18 @@ For simple text calendars this module provides the following functions.
|
|||||||
|
|
||||||
.. function:: leapdays(y1, y2)
|
.. function:: leapdays(y1, y2)
|
||||||
|
|
||||||
Returns the number of leap years in the range [*y1*...*y2*), where *y1* and *y2*
|
Returns the number of leap years in the range from *y1* to *y2* (exclusive),
|
||||||
are years.
|
where *y1* and *y2* are years.
|
||||||
|
|
||||||
.. versionchanged:: 2.0
|
.. versionchanged:: 2.0
|
||||||
This function didn't work for ranges spanning a century change in Python 1.5.2.
|
This function didn't work for ranges spanning a century change in Python
|
||||||
|
1.5.2.
|
||||||
|
|
||||||
|
|
||||||
.. function:: weekday(year, month, day)
|
.. function:: weekday(year, month, day)
|
||||||
|
|
||||||
Returns the day of the week (``0`` is Monday) for *year* (``1970``\ --...),
|
Returns the day of the week (``0`` is Monday) for *year* (``1970``--...),
|
||||||
*month* (``1``\ --\ ``12``), *day* (``1``\ --\ ``31``).
|
*month* (``1``--``12``), *day* (``1``--``31``).
|
||||||
|
|
||||||
|
|
||||||
.. function:: weekheader(n)
|
.. function:: weekheader(n)
|
||||||
|
@ -22,10 +22,10 @@ Future editions of the standard library may include balanced trees and
|
|||||||
ordered dictionaries.
|
ordered dictionaries.
|
||||||
|
|
||||||
.. versionchanged:: 2.5
|
.. versionchanged:: 2.5
|
||||||
Added defaultdict.
|
Added :class:`defaultdict`.
|
||||||
|
|
||||||
.. versionchanged:: 2.6
|
.. versionchanged:: 2.6
|
||||||
Added NamedTuple.
|
Added :class:`NamedTuple`.
|
||||||
|
|
||||||
|
|
||||||
.. _deque-objects:
|
.. _deque-objects:
|
||||||
@ -42,10 +42,10 @@ ordered dictionaries.
|
|||||||
Deques are a generalization of stacks and queues (the name is pronounced "deck"
|
Deques are a generalization of stacks and queues (the name is pronounced "deck"
|
||||||
and is short for "double-ended queue"). Deques support thread-safe, memory
|
and is short for "double-ended queue"). Deques support thread-safe, memory
|
||||||
efficient appends and pops from either side of the deque with approximately the
|
efficient appends and pops from either side of the deque with approximately the
|
||||||
same ``O(1)`` performance in either direction.
|
same O(1) performance in either direction.
|
||||||
|
|
||||||
Though :class:`list` objects support similar operations, they are optimized for
|
Though :class:`list` objects support similar operations, they are optimized for
|
||||||
fast fixed-length operations and incur ``O(n)`` memory movement costs for
|
fast fixed-length operations and incur O(n) memory movement costs for
|
||||||
``pop(0)`` and ``insert(0, v)`` operations which change both the size and
|
``pop(0)`` and ``insert(0, v)`` operations which change both the size and
|
||||||
position of the underlying data representation.
|
position of the underlying data representation.
|
||||||
|
|
||||||
@ -195,16 +195,16 @@ A roundrobin task server can be built from a :class:`deque` using
|
|||||||
:meth:`popleft` to select the current task and :meth:`append` to add it back to
|
:meth:`popleft` to select the current task and :meth:`append` to add it back to
|
||||||
the tasklist if the input stream is not exhausted::
|
the tasklist if the input stream is not exhausted::
|
||||||
|
|
||||||
def roundrobin(*iterables):
|
>>> def roundrobin(*iterables):
|
||||||
pending = deque(iter(i) for i in iterables)
|
... pending = deque(iter(i) for i in iterables)
|
||||||
while pending:
|
... while pending:
|
||||||
task = pending.popleft()
|
... task = pending.popleft()
|
||||||
try:
|
... try:
|
||||||
yield task.next()
|
... yield task.next()
|
||||||
except StopIteration:
|
... except StopIteration:
|
||||||
continue
|
... continue
|
||||||
pending.append(task)
|
... pending.append(task)
|
||||||
|
...
|
||||||
>>> for value in roundrobin('abc', 'd', 'efgh'):
|
>>> for value in roundrobin('abc', 'd', 'efgh'):
|
||||||
... print value
|
... print value
|
||||||
|
|
||||||
@ -226,13 +226,13 @@ queue.
|
|||||||
For example, building a balanced binary tree of nested lists entails reducing
|
For example, building a balanced binary tree of nested lists entails reducing
|
||||||
two adjacent nodes into one by grouping them in a list::
|
two adjacent nodes into one by grouping them in a list::
|
||||||
|
|
||||||
def maketree(iterable):
|
>>> def maketree(iterable):
|
||||||
d = deque(iterable)
|
... d = deque(iterable)
|
||||||
while len(d) > 1:
|
... while len(d) > 1:
|
||||||
pair = [d.popleft(), d.popleft()]
|
... pair = [d.popleft(), d.popleft()]
|
||||||
d.append(pair)
|
... d.append(pair)
|
||||||
return list(d)
|
... return list(d)
|
||||||
|
...
|
||||||
>>> print maketree('abcdefgh')
|
>>> print maketree('abcdefgh')
|
||||||
[[[['a', 'b'], ['c', 'd']], [['e', 'f'], ['g', 'h']]]]
|
[[[['a', 'b'], ['c', 'd']], [['e', 'f'], ['g', 'h']]]]
|
||||||
|
|
||||||
@ -298,8 +298,8 @@ sequence of key-value pairs into a dictionary of lists::
|
|||||||
>>> s = [('yellow', 1), ('blue', 2), ('yellow', 3), ('blue', 4), ('red', 1)]
|
>>> s = [('yellow', 1), ('blue', 2), ('yellow', 3), ('blue', 4), ('red', 1)]
|
||||||
>>> d = defaultdict(list)
|
>>> d = defaultdict(list)
|
||||||
>>> for k, v in s:
|
>>> for k, v in s:
|
||||||
d[k].append(v)
|
... d[k].append(v)
|
||||||
|
...
|
||||||
>>> d.items()
|
>>> d.items()
|
||||||
[('blue', [2, 4]), ('red', [1]), ('yellow', [1, 3])]
|
[('blue', [2, 4]), ('red', [1]), ('yellow', [1, 3])]
|
||||||
|
|
||||||
@ -313,8 +313,8 @@ simpler and faster than an equivalent technique using :meth:`dict.setdefault`::
|
|||||||
|
|
||||||
>>> d = {}
|
>>> d = {}
|
||||||
>>> for k, v in s:
|
>>> for k, v in s:
|
||||||
d.setdefault(k, []).append(v)
|
... d.setdefault(k, []).append(v)
|
||||||
|
...
|
||||||
>>> d.items()
|
>>> d.items()
|
||||||
[('blue', [2, 4]), ('red', [1]), ('yellow', [1, 3])]
|
[('blue', [2, 4]), ('red', [1]), ('yellow', [1, 3])]
|
||||||
|
|
||||||
@ -325,8 +325,8 @@ languages)::
|
|||||||
>>> s = 'mississippi'
|
>>> s = 'mississippi'
|
||||||
>>> d = defaultdict(int)
|
>>> d = defaultdict(int)
|
||||||
>>> for k in s:
|
>>> for k in s:
|
||||||
d[k] += 1
|
... d[k] += 1
|
||||||
|
...
|
||||||
>>> d.items()
|
>>> d.items()
|
||||||
[('i', 4), ('p', 2), ('s', 4), ('m', 1)]
|
[('i', 4), ('p', 2), ('s', 4), ('m', 1)]
|
||||||
|
|
||||||
@ -352,8 +352,8 @@ Setting the :attr:`default_factory` to :class:`set` makes the
|
|||||||
>>> s = [('red', 1), ('blue', 2), ('red', 3), ('blue', 4), ('red', 1), ('blue', 4)]
|
>>> s = [('red', 1), ('blue', 2), ('red', 3), ('blue', 4), ('red', 1), ('blue', 4)]
|
||||||
>>> d = defaultdict(set)
|
>>> d = defaultdict(set)
|
||||||
>>> for k, v in s:
|
>>> for k, v in s:
|
||||||
d[k].add(v)
|
... d[k].add(v)
|
||||||
|
...
|
||||||
>>> d.items()
|
>>> d.items()
|
||||||
[('blue', set([2, 4])), ('red', set([1, 3]))]
|
[('blue', set([2, 4])), ('red', set([1, 3]))]
|
||||||
|
|
||||||
|
@ -44,7 +44,7 @@ The :mod:`pprint` module defines one class:
|
|||||||
controlled by *depth*; if the data structure being printed is too deep, the next
|
controlled by *depth*; if the data structure being printed is too deep, the next
|
||||||
contained level is replaced by ``...``. By default, there is no constraint on
|
contained level is replaced by ``...``. By default, there is no constraint on
|
||||||
the depth of the objects being formatted. The desired output width is
|
the depth of the objects being formatted. The desired output width is
|
||||||
constrained using the *width* parameter; the default is eighty characters. If a
|
constrained using the *width* parameter; the default is 80 characters. If a
|
||||||
structure cannot be formatted within the constrained width, a best effort will
|
structure cannot be formatted within the constrained width, a best effort will
|
||||||
be made. ::
|
be made. ::
|
||||||
|
|
||||||
|
@ -75,7 +75,7 @@ See the source code for details. The public methods are:
|
|||||||
ignored in that case).
|
ignored in that case).
|
||||||
|
|
||||||
.. versionadded:: 2.3
|
.. versionadded:: 2.3
|
||||||
the timeout parameter.
|
The *timeout* parameter.
|
||||||
|
|
||||||
|
|
||||||
.. method:: Queue.put_nowait(item)
|
.. method:: Queue.put_nowait(item)
|
||||||
@ -93,7 +93,7 @@ See the source code for details. The public methods are:
|
|||||||
else raise the :exc:`Empty` exception (*timeout* is ignored in that case).
|
else raise the :exc:`Empty` exception (*timeout* is ignored in that case).
|
||||||
|
|
||||||
.. versionadded:: 2.3
|
.. versionadded:: 2.3
|
||||||
the timeout parameter.
|
The *timeout* parameter.
|
||||||
|
|
||||||
|
|
||||||
.. method:: Queue.get_nowait()
|
.. method:: Queue.get_nowait()
|
||||||
|
@ -101,11 +101,11 @@ which format specific object types.
|
|||||||
with ``level - 1`` for the value of *level* in the recursive call.
|
with ``level - 1`` for the value of *level* in the recursive call.
|
||||||
|
|
||||||
|
|
||||||
.. method:: Repr.repr_type(obj, level)
|
.. method:: Repr.repr_TYPE(obj, level)
|
||||||
:noindex:
|
:noindex:
|
||||||
|
|
||||||
Formatting methods for specific types are implemented as methods with a name
|
Formatting methods for specific types are implemented as methods with a name
|
||||||
based on the type name. In the method name, *type* is replaced by
|
based on the type name. In the method name, **TYPE** is replaced by
|
||||||
``string.join(string.split(type(obj).__name__, '_'))``. Dispatch to these
|
``string.join(string.split(type(obj).__name__, '_'))``. Dispatch to these
|
||||||
methods is handled by :meth:`repr1`. Type-specific methods which need to
|
methods is handled by :meth:`repr1`. Type-specific methods which need to
|
||||||
recursively format a value should call ``self.repr1(subobj, level - 1)``.
|
recursively format a value should call ``self.repr1(subobj, level - 1)``.
|
||||||
|
@ -88,16 +88,16 @@ operations:
|
|||||||
| ``s.issuperset(t)`` | ``s >= t`` | test whether every element in |
|
| ``s.issuperset(t)`` | ``s >= t`` | test whether every element in |
|
||||||
| | | *t* is in *s* |
|
| | | *t* is in *s* |
|
||||||
+-------------------------------+------------+---------------------------------+
|
+-------------------------------+------------+---------------------------------+
|
||||||
| ``s.union(t)`` | *s* \| *t* | new set with elements from both |
|
| ``s.union(t)`` | ``s | t`` | new set with elements from both |
|
||||||
| | | *s* and *t* |
|
| | | *s* and *t* |
|
||||||
+-------------------------------+------------+---------------------------------+
|
+-------------------------------+------------+---------------------------------+
|
||||||
| ``s.intersection(t)`` | *s* & *t* | new set with elements common to |
|
| ``s.intersection(t)`` | ``s & t`` | new set with elements common to |
|
||||||
| | | *s* and *t* |
|
| | | *s* and *t* |
|
||||||
+-------------------------------+------------+---------------------------------+
|
+-------------------------------+------------+---------------------------------+
|
||||||
| ``s.difference(t)`` | *s* - *t* | new set with elements in *s* |
|
| ``s.difference(t)`` | ``s - t`` | new set with elements in *s* |
|
||||||
| | | but not in *t* |
|
| | | but not in *t* |
|
||||||
+-------------------------------+------------+---------------------------------+
|
+-------------------------------+------------+---------------------------------+
|
||||||
| ``s.symmetric_difference(t)`` | *s* ^ *t* | new set with elements in either |
|
| ``s.symmetric_difference(t)`` | ``s ^ t`` | new set with elements in either |
|
||||||
| | | *s* or *t* but not both |
|
| | | *s* or *t* but not both |
|
||||||
+-------------------------------+------------+---------------------------------+
|
+-------------------------------+------------+---------------------------------+
|
||||||
| ``s.copy()`` | | new set with a shallow copy of |
|
| ``s.copy()`` | | new set with a shallow copy of |
|
||||||
@ -277,7 +277,7 @@ lessons learned from the :mod:`sets` module. The key differences are:
|
|||||||
* The built-in versions do not have a :meth:`union_update` method. Instead, use
|
* The built-in versions do not have a :meth:`union_update` method. Instead, use
|
||||||
the :meth:`update` method which is equivalent.
|
the :meth:`update` method which is equivalent.
|
||||||
|
|
||||||
* The built-in versions do not have a :meth:`_repr(sorted=True)` method.
|
* The built-in versions do not have a ``_repr(sorted=True)`` method.
|
||||||
Instead, use the built-in :func:`repr` and :func:`sorted` functions:
|
Instead, use the built-in :func:`repr` and :func:`sorted` functions:
|
||||||
``repr(sorted(s))``.
|
``repr(sorted(s))``.
|
||||||
|
|
||||||
|
@ -46,30 +46,32 @@ The module defines the following names:
|
|||||||
|
|
||||||
.. index:: builtin: type
|
.. index:: builtin: type
|
||||||
|
|
||||||
The type of type objects (such as returned by :func:`type`).
|
The type of type objects (such as returned by :func:`type`); alias of the
|
||||||
|
built-in :class:`type`.
|
||||||
|
|
||||||
|
|
||||||
.. data:: BooleanType
|
.. data:: BooleanType
|
||||||
|
|
||||||
The type of the :class:`bool` values ``True`` and ``False``; this is an alias of
|
The type of the :class:`bool` values ``True`` and ``False``; alias of the
|
||||||
the built-in :func:`bool` function.
|
built-in :class:`bool`.
|
||||||
|
|
||||||
.. versionadded:: 2.3
|
.. versionadded:: 2.3
|
||||||
|
|
||||||
|
|
||||||
.. data:: IntType
|
.. data:: IntType
|
||||||
|
|
||||||
The type of integers (e.g. ``1``).
|
The type of integers (e.g. ``1``); alias of the built-in :class:`int`.
|
||||||
|
|
||||||
|
|
||||||
.. data:: LongType
|
.. data:: LongType
|
||||||
|
|
||||||
The type of long integers (e.g. ``1L``).
|
The type of long integers (e.g. ``1L``); alias of the built-in :class:`long`.
|
||||||
|
|
||||||
|
|
||||||
.. data:: FloatType
|
.. data:: FloatType
|
||||||
|
|
||||||
The type of floating point numbers (e.g. ``1.0``).
|
The type of floating point numbers (e.g. ``1.0``); alias of the built-in
|
||||||
|
:class:`float`.
|
||||||
|
|
||||||
|
|
||||||
.. data:: ComplexType
|
.. data:: ComplexType
|
||||||
@ -80,28 +82,33 @@ The module defines the following names:
|
|||||||
|
|
||||||
.. data:: StringType
|
.. data:: StringType
|
||||||
|
|
||||||
The type of character strings (e.g. ``'Spam'``).
|
The type of character strings (e.g. ``'Spam'``); alias of the built-in
|
||||||
|
:class:`str`.
|
||||||
|
|
||||||
|
|
||||||
.. data:: UnicodeType
|
.. data:: UnicodeType
|
||||||
|
|
||||||
The type of Unicode character strings (e.g. ``u'Spam'``). This is not defined
|
The type of Unicode character strings (e.g. ``u'Spam'``). This is not defined
|
||||||
if Python was built without Unicode support.
|
if Python was built without Unicode support. It's an alias of the built-in
|
||||||
|
:class:`unicode`.
|
||||||
|
|
||||||
|
|
||||||
.. data:: TupleType
|
.. data:: TupleType
|
||||||
|
|
||||||
The type of tuples (e.g. ``(1, 2, 3, 'Spam')``).
|
The type of tuples (e.g. ``(1, 2, 3, 'Spam')``); alias of the built-in
|
||||||
|
:class:`tuple`.
|
||||||
|
|
||||||
|
|
||||||
.. data:: ListType
|
.. data:: ListType
|
||||||
|
|
||||||
The type of lists (e.g. ``[0, 1, 2, 3]``).
|
The type of lists (e.g. ``[0, 1, 2, 3]``); alias of the built-in
|
||||||
|
:class:`list`.
|
||||||
|
|
||||||
|
|
||||||
.. data:: DictType
|
.. data:: DictType
|
||||||
|
|
||||||
The type of dictionaries (e.g. ``{'Bacon': 1, 'Ham': 0}``).
|
The type of dictionaries (e.g. ``{'Bacon': 1, 'Ham': 0}``); alias of the
|
||||||
|
built-in :class:`dict`.
|
||||||
|
|
||||||
|
|
||||||
.. data:: DictionaryType
|
.. data:: DictionaryType
|
||||||
@ -171,21 +178,24 @@ The module defines the following names:
|
|||||||
|
|
||||||
.. data:: FileType
|
.. data:: FileType
|
||||||
|
|
||||||
The type of open file objects such as ``sys.stdout``.
|
The type of open file objects such as ``sys.stdout``; alias of the built-in
|
||||||
|
:class:`file`.
|
||||||
|
|
||||||
|
|
||||||
.. data:: XRangeType
|
.. data:: XRangeType
|
||||||
|
|
||||||
.. index:: builtin: xrange
|
.. index:: builtin: xrange
|
||||||
|
|
||||||
The type of range objects returned by :func:`xrange`.
|
The type of range objects returned by :func:`xrange`; alias of the built-in
|
||||||
|
:class:`xrange`.
|
||||||
|
|
||||||
|
|
||||||
.. data:: SliceType
|
.. data:: SliceType
|
||||||
|
|
||||||
.. index:: builtin: slice
|
.. index:: builtin: slice
|
||||||
|
|
||||||
The type of objects returned by :func:`slice`.
|
The type of objects returned by :func:`slice`; alias of the built-in
|
||||||
|
:class:`slice`.
|
||||||
|
|
||||||
|
|
||||||
.. data:: EllipsisType
|
.. data:: EllipsisType
|
||||||
@ -250,4 +260,3 @@ The module defines the following names:
|
|||||||
example: ``isinstance(s, types.StringTypes)``.
|
example: ``isinstance(s, types.StringTypes)``.
|
||||||
|
|
||||||
.. versionadded:: 2.2
|
.. versionadded:: 2.2
|
||||||
|
|
||||||
|
@ -95,8 +95,8 @@ The :mod:`UserList` module defines the :class:`UserList` class:
|
|||||||
Class that simulates a list. The instance's contents are kept in a regular
|
Class that simulates a list. The instance's contents are kept in a regular
|
||||||
list, which is accessible via the :attr:`data` attribute of :class:`UserList`
|
list, which is accessible via the :attr:`data` attribute of :class:`UserList`
|
||||||
instances. The instance's contents are initially set to a copy of *list*,
|
instances. The instance's contents are initially set to a copy of *list*,
|
||||||
defaulting to the empty list ``[]``. *list* can be either a regular Python
|
defaulting to the empty list ``[]``. *list* can be any iterable, e.g. a
|
||||||
list, or an instance of :class:`UserList` (or a subclass).
|
real Python list or a :class:`UserList` object.
|
||||||
|
|
||||||
In addition to supporting the methods and operations of mutable sequences (see
|
In addition to supporting the methods and operations of mutable sequences (see
|
||||||
section :ref:`typesseq`), :class:`UserList` instances provide the following
|
section :ref:`typesseq`), :class:`UserList` instances provide the following
|
||||||
|
@ -66,6 +66,7 @@ docs@python.org), and we'll be glad to correct the problem.
|
|||||||
* Manus Hand
|
* Manus Hand
|
||||||
* Gerhard Häring
|
* Gerhard Häring
|
||||||
* Travis B. Hartwell
|
* Travis B. Hartwell
|
||||||
|
* Tim Hatch
|
||||||
* Janko Hauser
|
* Janko Hauser
|
||||||
* Bernhard Herzog
|
* Bernhard Herzog
|
||||||
* Magnus L. Hetland
|
* Magnus L. Hetland
|
||||||
|
@ -162,10 +162,10 @@ The following data items and methods are also supported:
|
|||||||
|
|
||||||
.. method:: array.fromunicode(s)
|
.. method:: array.fromunicode(s)
|
||||||
|
|
||||||
Extends this array with data from the given unicode string. The array must be a
|
Extends this array with data from the given unicode string. The array must
|
||||||
type ``'u'`` array; otherwise a :exc:`ValueError` is raised. Use
|
be a type ``'u'`` array; otherwise a :exc:`ValueError` is raised. Use
|
||||||
``array.fromstring(ustr.decode(enc))`` to append Unicode data to an array of
|
``array.fromstring(unicodestring.encode(enc))`` to append Unicode data to an
|
||||||
some other type.
|
array of some other type.
|
||||||
|
|
||||||
|
|
||||||
.. method:: array.index(x)
|
.. method:: array.index(x)
|
||||||
@ -244,13 +244,13 @@ When an array object is printed or converted to a string, it is represented as
|
|||||||
``array(typecode, initializer)``. The *initializer* is omitted if the array is
|
``array(typecode, initializer)``. The *initializer* is omitted if the array is
|
||||||
empty, otherwise it is a string if the *typecode* is ``'c'``, otherwise it is a
|
empty, otherwise it is a string if the *typecode* is ``'c'``, otherwise it is a
|
||||||
list of numbers. The string is guaranteed to be able to be converted back to an
|
list of numbers. The string is guaranteed to be able to be converted back to an
|
||||||
array with the same type and value using reverse quotes (``````), so long as the
|
array with the same type and value using :func:`eval`, so long as the
|
||||||
:func:`array` function has been imported using ``from array import array``.
|
:func:`array` function has been imported using ``from array import array``.
|
||||||
Examples::
|
Examples::
|
||||||
|
|
||||||
array('l')
|
array('l')
|
||||||
array('c', 'hello world')
|
array('c', 'hello world')
|
||||||
array('u', u'hello \textbackslash u2641')
|
array('u', u'hello \u2641')
|
||||||
array('l', [1, 2, 3, 4, 5])
|
array('l', [1, 2, 3, 4, 5])
|
||||||
array('d', [1.0, 2.0, 3.14])
|
array('d', [1.0, 2.0, 3.14])
|
||||||
|
|
||||||
|
@ -220,17 +220,18 @@ For simple text calendars this module provides the following functions.
|
|||||||
|
|
||||||
.. function:: leapdays(y1, y2)
|
.. function:: leapdays(y1, y2)
|
||||||
|
|
||||||
Returns the number of leap years in the range [*y1*...*y2*), where *y1* and *y2*
|
Returns the number of leap years in the range from *y1* to *y2* (exclusive),
|
||||||
are years.
|
where *y1* and *y2* are years.
|
||||||
|
|
||||||
.. versionchanged:: 2.0
|
.. versionchanged:: 2.0
|
||||||
This function didn't work for ranges spanning a century change in Python 1.5.2.
|
This function didn't work for ranges spanning a century change in Python
|
||||||
|
1.5.2.
|
||||||
|
|
||||||
|
|
||||||
.. function:: weekday(year, month, day)
|
.. function:: weekday(year, month, day)
|
||||||
|
|
||||||
Returns the day of the week (``0`` is Monday) for *year* (``1970``\ --...),
|
Returns the day of the week (``0`` is Monday) for *year* (``1970``--...),
|
||||||
*month* (``1``\ --\ ``12``), *day* (``1``\ --\ ``31``).
|
*month* (``1``--``12``), *day* (``1``--``31``).
|
||||||
|
|
||||||
|
|
||||||
.. function:: weekheader(n)
|
.. function:: weekheader(n)
|
||||||
|
@ -22,10 +22,10 @@ Future editions of the standard library may include balanced trees and
|
|||||||
ordered dictionaries.
|
ordered dictionaries.
|
||||||
|
|
||||||
.. versionchanged:: 2.5
|
.. versionchanged:: 2.5
|
||||||
Added defaultdict.
|
Added :class:`defaultdict`.
|
||||||
|
|
||||||
.. versionchanged:: 2.6
|
.. versionchanged:: 2.6
|
||||||
Added NamedTuple.
|
Added :class:`NamedTuple`.
|
||||||
|
|
||||||
|
|
||||||
.. _deque-objects:
|
.. _deque-objects:
|
||||||
@ -42,10 +42,10 @@ ordered dictionaries.
|
|||||||
Deques are a generalization of stacks and queues (the name is pronounced "deck"
|
Deques are a generalization of stacks and queues (the name is pronounced "deck"
|
||||||
and is short for "double-ended queue"). Deques support thread-safe, memory
|
and is short for "double-ended queue"). Deques support thread-safe, memory
|
||||||
efficient appends and pops from either side of the deque with approximately the
|
efficient appends and pops from either side of the deque with approximately the
|
||||||
same ``O(1)`` performance in either direction.
|
same O(1) performance in either direction.
|
||||||
|
|
||||||
Though :class:`list` objects support similar operations, they are optimized for
|
Though :class:`list` objects support similar operations, they are optimized for
|
||||||
fast fixed-length operations and incur ``O(n)`` memory movement costs for
|
fast fixed-length operations and incur O(n) memory movement costs for
|
||||||
``pop(0)`` and ``insert(0, v)`` operations which change both the size and
|
``pop(0)`` and ``insert(0, v)`` operations which change both the size and
|
||||||
position of the underlying data representation.
|
position of the underlying data representation.
|
||||||
|
|
||||||
@ -195,16 +195,16 @@ A roundrobin task server can be built from a :class:`deque` using
|
|||||||
:meth:`popleft` to select the current task and :meth:`append` to add it back to
|
:meth:`popleft` to select the current task and :meth:`append` to add it back to
|
||||||
the tasklist if the input stream is not exhausted::
|
the tasklist if the input stream is not exhausted::
|
||||||
|
|
||||||
def roundrobin(*iterables):
|
>>> def roundrobin(*iterables):
|
||||||
pending = deque(iter(i) for i in iterables)
|
... pending = deque(iter(i) for i in iterables)
|
||||||
while pending:
|
... while pending:
|
||||||
task = pending.popleft()
|
... task = pending.popleft()
|
||||||
try:
|
... try:
|
||||||
yield next(task)
|
... yield next(task)
|
||||||
except StopIteration:
|
... except StopIteration:
|
||||||
continue
|
... continue
|
||||||
pending.append(task)
|
... pending.append(task)
|
||||||
|
...
|
||||||
>>> for value in roundrobin('abc', 'd', 'efgh'):
|
>>> for value in roundrobin('abc', 'd', 'efgh'):
|
||||||
... print value
|
... print value
|
||||||
|
|
||||||
@ -226,13 +226,13 @@ queue.
|
|||||||
For example, building a balanced binary tree of nested lists entails reducing
|
For example, building a balanced binary tree of nested lists entails reducing
|
||||||
two adjacent nodes into one by grouping them in a list::
|
two adjacent nodes into one by grouping them in a list::
|
||||||
|
|
||||||
def maketree(iterable):
|
>>> def maketree(iterable):
|
||||||
d = deque(iterable)
|
... d = deque(iterable)
|
||||||
while len(d) > 1:
|
... while len(d) > 1:
|
||||||
pair = [d.popleft(), d.popleft()]
|
... pair = [d.popleft(), d.popleft()]
|
||||||
d.append(pair)
|
... d.append(pair)
|
||||||
return list(d)
|
... return list(d)
|
||||||
|
...
|
||||||
>>> print maketree('abcdefgh')
|
>>> print maketree('abcdefgh')
|
||||||
[[[['a', 'b'], ['c', 'd']], [['e', 'f'], ['g', 'h']]]]
|
[[[['a', 'b'], ['c', 'd']], [['e', 'f'], ['g', 'h']]]]
|
||||||
|
|
||||||
@ -298,8 +298,8 @@ sequence of key-value pairs into a dictionary of lists::
|
|||||||
>>> s = [('yellow', 1), ('blue', 2), ('yellow', 3), ('blue', 4), ('red', 1)]
|
>>> s = [('yellow', 1), ('blue', 2), ('yellow', 3), ('blue', 4), ('red', 1)]
|
||||||
>>> d = defaultdict(list)
|
>>> d = defaultdict(list)
|
||||||
>>> for k, v in s:
|
>>> for k, v in s:
|
||||||
d[k].append(v)
|
... d[k].append(v)
|
||||||
|
...
|
||||||
>>> d.items()
|
>>> d.items()
|
||||||
[('blue', [2, 4]), ('red', [1]), ('yellow', [1, 3])]
|
[('blue', [2, 4]), ('red', [1]), ('yellow', [1, 3])]
|
||||||
|
|
||||||
@ -313,8 +313,8 @@ simpler and faster than an equivalent technique using :meth:`dict.setdefault`::
|
|||||||
|
|
||||||
>>> d = {}
|
>>> d = {}
|
||||||
>>> for k, v in s:
|
>>> for k, v in s:
|
||||||
d.setdefault(k, []).append(v)
|
... d.setdefault(k, []).append(v)
|
||||||
|
...
|
||||||
>>> d.items()
|
>>> d.items()
|
||||||
[('blue', [2, 4]), ('red', [1]), ('yellow', [1, 3])]
|
[('blue', [2, 4]), ('red', [1]), ('yellow', [1, 3])]
|
||||||
|
|
||||||
@ -325,8 +325,8 @@ languages)::
|
|||||||
>>> s = 'mississippi'
|
>>> s = 'mississippi'
|
||||||
>>> d = defaultdict(int)
|
>>> d = defaultdict(int)
|
||||||
>>> for k in s:
|
>>> for k in s:
|
||||||
d[k] += 1
|
... d[k] += 1
|
||||||
|
...
|
||||||
>>> d.items()
|
>>> d.items()
|
||||||
[('i', 4), ('p', 2), ('s', 4), ('m', 1)]
|
[('i', 4), ('p', 2), ('s', 4), ('m', 1)]
|
||||||
|
|
||||||
@ -352,8 +352,8 @@ Setting the :attr:`default_factory` to :class:`set` makes the
|
|||||||
>>> s = [('red', 1), ('blue', 2), ('red', 3), ('blue', 4), ('red', 1), ('blue', 4)]
|
>>> s = [('red', 1), ('blue', 2), ('red', 3), ('blue', 4), ('red', 1), ('blue', 4)]
|
||||||
>>> d = defaultdict(set)
|
>>> d = defaultdict(set)
|
||||||
>>> for k, v in s:
|
>>> for k, v in s:
|
||||||
d[k].add(v)
|
... d[k].add(v)
|
||||||
|
...
|
||||||
>>> d.items()
|
>>> d.items()
|
||||||
[('blue', set([2, 4])), ('red', set([1, 3]))]
|
[('blue', set([2, 4])), ('red', set([1, 3]))]
|
||||||
|
|
||||||
|
@ -44,7 +44,7 @@ The :mod:`pprint` module defines one class:
|
|||||||
controlled by *depth*; if the data structure being printed is too deep, the next
|
controlled by *depth*; if the data structure being printed is too deep, the next
|
||||||
contained level is replaced by ``...``. By default, there is no constraint on
|
contained level is replaced by ``...``. By default, there is no constraint on
|
||||||
the depth of the objects being formatted. The desired output width is
|
the depth of the objects being formatted. The desired output width is
|
||||||
constrained using the *width* parameter; the default is eighty characters. If a
|
constrained using the *width* parameter; the default is 80 characters. If a
|
||||||
structure cannot be formatted within the constrained width, a best effort will
|
structure cannot be formatted within the constrained width, a best effort will
|
||||||
be made. ::
|
be made. ::
|
||||||
|
|
||||||
|
@ -75,7 +75,7 @@ See the source code for details. The public methods are:
|
|||||||
ignored in that case).
|
ignored in that case).
|
||||||
|
|
||||||
.. versionadded:: 2.3
|
.. versionadded:: 2.3
|
||||||
the timeout parameter.
|
The *timeout* parameter.
|
||||||
|
|
||||||
|
|
||||||
.. method:: Queue.put_nowait(item)
|
.. method:: Queue.put_nowait(item)
|
||||||
@ -93,7 +93,7 @@ See the source code for details. The public methods are:
|
|||||||
else raise the :exc:`Empty` exception (*timeout* is ignored in that case).
|
else raise the :exc:`Empty` exception (*timeout* is ignored in that case).
|
||||||
|
|
||||||
.. versionadded:: 2.3
|
.. versionadded:: 2.3
|
||||||
the timeout parameter.
|
The *timeout* parameter.
|
||||||
|
|
||||||
|
|
||||||
.. method:: Queue.get_nowait()
|
.. method:: Queue.get_nowait()
|
||||||
|
@ -101,11 +101,11 @@ which format specific object types.
|
|||||||
with ``level - 1`` for the value of *level* in the recursive call.
|
with ``level - 1`` for the value of *level* in the recursive call.
|
||||||
|
|
||||||
|
|
||||||
.. method:: Repr.repr_type(obj, level)
|
.. method:: Repr.repr_TYPE(obj, level)
|
||||||
:noindex:
|
:noindex:
|
||||||
|
|
||||||
Formatting methods for specific types are implemented as methods with a name
|
Formatting methods for specific types are implemented as methods with a name
|
||||||
based on the type name. In the method name, *type* is replaced by
|
based on the type name. In the method name, **TYPE** is replaced by
|
||||||
``string.join(string.split(type(obj).__name__, '_'))``. Dispatch to these
|
``string.join(string.split(type(obj).__name__, '_'))``. Dispatch to these
|
||||||
methods is handled by :meth:`repr1`. Type-specific methods which need to
|
methods is handled by :meth:`repr1`. Type-specific methods which need to
|
||||||
recursively format a value should call ``self.repr1(subobj, level - 1)``.
|
recursively format a value should call ``self.repr1(subobj, level - 1)``.
|
||||||
|
@ -46,30 +46,32 @@ The module defines the following names:
|
|||||||
|
|
||||||
.. index:: builtin: type
|
.. index:: builtin: type
|
||||||
|
|
||||||
The type of type objects (such as returned by :func:`type`).
|
The type of type objects (such as returned by :func:`type`); alias of the
|
||||||
|
built-in :class:`type`.
|
||||||
|
|
||||||
|
|
||||||
.. data:: BooleanType
|
.. data:: BooleanType
|
||||||
|
|
||||||
The type of the :class:`bool` values ``True`` and ``False``; this is an alias of
|
The type of the :class:`bool` values ``True`` and ``False``; alias of the
|
||||||
the built-in :func:`bool` function.
|
built-in :class:`bool`.
|
||||||
|
|
||||||
.. versionadded:: 2.3
|
.. versionadded:: 2.3
|
||||||
|
|
||||||
|
|
||||||
.. data:: IntType
|
.. data:: IntType
|
||||||
|
|
||||||
The type of integers (e.g. ``1``).
|
The type of integers (e.g. ``1``); alias of the built-in :class:`int`.
|
||||||
|
|
||||||
|
|
||||||
.. data:: LongType
|
.. data:: LongType
|
||||||
|
|
||||||
The type of long integers (e.g. ``1L``).
|
The type of long integers (e.g. ``1L``); alias of the built-in :class:`long`.
|
||||||
|
|
||||||
|
|
||||||
.. data:: FloatType
|
.. data:: FloatType
|
||||||
|
|
||||||
The type of floating point numbers (e.g. ``1.0``).
|
The type of floating point numbers (e.g. ``1.0``); alias of the built-in
|
||||||
|
:class:`float`.
|
||||||
|
|
||||||
|
|
||||||
.. data:: ComplexType
|
.. data:: ComplexType
|
||||||
@ -80,28 +82,33 @@ The module defines the following names:
|
|||||||
|
|
||||||
.. data:: StringType
|
.. data:: StringType
|
||||||
|
|
||||||
The type of character strings (e.g. ``'Spam'``).
|
The type of character strings (e.g. ``'Spam'``); alias of the built-in
|
||||||
|
:class:`str`.
|
||||||
|
|
||||||
|
|
||||||
.. data:: UnicodeType
|
.. data:: UnicodeType
|
||||||
|
|
||||||
The type of Unicode character strings (e.g. ``u'Spam'``). This is not defined
|
The type of Unicode character strings (e.g. ``u'Spam'``). This is not defined
|
||||||
if Python was built without Unicode support.
|
if Python was built without Unicode support. It's an alias of the built-in
|
||||||
|
:class:`unicode`.
|
||||||
|
|
||||||
|
|
||||||
.. data:: TupleType
|
.. data:: TupleType
|
||||||
|
|
||||||
The type of tuples (e.g. ``(1, 2, 3, 'Spam')``).
|
The type of tuples (e.g. ``(1, 2, 3, 'Spam')``); alias of the built-in
|
||||||
|
:class:`tuple`.
|
||||||
|
|
||||||
|
|
||||||
.. data:: ListType
|
.. data:: ListType
|
||||||
|
|
||||||
The type of lists (e.g. ``[0, 1, 2, 3]``).
|
The type of lists (e.g. ``[0, 1, 2, 3]``); alias of the built-in
|
||||||
|
:class:`list`.
|
||||||
|
|
||||||
|
|
||||||
.. data:: DictType
|
.. data:: DictType
|
||||||
|
|
||||||
The type of dictionaries (e.g. ``{'Bacon': 1, 'Ham': 0}``).
|
The type of dictionaries (e.g. ``{'Bacon': 1, 'Ham': 0}``); alias of the
|
||||||
|
built-in :class:`dict`.
|
||||||
|
|
||||||
|
|
||||||
.. data:: DictionaryType
|
.. data:: DictionaryType
|
||||||
@ -166,21 +173,24 @@ The module defines the following names:
|
|||||||
|
|
||||||
.. data:: FileType
|
.. data:: FileType
|
||||||
|
|
||||||
The type of open file objects such as ``sys.stdout``.
|
The type of open file objects such as ``sys.stdout``; alias of the built-in
|
||||||
|
:class:`file`.
|
||||||
|
|
||||||
|
|
||||||
.. data:: RangeType
|
.. data:: RangeType
|
||||||
|
|
||||||
.. index:: builtin: range
|
.. index:: builtin: range
|
||||||
|
|
||||||
The type of range objects returned by :func:`range`.
|
The type of range objects returned by :func:`range`; alias of the built-in
|
||||||
|
:class:`range`.
|
||||||
|
|
||||||
|
|
||||||
.. data:: SliceType
|
.. data:: SliceType
|
||||||
|
|
||||||
.. index:: builtin: slice
|
.. index:: builtin: slice
|
||||||
|
|
||||||
The type of objects returned by :func:`slice`.
|
The type of objects returned by :func:`slice`; alias of the built-in
|
||||||
|
:class:`slice`.
|
||||||
|
|
||||||
|
|
||||||
.. data:: EllipsisType
|
.. data:: EllipsisType
|
||||||
@ -245,4 +255,3 @@ The module defines the following names:
|
|||||||
example: ``isinstance(s, types.StringTypes)``.
|
example: ``isinstance(s, types.StringTypes)``.
|
||||||
|
|
||||||
.. versionadded:: 2.2
|
.. versionadded:: 2.2
|
||||||
|
|
||||||
|
@ -95,8 +95,8 @@ The :mod:`UserList` module defines the :class:`UserList` class:
|
|||||||
Class that simulates a list. The instance's contents are kept in a regular
|
Class that simulates a list. The instance's contents are kept in a regular
|
||||||
list, which is accessible via the :attr:`data` attribute of :class:`UserList`
|
list, which is accessible via the :attr:`data` attribute of :class:`UserList`
|
||||||
instances. The instance's contents are initially set to a copy of *list*,
|
instances. The instance's contents are initially set to a copy of *list*,
|
||||||
defaulting to the empty list ``[]``. *list* can be either a regular Python
|
defaulting to the empty list ``[]``. *list* can be any iterable, e.g. a
|
||||||
list, or an instance of :class:`UserList` (or a subclass).
|
real Python list or a :class:`UserList` object.
|
||||||
|
|
||||||
In addition to supporting the methods and operations of mutable sequences (see
|
In addition to supporting the methods and operations of mutable sequences (see
|
||||||
section :ref:`typesseq`), :class:`UserList` instances provide the following
|
section :ref:`typesseq`), :class:`UserList` instances provide the following
|
||||||
|
Loading…
Reference in New Issue
Block a user