mirror of
https://github.com/python/cpython.git
synced 2025-11-03 03:22:27 +00:00
Update docs w.r.t. PEP 3100 changes -- patch for GHOP by Dan Finnie.
This commit is contained in:
parent
f25ef50549
commit
f694518331
48 changed files with 395 additions and 534 deletions
|
|
@ -826,10 +826,9 @@ to run the detector (the :func:`collect` function), as well as configuration
|
|||
interfaces and the ability to disable the detector at runtime. The cycle
|
||||
detector is considered an optional component; though it is included by default,
|
||||
it can be disabled at build time using the :option:`--without-cycle-gc` option
|
||||
to the :program:`configure` script on Unix platforms (including Mac OS X) or by
|
||||
removing the definition of ``WITH_CYCLE_GC`` in the :file:`pyconfig.h` header on
|
||||
other platforms. If the cycle detector is disabled in this way, the :mod:`gc`
|
||||
module will not be available.
|
||||
to the :program:`configure` script on Unix platforms (including Mac OS X). If
|
||||
the cycle detector is disabled in this way, the :mod:`gc` module will not be
|
||||
available.
|
||||
|
||||
|
||||
.. _refcountsinpython:
|
||||
|
|
|
|||
|
|
@ -314,7 +314,7 @@ this::
|
|||
Sets can take their contents from an iterable and let you iterate over the set's
|
||||
elements::
|
||||
|
||||
S = set((2, 3, 5, 7, 11, 13))
|
||||
S = {2, 3, 5, 7, 11, 13}
|
||||
for i in S:
|
||||
print(i)
|
||||
|
||||
|
|
@ -616,29 +616,26 @@ Built-in functions
|
|||
|
||||
Let's look in more detail at built-in functions often used with iterators.
|
||||
|
||||
Two of Python's built-in functions, :func:`map` and :func:`filter`, are somewhat
|
||||
obsolete; they duplicate the features of list comprehensions but return actual
|
||||
lists instead of iterators.
|
||||
Two of Python's built-in functions, :func:`map` and :func:`filter` duplicate the
|
||||
features of generator expressions:
|
||||
|
||||
``map(f, iterA, iterB, ...)`` returns a list containing ``f(iterA[0], iterB[0]),
|
||||
f(iterA[1], iterB[1]), f(iterA[2], iterB[2]), ...``.
|
||||
``map(f, iterA, iterB, ...)`` returns an iterator over the sequence
|
||||
``f(iterA[0], iterB[0]), f(iterA[1], iterB[1]), f(iterA[2], iterB[2]), ...``.
|
||||
|
||||
::
|
||||
|
||||
def upper(s):
|
||||
return s.upper()
|
||||
map(upper, ['sentence', 'fragment']) =>
|
||||
list(map(upper, ['sentence', 'fragment'])) =>
|
||||
['SENTENCE', 'FRAGMENT']
|
||||
|
||||
[upper(s) for s in ['sentence', 'fragment']] =>
|
||||
list(upper(s) for s in ['sentence', 'fragment']) =>
|
||||
['SENTENCE', 'FRAGMENT']
|
||||
|
||||
As shown above, you can achieve the same effect with a list comprehension. The
|
||||
:func:`itertools.imap` function does the same thing but can handle infinite
|
||||
iterators; it'll be discussed later, in the section on the :mod:`itertools` module.
|
||||
You can of course achieve the same effect with a list comprehension.
|
||||
|
||||
``filter(predicate, iter)`` returns a list that contains all the sequence
|
||||
elements that meet a certain condition, and is similarly duplicated by list
|
||||
``filter(predicate, iter)`` returns an iterator over all the sequence elements
|
||||
that meet a certain condition, and is similarly duplicated by list
|
||||
comprehensions. A **predicate** is a function that returns the truth value of
|
||||
some condition; for use with :func:`filter`, the predicate must take a single
|
||||
value.
|
||||
|
|
@ -648,47 +645,41 @@ value.
|
|||
def is_even(x):
|
||||
return (x % 2) == 0
|
||||
|
||||
filter(is_even, range(10)) =>
|
||||
list(filter(is_even, range(10))) =>
|
||||
[0, 2, 4, 6, 8]
|
||||
|
||||
This can also be written as a list comprehension::
|
||||
This can also be written as a generator expression::
|
||||
|
||||
>>> [x for x in range(10) if is_even(x)]
|
||||
>>> list(x for x in range(10) if is_even(x))
|
||||
[0, 2, 4, 6, 8]
|
||||
|
||||
:func:`filter` also has a counterpart in the :mod:`itertools` module,
|
||||
:func:`itertools.ifilter`, that returns an iterator and can therefore handle
|
||||
infinite sequences just as :func:`itertools.imap` can.
|
||||
|
||||
``reduce(func, iter, [initial_value])`` doesn't have a counterpart in the
|
||||
:mod:`itertools` module because it cumulatively performs an operation on all the
|
||||
iterable's elements and therefore can't be applied to infinite iterables.
|
||||
``func`` must be a function that takes two elements and returns a single value.
|
||||
:func:`reduce` takes the first two elements A and B returned by the iterator and
|
||||
calculates ``func(A, B)``. It then requests the third element, C, calculates
|
||||
``func(func(A, B), C)``, combines this result with the fourth element returned,
|
||||
and continues until the iterable is exhausted. If the iterable returns no
|
||||
values at all, a :exc:`TypeError` exception is raised. If the initial value is
|
||||
supplied, it's used as a starting point and ``func(initial_value, A)`` is the
|
||||
first calculation.
|
||||
|
||||
::
|
||||
``functools.reduce(func, iter, [initial_value])`` cumulatively performs an
|
||||
operation on all the iterable's elements and, therefore, can't be applied to
|
||||
infinite iterables. ``func`` must be a function that takes two elements and
|
||||
returns a single value. :func:`functools.reduce` takes the first two elements A
|
||||
and B returned by the iterator and calculates ``func(A, B)``. It then requests
|
||||
the third element, C, calculates ``func(func(A, B), C)``, combines this result
|
||||
with the fourth element returned, and continues until the iterable is exhausted.
|
||||
If the iterable returns no values at all, a :exc:`TypeError` exception is
|
||||
raised. If the initial value is supplied, it's used as a starting point and
|
||||
``func(initial_value, A)`` is the first calculation. ::
|
||||
|
||||
import operator
|
||||
reduce(operator.concat, ['A', 'BB', 'C']) =>
|
||||
import functools
|
||||
functools.reduce(operator.concat, ['A', 'BB', 'C']) =>
|
||||
'ABBC'
|
||||
reduce(operator.concat, []) =>
|
||||
functools.reduce(operator.concat, []) =>
|
||||
TypeError: reduce() of empty sequence with no initial value
|
||||
reduce(operator.mul, [1,2,3], 1) =>
|
||||
functools.reduce(operator.mul, [1,2,3], 1) =>
|
||||
6
|
||||
reduce(operator.mul, [], 1) =>
|
||||
functools.reduce(operator.mul, [], 1) =>
|
||||
1
|
||||
|
||||
If you use :func:`operator.add` with :func:`reduce`, you'll add up all the
|
||||
elements of the iterable. This case is so common that there's a special
|
||||
If you use :func:`operator.add` with :func:`functools.reduce`, you'll add up all
|
||||
the elements of the iterable. This case is so common that there's a special
|
||||
built-in called :func:`sum` to compute it::
|
||||
|
||||
reduce(operator.add, [1,2,3,4], 0) =>
|
||||
functools.reduce(operator.add, [1,2,3,4], 0) =>
|
||||
10
|
||||
sum([1,2,3,4]) =>
|
||||
10
|
||||
|
|
@ -699,7 +690,7 @@ For many uses of :func:`reduce`, though, it can be clearer to just write the
|
|||
obvious :keyword:`for` loop::
|
||||
|
||||
# Instead of:
|
||||
product = reduce(operator.mul, [1,2,3], 1)
|
||||
product = functools.reduce(operator.mul, [1,2,3], 1)
|
||||
|
||||
# You can write:
|
||||
product = 1
|
||||
|
|
@ -708,9 +699,7 @@ obvious :keyword:`for` loop::
|
|||
|
||||
|
||||
``enumerate(iter)`` counts off the elements in the iterable, returning 2-tuples
|
||||
containing the count and each element.
|
||||
|
||||
::
|
||||
containing the count and each element. ::
|
||||
|
||||
enumerate(['subject', 'verb', 'object']) =>
|
||||
(0, 'subject'), (1, 'verb'), (2, 'object')
|
||||
|
|
@ -723,12 +712,10 @@ indexes at which certain conditions are met::
|
|||
if line.strip() == '':
|
||||
print('Blank line at line #%i' % i)
|
||||
|
||||
``sorted(iterable, [cmp=None], [key=None], [reverse=False)`` collects all the
|
||||
elements of the iterable into a list, sorts the list, and returns the sorted
|
||||
result. The ``cmp``, ``key``, and ``reverse`` arguments are passed through to
|
||||
the constructed list's ``.sort()`` method.
|
||||
|
||||
::
|
||||
``sorted(iterable, [key=None], [reverse=False)`` collects all the elements of
|
||||
the iterable into a list, sorts the list, and returns the sorted result. The
|
||||
``key``, and ``reverse`` arguments are passed through to the constructed list's
|
||||
``sort()`` method. ::
|
||||
|
||||
import random
|
||||
# Generate 8 random numbers between [0, 10000)
|
||||
|
|
@ -962,14 +949,7 @@ consumed more than the others.
|
|||
Calling functions on elements
|
||||
-----------------------------
|
||||
|
||||
Two functions are used for calling other functions on the contents of an
|
||||
iterable.
|
||||
|
||||
``itertools.imap(f, iterA, iterB, ...)`` returns a stream containing
|
||||
``f(iterA[0], iterB[0]), f(iterA[1], iterB[1]), f(iterA[2], iterB[2]), ...``::
|
||||
|
||||
itertools.imap(operator.add, [5, 6, 5], [1, 2, 3]) =>
|
||||
6, 8, 8
|
||||
``itertools.imap(func, iter)`` is the same as built-in :func:`map`.
|
||||
|
||||
The ``operator`` module contains a set of functions corresponding to Python's
|
||||
operators. Some examples are ``operator.add(a, b)`` (adds two values),
|
||||
|
|
@ -992,14 +972,7 @@ Selecting elements
|
|||
Another group of functions chooses a subset of an iterator's elements based on a
|
||||
predicate.
|
||||
|
||||
``itertools.ifilter(predicate, iter)`` returns all the elements for which the
|
||||
predicate returns true::
|
||||
|
||||
def is_even(x):
|
||||
return (x % 2) == 0
|
||||
|
||||
itertools.ifilter(is_even, itertools.count()) =>
|
||||
0, 2, 4, 6, 8, 10, 12, 14, ...
|
||||
``itertools.ifilter(predicate, iter)`` is the same as built-in :func:`filter`.
|
||||
|
||||
``itertools.ifilterfalse(predicate, iter)`` is the opposite, returning all
|
||||
elements for which the predicate returns false::
|
||||
|
|
@ -1117,8 +1090,7 @@ that perform a single operation.
|
|||
|
||||
Some of the functions in this module are:
|
||||
|
||||
* Math operations: ``add()``, ``sub()``, ``mul()``, ``div()``, ``floordiv()``,
|
||||
``abs()``, ...
|
||||
* Math operations: ``add()``, ``sub()``, ``mul()``, ``floordiv()``, ``abs()``, ...
|
||||
* Logical operations: ``not_()``, ``truth()``.
|
||||
* Bitwise operations: ``and_()``, ``or_()``, ``invert()``.
|
||||
* Comparisons: ``eq()``, ``ne()``, ``lt()``, ``le()``, ``gt()``, and ``ge()``.
|
||||
|
|
@ -1190,7 +1162,7 @@ is equivalent to::
|
|||
f(*g(5, 6))
|
||||
|
||||
Even though ``compose()`` only accepts two functions, it's trivial to build up a
|
||||
version that will compose any number of functions. We'll use ``reduce()``,
|
||||
version that will compose any number of functions. We'll use ``functools.reduce()``,
|
||||
``compose()`` and ``partial()`` (the last of which is provided by both
|
||||
``functional`` and ``functools``).
|
||||
|
||||
|
|
@ -1198,7 +1170,7 @@ version that will compose any number of functions. We'll use ``reduce()``,
|
|||
|
||||
from functional import compose, partial
|
||||
|
||||
multi_compose = partial(reduce, compose)
|
||||
multi_compose = partial(functools.reduce, compose)
|
||||
|
||||
|
||||
We can also use ``map()``, ``compose()`` and ``partial()`` to craft a version of
|
||||
|
|
|
|||
|
|
@ -497,7 +497,7 @@ more convenient. If a program contains a lot of regular expressions, or re-uses
|
|||
the same ones in several locations, then it might be worthwhile to collect all
|
||||
the definitions in one place, in a section of code that compiles all the REs
|
||||
ahead of time. To take an example from the standard library, here's an extract
|
||||
from :file:`xmllib.py`::
|
||||
from the now deprecated :file:`xmllib.py`::
|
||||
|
||||
ref = re.compile( ... )
|
||||
entityref = re.compile( ... )
|
||||
|
|
|
|||
|
|
@ -237,129 +237,83 @@ Python's Unicode Support
|
|||
Now that you've learned the rudiments of Unicode, we can look at Python's
|
||||
Unicode features.
|
||||
|
||||
The String Type
|
||||
---------------
|
||||
|
||||
The Unicode Type
|
||||
----------------
|
||||
Since Python 3.0, the language features a ``str`` type that contain Unicode
|
||||
characters, meaning any string created using ``"unicode rocks!"``, ``'unicode
|
||||
rocks!``, or the triple-quoted string syntax is stored as Unicode.
|
||||
|
||||
Unicode strings are expressed as instances of the :class:`unicode` type, one of
|
||||
Python's repertoire of built-in types. It derives from an abstract type called
|
||||
:class:`basestring`, which is also an ancestor of the :class:`str` type; you can
|
||||
therefore check if a value is a string type with ``isinstance(value,
|
||||
basestring)``. Under the hood, Python represents Unicode strings as either 16-
|
||||
or 32-bit integers, depending on how the Python interpreter was compiled.
|
||||
To insert a Unicode character that is not part ASCII, e.g., any letters with
|
||||
accents, one can use escape sequences in their string literals as such::
|
||||
|
||||
The :func:`unicode` constructor has the signature ``unicode(string[, encoding,
|
||||
errors])``. All of its arguments should be 8-bit strings. The first argument
|
||||
is converted to Unicode using the specified encoding; if you leave off the
|
||||
``encoding`` argument, the ASCII encoding is used for the conversion, so
|
||||
characters greater than 127 will be treated as errors::
|
||||
>>> "\N{GREEK CAPITAL LETTER DELTA}" # Using the character name
|
||||
'\u0394'
|
||||
>>> "\u0394" # Using a 16-bit hex value
|
||||
'\u0394'
|
||||
>>> "\U00000394" # Using a 32-bit hex value
|
||||
'\u0394'
|
||||
|
||||
>>> unicode('abcdef')
|
||||
u'abcdef'
|
||||
>>> s = unicode('abcdef')
|
||||
>>> type(s)
|
||||
<type 'unicode'>
|
||||
>>> unicode('abcdef' + chr(255))
|
||||
Traceback (most recent call last):
|
||||
File "<stdin>", line 1, in ?
|
||||
UnicodeDecodeError: 'ascii' codec can't decode byte 0xff in position 6:
|
||||
ordinal not in range(128)
|
||||
In addition, one can create a string using the :func:`decode` method of
|
||||
:class:`bytes`. This method takes an encoding, such as UTF-8, and, optionally,
|
||||
an *errors* argument.
|
||||
|
||||
The ``errors`` argument specifies the response when the input string can't be
|
||||
The *errors* argument specifies the response when the input string can't be
|
||||
converted according to the encoding's rules. Legal values for this argument are
|
||||
'strict' (raise a ``UnicodeDecodeError`` exception), 'replace' (add U+FFFD,
|
||||
'strict' (raise a :exc:`UnicodeDecodeError` exception), 'replace' (add U+FFFD,
|
||||
'REPLACEMENT CHARACTER'), or 'ignore' (just leave the character out of the
|
||||
Unicode result). The following examples show the differences::
|
||||
|
||||
>>> unicode('\x80abc', errors='strict')
|
||||
>>> b'\x80abc'.decode("utf-8", "strict")
|
||||
Traceback (most recent call last):
|
||||
File "<stdin>", line 1, in ?
|
||||
UnicodeDecodeError: 'ascii' codec can't decode byte 0x80 in position 0:
|
||||
ordinal not in range(128)
|
||||
>>> unicode('\x80abc', errors='replace')
|
||||
u'\ufffdabc'
|
||||
>>> unicode('\x80abc', errors='ignore')
|
||||
u'abc'
|
||||
>>> b'\x80abc'.decode("utf-8", "replace")
|
||||
'\ufffdabc'
|
||||
>>> b'\x80abc'.decode("utf-8", "ignore")
|
||||
'abc'
|
||||
|
||||
Encodings are specified as strings containing the encoding's name. Python 2.4
|
||||
Encodings are specified as strings containing the encoding's name. Python
|
||||
comes with roughly 100 different encodings; see the Python Library Reference at
|
||||
<http://docs.python.org/lib/standard-encodings.html> for a list. Some encodings
|
||||
have multiple names; for example, 'latin-1', 'iso_8859_1' and '8859' are all
|
||||
synonyms for the same encoding.
|
||||
|
||||
One-character Unicode strings can also be created with the :func:`unichr`
|
||||
One-character Unicode strings can also be created with the :func:`chr`
|
||||
built-in function, which takes integers and returns a Unicode string of length 1
|
||||
that contains the corresponding code point. The reverse operation is the
|
||||
built-in :func:`ord` function that takes a one-character Unicode string and
|
||||
returns the code point value::
|
||||
|
||||
>>> unichr(40960)
|
||||
u'\ua000'
|
||||
>>> ord(u'\ua000')
|
||||
>>> chr(40960)
|
||||
'\ua000'
|
||||
>>> ord('\ua000')
|
||||
40960
|
||||
|
||||
Instances of the :class:`unicode` type have many of the same methods as the
|
||||
8-bit string type for operations such as searching and formatting::
|
||||
Converting to Bytes
|
||||
-------------------
|
||||
|
||||
>>> s = u'Was ever feather so lightly blown to and fro as this multitude?'
|
||||
>>> s.count('e')
|
||||
5
|
||||
>>> s.find('feather')
|
||||
9
|
||||
>>> s.find('bird')
|
||||
-1
|
||||
>>> s.replace('feather', 'sand')
|
||||
u'Was ever sand so lightly blown to and fro as this multitude?'
|
||||
>>> s.upper()
|
||||
u'WAS EVER FEATHER SO LIGHTLY BLOWN TO AND FRO AS THIS MULTITUDE?'
|
||||
|
||||
Note that the arguments to these methods can be Unicode strings or 8-bit
|
||||
strings. 8-bit strings will be converted to Unicode before carrying out the
|
||||
operation; Python's default ASCII encoding will be used, so characters greater
|
||||
than 127 will cause an exception::
|
||||
|
||||
>>> s.find('Was\x9f')
|
||||
Traceback (most recent call last):
|
||||
File "<stdin>", line 1, in ?
|
||||
UnicodeDecodeError: 'ascii' codec can't decode byte 0x9f in position 3: ordinal not in range(128)
|
||||
>>> s.find(u'Was\x9f')
|
||||
-1
|
||||
|
||||
Much Python code that operates on strings will therefore work with Unicode
|
||||
strings without requiring any changes to the code. (Input and output code needs
|
||||
more updating for Unicode; more on this later.)
|
||||
|
||||
Another important method is ``.encode([encoding], [errors='strict'])``, which
|
||||
returns an 8-bit string version of the Unicode string, encoded in the requested
|
||||
encoding. The ``errors`` parameter is the same as the parameter of the
|
||||
``unicode()`` constructor, with one additional possibility; as well as 'strict',
|
||||
Another important str method is ``.encode([encoding], [errors='strict'])``,
|
||||
which returns a ``bytes`` representation of the Unicode string, encoded in the
|
||||
requested encoding. The ``errors`` parameter is the same as the parameter of
|
||||
the :meth:`decode` method, with one additional possibility; as well as 'strict',
|
||||
'ignore', and 'replace', you can also pass 'xmlcharrefreplace' which uses XML's
|
||||
character references. The following example shows the different results::
|
||||
|
||||
>>> u = unichr(40960) + u'abcd' + unichr(1972)
|
||||
>>> u = chr(40960) + 'abcd' + chr(1972)
|
||||
>>> u.encode('utf-8')
|
||||
'\xea\x80\x80abcd\xde\xb4'
|
||||
b'\xea\x80\x80abcd\xde\xb4'
|
||||
>>> u.encode('ascii')
|
||||
Traceback (most recent call last):
|
||||
File "<stdin>", line 1, in ?
|
||||
UnicodeEncodeError: 'ascii' codec can't encode character '\ua000' in position 0: ordinal not in range(128)
|
||||
>>> u.encode('ascii', 'ignore')
|
||||
'abcd'
|
||||
b'abcd'
|
||||
>>> u.encode('ascii', 'replace')
|
||||
'?abcd?'
|
||||
b'?abcd?'
|
||||
>>> u.encode('ascii', 'xmlcharrefreplace')
|
||||
'ꀀabcd޴'
|
||||
|
||||
Python's 8-bit strings have a ``.decode([encoding], [errors])`` method that
|
||||
interprets the string using the given encoding::
|
||||
|
||||
>>> u = unichr(40960) + u'abcd' + unichr(1972) # Assemble a string
|
||||
>>> utf8_version = u.encode('utf-8') # Encode as UTF-8
|
||||
>>> type(utf8_version), utf8_version
|
||||
(<type 'str'>, '\xea\x80\x80abcd\xde\xb4')
|
||||
>>> u2 = utf8_version.decode('utf-8') # Decode using UTF-8
|
||||
>>> u == u2 # The two strings match
|
||||
True
|
||||
b'ꀀabcd޴'
|
||||
|
||||
The low-level routines for registering and accessing the available encodings are
|
||||
found in the :mod:`codecs` module. However, the encoding and decoding functions
|
||||
|
|
@ -377,21 +331,13 @@ output.
|
|||
Unicode Literals in Python Source Code
|
||||
--------------------------------------
|
||||
|
||||
In Python source code, Unicode literals are written as strings prefixed with the
|
||||
'u' or 'U' character: ``u'abcdefghijk'``. Specific code points can be written
|
||||
using the ``\u`` escape sequence, which is followed by four hex digits giving
|
||||
the code point. The ``\U`` escape sequence is similar, but expects 8 hex
|
||||
digits, not 4.
|
||||
In Python source code, specific Unicode code points can be written using the
|
||||
``\u`` escape sequence, which is followed by four hex digits giving the code
|
||||
point. The ``\U`` escape sequence is similar, but expects 8 hex digits, not 4::
|
||||
|
||||
Unicode literals can also use the same escape sequences as 8-bit strings,
|
||||
including ``\x``, but ``\x`` only takes two hex digits so it can't express an
|
||||
arbitrary code point. Octal escapes can go up to U+01ff, which is octal 777.
|
||||
|
||||
::
|
||||
|
||||
>>> s = u"a\xac\u1234\u20ac\U00008000"
|
||||
>>> s = "a\xac\u1234\u20ac\U00008000"
|
||||
^^^^ two-digit hex escape
|
||||
^^^^^^ four-digit Unicode escape
|
||||
^^^^^ four-digit Unicode escape
|
||||
^^^^^^^^^^ eight-digit Unicode escape
|
||||
>>> for c in s: print(ord(c), end=" ")
|
||||
...
|
||||
|
|
@ -400,7 +346,7 @@ arbitrary code point. Octal escapes can go up to U+01ff, which is octal 777.
|
|||
Using escape sequences for code points greater than 127 is fine in small doses,
|
||||
but becomes an annoyance if you're using many accented characters, as you would
|
||||
in a program with messages in French or some other accent-using language. You
|
||||
can also assemble strings using the :func:`unichr` built-in function, but this is
|
||||
can also assemble strings using the :func:`chr` built-in function, but this is
|
||||
even more tedious.
|
||||
|
||||
Ideally, you'd want to be able to write literals in your language's natural
|
||||
|
|
@ -408,14 +354,15 @@ encoding. You could then edit Python source code with your favorite editor
|
|||
which would display the accented characters naturally, and have the right
|
||||
characters used at runtime.
|
||||
|
||||
Python supports writing Unicode literals in any encoding, but you have to
|
||||
declare the encoding being used. This is done by including a special comment as
|
||||
either the first or second line of the source file::
|
||||
Python supports writing Unicode literals in UTF-8 by default, but you can use
|
||||
(almost) any encoding if you declare the encoding being used. This is done by
|
||||
including a special comment as either the first or second line of the source
|
||||
file::
|
||||
|
||||
#!/usr/bin/env python
|
||||
# -*- coding: latin-1 -*-
|
||||
|
||||
u = u'abcdé'
|
||||
u = 'abcdé'
|
||||
print(ord(u[-1]))
|
||||
|
||||
The syntax is inspired by Emacs's notation for specifying variables local to a
|
||||
|
|
@ -424,22 +371,8 @@ file. Emacs supports many different variables, but Python only supports
|
|||
them, you must supply the name ``coding`` and the name of your chosen encoding,
|
||||
separated by ``':'``.
|
||||
|
||||
If you don't include such a comment, the default encoding used will be ASCII.
|
||||
Versions of Python before 2.4 were Euro-centric and assumed Latin-1 as a default
|
||||
encoding for string literals; in Python 2.4, characters greater than 127 still
|
||||
work but result in a warning. For example, the following program has no
|
||||
encoding declaration::
|
||||
|
||||
#!/usr/bin/env python
|
||||
u = u'abcdé'
|
||||
print(ord(u[-1]))
|
||||
|
||||
When you run it with Python 2.4, it will output the following warning::
|
||||
|
||||
amk:~$ python p263.py
|
||||
sys:1: DeprecationWarning: Non-ASCII character '\xe9'
|
||||
in file p263.py on line 2, but no encoding declared;
|
||||
see http://www.python.org/peps/pep-0263.html for details
|
||||
If you don't include such a comment, the default encoding used will be UTF-8 as
|
||||
already mentioned.
|
||||
|
||||
|
||||
Unicode Properties
|
||||
|
|
@ -457,7 +390,7 @@ prints the numeric value of one particular character::
|
|||
|
||||
import unicodedata
|
||||
|
||||
u = unichr(233) + unichr(0x0bf2) + unichr(3972) + unichr(6000) + unichr(13231)
|
||||
u = chr(233) + chr(0x0bf2) + chr(3972) + chr(6000) + chr(13231)
|
||||
|
||||
for i, c in enumerate(u):
|
||||
print(i, '%04x' % ord(c), unicodedata.category(c), end=" ")
|
||||
|
|
@ -487,8 +420,8 @@ list of category codes.
|
|||
References
|
||||
----------
|
||||
|
||||
The Unicode and 8-bit string types are described in the Python library reference
|
||||
at :ref:`typesseq`.
|
||||
The ``str`` type is described in the Python library reference at
|
||||
:ref:`typesseq`.
|
||||
|
||||
The documentation for the :mod:`unicodedata` module.
|
||||
|
||||
|
|
@ -557,7 +490,7 @@ It's also possible to open files in update mode, allowing both reading and
|
|||
writing::
|
||||
|
||||
f = codecs.open('test', encoding='utf-8', mode='w+')
|
||||
f.write(u'\u4500 blah blah blah\n')
|
||||
f.write('\u4500 blah blah blah\n')
|
||||
f.seek(0)
|
||||
print(repr(f.readline()[:1]))
|
||||
f.close()
|
||||
|
|
@ -590,7 +523,7 @@ not much reason to bother. When opening a file for reading or writing, you can
|
|||
usually just provide the Unicode string as the filename, and it will be
|
||||
automatically converted to the right encoding for you::
|
||||
|
||||
filename = u'filename\u4500abc'
|
||||
filename = 'filename\u4500abc'
|
||||
f = open(filename, 'w')
|
||||
f.write('blah\n')
|
||||
f.close()
|
||||
|
|
@ -607,7 +540,7 @@ encoding and a list of Unicode strings will be returned, while passing an 8-bit
|
|||
path will return the 8-bit versions of the filenames. For example, assuming the
|
||||
default filesystem encoding is UTF-8, running the following program::
|
||||
|
||||
fn = u'filename\u4500abc'
|
||||
fn = 'filename\u4500abc'
|
||||
f = open(fn, 'w')
|
||||
f.close()
|
||||
|
||||
|
|
@ -619,7 +552,7 @@ will produce the following output::
|
|||
|
||||
amk:~$ python t.py
|
||||
['.svn', 'filename\xe4\x94\x80abc', ...]
|
||||
[u'.svn', u'filename\u4500abc', ...]
|
||||
['.svn', 'filename\u4500abc', ...]
|
||||
|
||||
The first list contains UTF-8-encoded filenames, and the second list contains
|
||||
the Unicode versions.
|
||||
|
|
|
|||
|
|
@ -183,18 +183,6 @@ The following data items and methods are also supported:
|
|||
returned.
|
||||
|
||||
|
||||
.. method:: array.read(f, n)
|
||||
|
||||
.. deprecated:: 1.5.1
|
||||
Use the :meth:`fromfile` method.
|
||||
|
||||
Read *n* items (as machine values) from the file object *f* and append them to
|
||||
the end of the array. If less than *n* items are available, :exc:`EOFError` is
|
||||
raised, but the items that were available are still inserted into the array.
|
||||
*f* must be a real built-in file object; something else with a :meth:`read`
|
||||
method won't do.
|
||||
|
||||
|
||||
.. method:: array.remove(x)
|
||||
|
||||
Remove the first occurrence of *x* from the array.
|
||||
|
|
@ -229,13 +217,6 @@ The following data items and methods are also supported:
|
|||
obtain a unicode string from an array of some other type.
|
||||
|
||||
|
||||
.. method:: array.write(f)
|
||||
|
||||
.. deprecated:: 1.5.1
|
||||
Use the :meth:`tofile` method.
|
||||
|
||||
Write all items (as machine values) to the file object *f*.
|
||||
|
||||
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
|
||||
empty, otherwise it is a string if the *typecode* is ``'c'``, otherwise it is a
|
||||
|
|
|
|||
|
|
@ -403,7 +403,7 @@ they add the ability to access fields by name instead of position index.
|
|||
Any valid Python identifier may be used for a fieldname except for names
|
||||
starting with an underscore. Valid identifiers consist of letters, digits,
|
||||
and underscores but do not start with a digit or underscore and cannot be
|
||||
a :mod:`keyword` such as *class*, *for*, *return*, *global*, *pass*, *print*,
|
||||
a :mod:`keyword` such as *class*, *for*, *return*, *global*, *pass*,
|
||||
or *raise*.
|
||||
|
||||
If *verbose* is true, the class definition is printed just before being built.
|
||||
|
|
|
|||
|
|
@ -199,7 +199,7 @@ RawConfigParser Objects
|
|||
.. method:: RawConfigParser.read(filenames)
|
||||
|
||||
Attempt to read and parse a list of filenames, returning a list of filenames
|
||||
which were successfully parsed. If *filenames* is a string or Unicode string,
|
||||
which were successfully parsed. If *filenames* is a string,
|
||||
it is treated as a single filename. If a file named in *filenames* cannot be
|
||||
opened, that file will be ignored. This is designed so that you can specify a
|
||||
list of potential configuration file locations (for example, the current
|
||||
|
|
@ -330,8 +330,8 @@ The :class:`SafeConfigParser` class implements the same extended interface as
|
|||
.. method:: SafeConfigParser.set(section, option, value)
|
||||
|
||||
If the given section exists, set the given option to the specified value;
|
||||
otherwise raise :exc:`NoSectionError`. *value* must be a string (:class:`str`
|
||||
or :class:`unicode`); if not, :exc:`TypeError` is raised.
|
||||
otherwise raise :exc:`NoSectionError`. *value* must be a string; if it is
|
||||
not, :exc:`TypeError` is raised.
|
||||
|
||||
|
||||
Examples
|
||||
|
|
@ -373,12 +373,12 @@ An example of reading the configuration file again::
|
|||
# getint() and getboolean() also do this for their respective types
|
||||
float = config.getfloat('Section1', 'float')
|
||||
int = config.getint('Section1', 'int')
|
||||
print float + int
|
||||
print(float + int)
|
||||
|
||||
# Notice that the next output does not interpolate '%(bar)s' or '%(baz)s'.
|
||||
# This is because we are using a RawConfigParser().
|
||||
if config.getboolean('Section1', 'bool'):
|
||||
print config.get('Section1', 'foo')
|
||||
print(config.get('Section1', 'foo'))
|
||||
|
||||
To get interpolation, you will need to use a :class:`ConfigParser` or
|
||||
:class:`SafeConfigParser`::
|
||||
|
|
@ -389,13 +389,13 @@ To get interpolation, you will need to use a :class:`ConfigParser` or
|
|||
config.read('example.cfg')
|
||||
|
||||
# Set the third, optional argument of get to 1 if you wish to use raw mode.
|
||||
print config.get('Section1', 'foo', 0) # -> "Python is fun!"
|
||||
print config.get('Section1', 'foo', 1) # -> "%(bar)s is %(baz)s!"
|
||||
print(config.get('Section1', 'foo', 0)) # -> "Python is fun!"
|
||||
print(config.get('Section1', 'foo', 1)) # -> "%(bar)s is %(baz)s!"
|
||||
|
||||
# The optional fourth argument is a dict with members that will take
|
||||
# precedence in interpolation.
|
||||
print config.get('Section1', 'foo', 0, {'bar': 'Documentation',
|
||||
'baz': 'evil'})
|
||||
print(config.get('Section1', 'foo', 0, {'bar': 'Documentation',
|
||||
'baz': 'evil'}))
|
||||
|
||||
Defaults are available in all three types of ConfigParsers. They are used in
|
||||
interpolation if an option used is not defined elsewhere. ::
|
||||
|
|
@ -406,10 +406,10 @@ interpolation if an option used is not defined elsewhere. ::
|
|||
config = ConfigParser.SafeConfigParser({'bar': 'Life', 'baz': 'hard'})
|
||||
config.read('example.cfg')
|
||||
|
||||
print config.get('Section1', 'foo') # -> "Python is fun!"
|
||||
print(config.get('Section1', 'foo')) # -> "Python is fun!"
|
||||
config.remove_option('Section1', 'bar')
|
||||
config.remove_option('Section1', 'baz')
|
||||
print config.get('Section1', 'foo') # -> "Life is hard!"
|
||||
print(config.get('Section1', 'foo')) # -> "Life is hard!"
|
||||
|
||||
The function ``opt_move`` below can be used to move options between sections::
|
||||
|
||||
|
|
|
|||
|
|
@ -86,7 +86,7 @@ The :mod:`csv` module defines the following functions:
|
|||
>>> import csv
|
||||
>>> spamReader = csv.reader(open('eggs.csv'), delimiter=' ', quotechar='|')
|
||||
>>> for row in spamReader:
|
||||
... print ', '.join(row)
|
||||
... print(', '.join(row))
|
||||
Spam, Spam, Spam, Spam, Spam, Baked Beans
|
||||
Spam, Lovely Spam, Wonderful Spam
|
||||
|
||||
|
|
@ -121,7 +121,7 @@ The :mod:`csv` module defines the following functions:
|
|||
|
||||
.. function:: register_dialect(name[, dialect][, fmtparam])
|
||||
|
||||
Associate *dialect* with *name*. *name* must be a string or Unicode object. The
|
||||
Associate *dialect* with *name*. *name* must be a string. The
|
||||
dialect can be specified either by passing a sub-class of :class:`Dialect`, or
|
||||
by *fmtparam* keyword arguments, or both, with keyword arguments overriding
|
||||
parameters of the dialect. For full details about the dialect and formatting
|
||||
|
|
|
|||
|
|
@ -11,8 +11,8 @@ queues, and sets.
|
|||
|
||||
Python also provides some built-in data types, in particular,
|
||||
:class:`dict`, :class:`list`, :class:`set` and :class:`frozenset`, and
|
||||
:class:`tuple`. The :class:`str` class can be used to handle binary data
|
||||
and 8-bit text, and the :class:`unicode` class to handle Unicode text.
|
||||
:class:`tuple`. The :class:`str` class can be used to strings, including
|
||||
Unicode strings, and the :class:`bytes` class to handle binary data.
|
||||
|
||||
The following modules are documented in this chapter:
|
||||
|
||||
|
|
|
|||
|
|
@ -107,7 +107,7 @@ The :mod:`EasyDialogs` module defines the following functions:
|
|||
*actionButtonLabel* is a string to show instead of "Open" in the OK button,
|
||||
*cancelButtonLabel* is a string to show instead of "Cancel" in the cancel
|
||||
button, *wanted* is the type of value wanted as a return: :class:`str`,
|
||||
:class:`unicode`, :class:`FSSpec`, :class:`FSRef` and subtypes thereof are
|
||||
:class:`FSSpec`, :class:`FSRef` and subtypes thereof are
|
||||
acceptable.
|
||||
|
||||
.. index:: single: Navigation Services
|
||||
|
|
|
|||
|
|
@ -242,6 +242,6 @@ new entries to the global character set, alias, and codec registries:
|
|||
Add a codec that map characters in the given character set to and from Unicode.
|
||||
|
||||
*charset* is the canonical name of a character set. *codecname* is the name of a
|
||||
Python codec, as appropriate for the second argument to the :func:`unicode`
|
||||
built-in, or to the :meth:`encode` method of a Unicode string.
|
||||
Python codec, as appropriate for the second argument to the :class:`str`'s
|
||||
:func:`decode` method
|
||||
|
||||
|
|
|
|||
|
|
@ -53,8 +53,8 @@ Here is the :class:`Header` class description:
|
|||
|
||||
Optional *s* is the initial header value. If ``None`` (the default), the
|
||||
initial header value is not set. You can later append to the header with
|
||||
:meth:`append` method calls. *s* may be a byte string or a Unicode string, but
|
||||
see the :meth:`append` documentation for semantics.
|
||||
:meth:`append` method calls. *s* may be an instance of :class:`bytes` or
|
||||
:class:`str`, but see the :meth:`append` documentation for semantics.
|
||||
|
||||
Optional *charset* serves two purposes: it has the same meaning as the *charset*
|
||||
argument to the :meth:`append` method. It also sets the default character set
|
||||
|
|
@ -86,19 +86,19 @@ Optional *errors* is passed straight through to the :meth:`append` method.
|
|||
a :class:`Charset` instance. A value of ``None`` (the default) means that the
|
||||
*charset* given in the constructor is used.
|
||||
|
||||
*s* may be a byte string or a Unicode string. If it is a byte string (i.e.
|
||||
``isinstance(s, str)`` is true), then *charset* is the encoding of that byte
|
||||
string, and a :exc:`UnicodeError` will be raised if the string cannot be decoded
|
||||
with that character set.
|
||||
*s* may be an instance of :class:`bytes` or :class:`str`. If it is an instance
|
||||
of :class:`bytes`, then *charset* is the encoding of that byte string, and a
|
||||
:exc:`UnicodeError` will be raised if the string cannot be decoded with that
|
||||
character set.
|
||||
|
||||
If *s* is a Unicode string, then *charset* is a hint specifying the character
|
||||
set of the characters in the string. In this case, when producing an
|
||||
If *s* is an instance of :class:`str`, then *charset* is a hint specifying the
|
||||
character set of the characters in the string. In this case, when producing an
|
||||
:rfc:`2822`\ -compliant header using :rfc:`2047` rules, the Unicode string will
|
||||
be encoded using the following charsets in order: ``us-ascii``, the *charset*
|
||||
hint, ``utf-8``. The first character set to not provoke a :exc:`UnicodeError`
|
||||
is used.
|
||||
|
||||
Optional *errors* is passed through to any :func:`unicode` or
|
||||
Optional *errors* is passed through to any :func:`encode` or
|
||||
:func:`ustr.encode` call, and defaults to "strict".
|
||||
|
||||
|
||||
|
|
@ -121,7 +121,7 @@ operators and built-in functions.
|
|||
|
||||
.. method:: Header.__unicode__()
|
||||
|
||||
A helper for the built-in :func:`unicode` function. Returns the header as a
|
||||
A helper for :class:`str`'s :func:`encode` method. Returns the header as a
|
||||
Unicode string.
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -130,10 +130,10 @@ There are several useful utilities provided in the :mod:`email.utils` module:
|
|||
When a header parameter is encoded in :rfc:`2231` format,
|
||||
:meth:`Message.get_param` may return a 3-tuple containing the character set,
|
||||
language, and value. :func:`collapse_rfc2231_value` turns this into a unicode
|
||||
string. Optional *errors* is passed to the *errors* argument of the built-in
|
||||
:func:`unicode` function; it defaults to ``replace``. Optional
|
||||
string. Optional *errors* is passed to the *errors* argument of :class:`str`'s
|
||||
:func:`encode` method; it defaults to ``'replace'``. Optional
|
||||
*fallback_charset* specifies the character set to use if the one in the
|
||||
:rfc:`2231` header is not known by Python; it defaults to ``us-ascii``.
|
||||
:rfc:`2231` header is not known by Python; it defaults to ``'us-ascii'``.
|
||||
|
||||
For convenience, if the *value* passed to :func:`collapse_rfc2231_value` is not
|
||||
a tuple, it should be a string and it is returned unquoted.
|
||||
|
|
|
|||
|
|
@ -47,7 +47,6 @@ The module defines the following functions:
|
|||
.. function:: ioctl(fd, op[, arg[, mutate_flag]])
|
||||
|
||||
This function is identical to the :func:`fcntl` function, except that the
|
||||
operations are typically defined in the library module :mod:`termios` and the
|
||||
argument handling is even more complicated.
|
||||
|
||||
The parameter *arg* can be one of an integer, absent (treated identically to the
|
||||
|
|
|
|||
|
|
@ -168,9 +168,3 @@ The two following opening hooks are provided by this module:
|
|||
|
||||
Usage example: ``fi =
|
||||
fileinput.FileInput(openhook=fileinput.hook_encoded("iso-8859-1"))``
|
||||
|
||||
.. note::
|
||||
|
||||
With this hook, :class:`FileInput` might return Unicode strings depending on the
|
||||
specified *encoding*.
|
||||
|
||||
|
|
|
|||
|
|
@ -129,7 +129,7 @@ available. They are listed here in alphabetical order.
|
|||
different ways:
|
||||
|
||||
* If it is a *string*, you must also give the *encoding* (and optionally,
|
||||
*errors*) parameters; :func:`bytearray` then converts the Unicode string to
|
||||
*errors*) parameters; :func:`bytearray` then converts the string to
|
||||
bytes using :meth:`str.encode`.
|
||||
|
||||
* If it is an *integer*, the array will have that size and will be
|
||||
|
|
@ -415,10 +415,9 @@ available. They are listed here in alphabetical order.
|
|||
.. warning::
|
||||
|
||||
The default *locals* act as described for function :func:`locals` below:
|
||||
modifications to the default *locals* dictionary should not be attempted. Pass
|
||||
an explicit *locals* dictionary if you need to see effects of the code on
|
||||
*locals* after function :func:`execfile` returns. :func:`exec` cannot be
|
||||
used reliably to modify a function's locals.
|
||||
modifications to the default *locals* dictionary should not be attempted.
|
||||
Pass an explicit *locals* dictionary if you need to see effects of the
|
||||
code on *locals* after function :func:`exec` returns.
|
||||
|
||||
|
||||
.. function:: filter(function, iterable)
|
||||
|
|
@ -805,16 +804,17 @@ available. They are listed here in alphabetical order.
|
|||
:mod:`fileinput`, :mod:`os`, :mod:`os.path`, :mod:`tempfile`, and
|
||||
:mod:`shutil`.
|
||||
|
||||
|
||||
.. XXX works for bytes too, but should it?
|
||||
.. function:: ord(c)
|
||||
|
||||
Given a string of length one, return an integer representing the Unicode code
|
||||
point of the character when the argument is a unicode object, or the value of
|
||||
the byte when the argument is an 8-bit string. For example, ``ord('a')`` returns
|
||||
the integer ``97``, ``ord(u'\u2020')`` returns ``8224``. This is the inverse of
|
||||
:func:`chr` for 8-bit strings and of :func:`unichr` for unicode objects. If a
|
||||
unicode argument is given and Python was built with UCS2 Unicode, then the
|
||||
character's code point must be in the range [0..65535] inclusive; otherwise the
|
||||
string length is two, and a :exc:`TypeError` will be raised.
|
||||
point of the character. For example, ``ord('a')`` returns the integer ``97``
|
||||
and ``ord('\u2020')`` returns ``8224``. This is the inverse of :func:`chr`.
|
||||
|
||||
If the argument length is not one, a :exc:`TypeError` will be raised. (If
|
||||
Python was built with UCS2 Unicode, then the character's code point must be
|
||||
in the range [0..65535] inclusive; otherwise the string length is two!)
|
||||
|
||||
|
||||
.. function:: pow(x, y[, z])
|
||||
|
|
@ -838,6 +838,22 @@ available. They are listed here in alphabetical order.
|
|||
accidents.)
|
||||
|
||||
|
||||
.. function:: print([object, ...][, sep=' '][, end='\n'][, file=sys.stdout])
|
||||
|
||||
Print *object*\(s) to the stream *file*, separated by *sep* and followed by
|
||||
*end*. *sep*, *end* and *file*, if present, must be given as keyword
|
||||
arguments.
|
||||
|
||||
All non-keyword arguments are converted to strings like :func:`str` does and
|
||||
written to the stream, separated by *sep* and followed by *end*. Both *sep*
|
||||
and *end* must be strings; they can also be ``None``, which means to use the
|
||||
default values. If no *object* is given, :func:`print` will just write
|
||||
*end*.
|
||||
|
||||
The *file* argument must be an object with a ``write(string)`` method; if it
|
||||
is not present or ``None``, :data:`sys.stdout` will be used.
|
||||
|
||||
|
||||
.. function:: property([fget[, fset[, fdel[, doc]]]])
|
||||
|
||||
Return a property attribute.
|
||||
|
|
|
|||
|
|
@ -136,9 +136,9 @@ The class-based API of the :mod:`gettext` module gives you more flexibility and
|
|||
greater convenience than the GNU :program:`gettext` API. It is the recommended
|
||||
way of localizing your Python applications and modules. :mod:`gettext` defines
|
||||
a "translations" class which implements the parsing of GNU :file:`.mo` format
|
||||
files, and has methods for returning either standard 8-bit strings or Unicode
|
||||
strings. Instances of this "translations" class can also install themselves in
|
||||
the built-in namespace as the function :func:`_`.
|
||||
files, and has methods for returning strings. Instances of this "translations"
|
||||
class can also install themselves in the built-in namespace as the function
|
||||
:func:`_`.
|
||||
|
||||
|
||||
.. function:: find(domain[, localedir[, languages[, all]]])
|
||||
|
|
@ -257,8 +257,7 @@ are the methods of :class:`NullTranslations`:
|
|||
.. method:: NullTranslations.ugettext(message)
|
||||
|
||||
If a fallback has been set, forward :meth:`ugettext` to the fallback. Otherwise,
|
||||
return the translated message as a Unicode string. Overridden in derived
|
||||
classes.
|
||||
return the translated message as a string. Overridden in derived classes.
|
||||
|
||||
|
||||
.. method:: NullTranslations.ngettext(singular, plural, n)
|
||||
|
|
@ -276,7 +275,7 @@ are the methods of :class:`NullTranslations`:
|
|||
.. method:: NullTranslations.ungettext(singular, plural, n)
|
||||
|
||||
If a fallback has been set, forward :meth:`ungettext` to the fallback.
|
||||
Otherwise, return the translated message as a Unicode string. Overridden in
|
||||
Otherwise, return the translated message as a string. Overridden in
|
||||
derived classes.
|
||||
|
||||
|
||||
|
|
@ -347,8 +346,8 @@ initialize the "protected" :attr:`_charset` instance variable, defaulting to
|
|||
``None`` if not found. If the charset encoding is specified, then all message
|
||||
ids and message strings read from the catalog are converted to Unicode using
|
||||
this encoding. The :meth:`ugettext` method always returns a Unicode, while the
|
||||
:meth:`gettext` returns an encoded 8-bit string. For the message id arguments
|
||||
of both methods, either Unicode strings or 8-bit strings containing only
|
||||
:meth:`gettext` returns an encoded bytestring. For the message id arguments
|
||||
of both methods, either Unicode strings or bytestrings containing only
|
||||
US-ASCII characters are acceptable. Note that the Unicode version of the
|
||||
methods (i.e. :meth:`ugettext` and :meth:`ungettext`) are the recommended
|
||||
interface to use for internationalized Python programs.
|
||||
|
|
@ -366,7 +365,7 @@ The following methods are overridden from the base class implementation:
|
|||
.. method:: GNUTranslations.gettext(message)
|
||||
|
||||
Look up the *message* id in the catalog and return the corresponding message
|
||||
string, as an 8-bit string encoded with the catalog's charset encoding, if
|
||||
string, as a bytestring encoded with the catalog's charset encoding, if
|
||||
known. If there is no entry in the catalog for the *message* id, and a fallback
|
||||
has been set, the look up is forwarded to the fallback's :meth:`gettext` method.
|
||||
Otherwise, the *message* id is returned.
|
||||
|
|
@ -382,7 +381,7 @@ The following methods are overridden from the base class implementation:
|
|||
.. method:: GNUTranslations.ugettext(message)
|
||||
|
||||
Look up the *message* id in the catalog and return the corresponding message
|
||||
string, as a Unicode string. If there is no entry in the catalog for the
|
||||
string, as a string. If there is no entry in the catalog for the
|
||||
*message* id, and a fallback has been set, the look up is forwarded to the
|
||||
fallback's :meth:`ugettext` method. Otherwise, the *message* id is returned.
|
||||
|
||||
|
|
@ -391,7 +390,7 @@ The following methods are overridden from the base class implementation:
|
|||
|
||||
Do a plural-forms lookup of a message id. *singular* is used as the message id
|
||||
for purposes of lookup in the catalog, while *n* is used to determine which
|
||||
plural form to use. The returned message string is an 8-bit string encoded with
|
||||
plural form to use. The returned message string is a bytestring encoded with
|
||||
the catalog's charset encoding, if known.
|
||||
|
||||
If the message id is not found in the catalog, and a fallback is specified, the
|
||||
|
|
@ -410,7 +409,7 @@ The following methods are overridden from the base class implementation:
|
|||
|
||||
Do a plural-forms lookup of a message id. *singular* is used as the message id
|
||||
for purposes of lookup in the catalog, while *n* is used to determine which
|
||||
plural form to use. The returned message string is a Unicode string.
|
||||
plural form to use. The returned message string is a string.
|
||||
|
||||
If the message id is not found in the catalog, and a fallback is specified, the
|
||||
request is forwarded to the fallback's :meth:`ungettext` method. Otherwise,
|
||||
|
|
|
|||
|
|
@ -185,6 +185,19 @@ This module provides an interface to the mechanisms used to implement the
|
|||
continue to use the old class definition. The same is true for derived classes.
|
||||
|
||||
|
||||
.. function:: acquire_lock()
|
||||
|
||||
Acquires the interpreter's import lock for the current thread. This lock should
|
||||
be used by import hooks to ensure thread-safety when importing modules. On
|
||||
platforms without threads, this function does nothing.
|
||||
|
||||
|
||||
.. function:: release_lock()
|
||||
|
||||
Release the interpreter's import lock. On platforms without threads, this
|
||||
function does nothing.
|
||||
|
||||
|
||||
The following constants with integer values, defined in this module, are used to
|
||||
indicate the search result of :func:`find_module`.
|
||||
|
||||
|
|
|
|||
|
|
@ -177,7 +177,8 @@ loops that truncate the stream.
|
|||
|
||||
Make an iterator that filters elements from iterable returning only those for
|
||||
which the predicate is ``True``. If *predicate* is ``None``, return the items
|
||||
that are true. Equivalent to::
|
||||
that are true. This function is the same as the built-in :func:`filter`
|
||||
function. Equivalent to::
|
||||
|
||||
def ifilter(predicate, iterable):
|
||||
if predicate is None:
|
||||
|
|
@ -204,7 +205,8 @@ loops that truncate the stream.
|
|||
.. function:: imap(function, *iterables)
|
||||
|
||||
Make an iterator that computes the function using arguments from each of the
|
||||
iterables. Equivalent to::
|
||||
iterables. This function is the same as the built-in :func:`map` function.
|
||||
Equivalent to::
|
||||
|
||||
def imap(function, *iterables):
|
||||
iterables = [iter(it) for it in iterables)
|
||||
|
|
@ -230,7 +232,7 @@ loops that truncate the stream.
|
|||
|
||||
def islice(iterable, *args):
|
||||
s = slice(*args)
|
||||
it = iter(range(s.start or 0, s.stop or sys.maxsize, s.step or 1))
|
||||
it = range(s.start or 0, s.stop or sys.maxsize, s.step or 1)
|
||||
nexti = next(it)
|
||||
for i, element in enumerate(iterable):
|
||||
if i == nexti:
|
||||
|
|
|
|||
|
|
@ -95,7 +95,7 @@ yourself, though, it is simpler to use a :class:`RotatingFileHandler`::
|
|||
logfiles = glob.glob('%s*' % LOG_FILENAME)
|
||||
|
||||
for filename in logfiles:
|
||||
print filename
|
||||
print(filename)
|
||||
|
||||
The result should be 6 separate files, each with part of the log history for the
|
||||
application::
|
||||
|
|
@ -2428,13 +2428,13 @@ configuration::
|
|||
HOST = 'localhost'
|
||||
PORT = 9999
|
||||
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||
print "connecting..."
|
||||
print("connecting...")
|
||||
s.connect((HOST, PORT))
|
||||
print "sending config..."
|
||||
print("sending config...")
|
||||
s.send(struct.pack(">L", len(data_to_send)))
|
||||
s.send(data_to_send)
|
||||
s.close()
|
||||
print "complete"
|
||||
print("complete")
|
||||
|
||||
|
||||
More examples
|
||||
|
|
|
|||
|
|
@ -1643,7 +1643,7 @@ due to malformed messages in the mailbox::
|
|||
|
||||
list_names = ('python-list', 'python-dev', 'python-bugs')
|
||||
|
||||
boxes = dict((name, mailbox.mbox('~/email/%s' % name)) for name in list_names)
|
||||
boxes = {name: mailbox.mbox('~/email/%s' % name) for name in list_names}
|
||||
inbox = mailbox.Maildir('~/Maildir', factory=None)
|
||||
|
||||
for key in inbox.iterkeys():
|
||||
|
|
|
|||
|
|
@ -38,7 +38,7 @@ supports a substantially wider range of objects than marshal.
|
|||
Not all Python object types are supported; in general, only objects whose value
|
||||
is independent from a particular invocation of Python can be written and read by
|
||||
this module. The following types are supported: ``None``, integers,
|
||||
floating point numbers, strings, Unicode objects, tuples, lists, sets,
|
||||
floating point numbers, strings, bytes, bytearrays, tuples, lists, sets,
|
||||
dictionaries, and code objects, where it should be understood that tuples, lists
|
||||
and dictionaries are only supported as long as the values contained therein are
|
||||
themselves supported; and recursive lists and dictionaries should not be written
|
||||
|
|
|
|||
|
|
@ -93,13 +93,6 @@ The mathematical and bitwise operations are the most numerous:
|
|||
Return the bitwise and of *a* and *b*.
|
||||
|
||||
|
||||
.. function:: div(a, b)
|
||||
__div__(a, b)
|
||||
|
||||
Return ``a / b`` when ``__future__.division`` is not in effect. This is
|
||||
also known as "classic" division.
|
||||
|
||||
|
||||
.. function:: floordiv(a, b)
|
||||
__floordiv__(a, b)
|
||||
|
||||
|
|
@ -171,8 +164,8 @@ The mathematical and bitwise operations are the most numerous:
|
|||
.. function:: truediv(a, b)
|
||||
__truediv__(a, b)
|
||||
|
||||
Return ``a / b`` when ``__future__.division`` is in effect. This is also
|
||||
known as "true" division.
|
||||
Return ``a / b`` where 2/3 is .66 rather than 0. This is also known as
|
||||
"true" division.
|
||||
|
||||
|
||||
.. function:: xor(a, b)
|
||||
|
|
@ -241,14 +234,6 @@ Operations which work with sequences include:
|
|||
Return ``a * b`` where *a* is a sequence and *b* is an integer.
|
||||
|
||||
|
||||
.. function:: sequenceIncludes(...)
|
||||
|
||||
.. deprecated:: 2.0
|
||||
Use :func:`contains` instead.
|
||||
|
||||
Alias for :func:`contains`.
|
||||
|
||||
|
||||
.. function:: setitem(a, b, c)
|
||||
__setitem__(a, b, c)
|
||||
|
||||
|
|
@ -260,6 +245,7 @@ Operations which work with sequences include:
|
|||
|
||||
Set the slice of *a* from index *b* to index *c-1* to the sequence *v*.
|
||||
|
||||
|
||||
Many operations have an "in-place" version. The following functions provide a
|
||||
more primitive access to in-place operators than the usual syntax does; for
|
||||
example, the :term:`statement` ``x += y`` is equivalent to
|
||||
|
|
@ -285,13 +271,6 @@ example, the :term:`statement` ``x += y`` is equivalent to
|
|||
``a = iconcat(a, b)`` is equivalent to ``a += b`` for *a* and *b* sequences.
|
||||
|
||||
|
||||
.. function:: idiv(a, b)
|
||||
__idiv__(a, b)
|
||||
|
||||
``a = idiv(a, b)`` is equivalent to ``a /= b`` when ``__future__.division`` is
|
||||
not in effect.
|
||||
|
||||
|
||||
.. function:: ifloordiv(a, b)
|
||||
__ifloordiv__(a, b)
|
||||
|
||||
|
|
@ -350,8 +329,7 @@ example, the :term:`statement` ``x += y`` is equivalent to
|
|||
.. function:: itruediv(a, b)
|
||||
__itruediv__(a, b)
|
||||
|
||||
``a = itruediv(a, b)`` is equivalent to ``a /= b`` when ``__future__.division``
|
||||
is in effect.
|
||||
``a = itruediv(a, b)`` is equivalent to ``a /= b``.
|
||||
|
||||
|
||||
.. function:: ixor(a, b)
|
||||
|
|
@ -363,10 +341,11 @@ example, the :term:`statement` ``x += y`` is equivalent to
|
|||
The :mod:`operator` module also defines a few predicates to test the type of
|
||||
objects.
|
||||
|
||||
.. XXX just remove them?
|
||||
.. note::
|
||||
|
||||
Be careful not to misinterpret the results of these functions; only
|
||||
:func:`isCallable` has any measure of reliability with instance objects.
|
||||
Be careful not to misinterpret the results of these functions; none have any
|
||||
measure of reliability with instance objects.
|
||||
For example::
|
||||
|
||||
>>> class C:
|
||||
|
|
@ -379,21 +358,10 @@ objects.
|
|||
|
||||
.. note::
|
||||
|
||||
Python 3 is expected to introduce abstract base classes for
|
||||
collection types, so it should be possible to write, for example,
|
||||
``isinstance(obj, collections.Mapping)`` and ``isinstance(obj,
|
||||
Since there are now abstract classes for collection types, you should write,
|
||||
for example, ``isinstance(obj, collections.Mapping)`` and ``isinstance(obj,
|
||||
collections.Sequence)``.
|
||||
|
||||
.. function:: isCallable(obj)
|
||||
|
||||
.. deprecated:: 2.0
|
||||
Use the :func:`callable` built-in function instead.
|
||||
|
||||
Returns true if the object *obj* can be called like a function, otherwise it
|
||||
returns false. True is returned for functions, instance methods, class
|
||||
objects, and instance objects which support the :meth:`__call__` method.
|
||||
|
||||
|
||||
.. function:: isMappingType(obj)
|
||||
|
||||
Returns true if the object *obj* supports the mapping interface. This is true for
|
||||
|
|
@ -492,11 +460,7 @@ Python syntax and the functions in the :mod:`operator` module.
|
|||
+-----------------------+-------------------------+---------------------------------+
|
||||
| Containment Test | ``obj in seq`` | ``contains(seq, obj)`` |
|
||||
+-----------------------+-------------------------+---------------------------------+
|
||||
| Division | ``a / b`` | ``div(a, b)`` (without |
|
||||
| | | ``__future__.division``) |
|
||||
+-----------------------+-------------------------+---------------------------------+
|
||||
| Division | ``a / b`` | ``truediv(a, b)`` (with |
|
||||
| | | ``__future__.division``) |
|
||||
| Division | ``a / b`` | ``truediv(a, b)`` |
|
||||
+-----------------------+-------------------------+---------------------------------+
|
||||
| Division | ``a // b`` | ``floordiv(a, b)`` |
|
||||
+-----------------------+-------------------------+---------------------------------+
|
||||
|
|
|
|||
|
|
@ -288,5 +288,5 @@ write files see :func:`open`, and for accessing the filesystem see the
|
|||
.. data:: supports_unicode_filenames
|
||||
|
||||
True if arbitrary Unicode strings can be used as file names (within limitations
|
||||
imposed by the file system), and if :func:`os.listdir` returns Unicode strings
|
||||
for a Unicode argument.
|
||||
imposed by the file system), and if :func:`os.listdir` returns strings that
|
||||
contain characters that cannot be represented by ASCII.
|
||||
|
|
|
|||
|
|
@ -701,13 +701,13 @@ Files and Directories
|
|||
|
||||
.. function:: getcwd()
|
||||
|
||||
Return a string representing the current working directory. Availability:
|
||||
Macintosh, Unix, Windows.
|
||||
Return a bytestring representing the current working directory.
|
||||
Availability: Macintosh, Unix, Windows.
|
||||
|
||||
|
||||
.. function:: getcwdu()
|
||||
|
||||
Return a Unicode object representing the current working directory.
|
||||
Return a string representing the current working directory.
|
||||
Availability: Macintosh, Unix, Windows.
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -327,7 +327,7 @@ The following types can be pickled:
|
|||
|
||||
* integers, floating point numbers, complex numbers
|
||||
|
||||
* normal and Unicode strings
|
||||
* strings, bytes, bytearrays
|
||||
|
||||
* tuples, lists, sets, and dictionaries containing only picklable objects
|
||||
|
||||
|
|
@ -659,7 +659,7 @@ that a self-referencing list is pickled and restored correctly. ::
|
|||
import pickle
|
||||
|
||||
data1 = {'a': [1, 2.0, 3, 4+6j],
|
||||
'b': ('string', u'Unicode string'),
|
||||
'b': ("string", "string using Unicode features \u0394"),
|
||||
'c': None}
|
||||
|
||||
selfref_list = [1, 2, 3]
|
||||
|
|
|
|||
|
|
@ -34,8 +34,7 @@ This module provides a single function:
|
|||
returned. Items are only appended to the copy at the end.
|
||||
|
||||
It is assumed that ``sys.path`` is a sequence. Items of ``sys.path`` that are
|
||||
not (Unicode or 8-bit) strings referring to existing directories are ignored.
|
||||
Unicode items on ``sys.path`` that cause errors when used as filenames may cause
|
||||
this function to raise an exception (in line with :func:`os.path.isdir`
|
||||
behavior).
|
||||
not strings referring to existing directories are ignored. Unicode items on
|
||||
``sys.path`` that cause errors when used as filenames may cause this function
|
||||
to raise an exception (in line with :func:`os.path.isdir` behavior).
|
||||
|
||||
|
|
|
|||
|
|
@ -1153,7 +1153,7 @@ in some text, he or she would use :func:`finditer` in the following manner::
|
|||
|
||||
>>> text = "He was carefully disguised but captured quickly by police."
|
||||
>>> for m in re.finditer(r"\w+ly", text):
|
||||
print '%02d-%02d: %s' % (m.start(), m.end(), m.group(0))
|
||||
print('%02d-%02d: %s' % (m.start(), m.end(), m.group(0)))
|
||||
07-16: carefully
|
||||
40-47: quickly
|
||||
|
||||
|
|
|
|||
|
|
@ -149,7 +149,7 @@ server::
|
|||
s = xmlrpclib.ServerProxy('http://localhost:8000')
|
||||
print(s.pow(2,3)) # Returns 2**3 = 8
|
||||
print(s.add(2,3)) # Returns 5
|
||||
print(s.div(5,2)) # Returns 5//2 = 2
|
||||
print(s.mul(5,2)) # Returns 5*2 = 10
|
||||
|
||||
# Print list of available methods
|
||||
print(s.system.listMethods())
|
||||
|
|
|
|||
|
|
@ -184,7 +184,7 @@ Module functions and constants
|
|||
Registers a callable to convert the custom Python type *type* into one of
|
||||
SQLite's supported types. The callable *callable* accepts as single parameter
|
||||
the Python value, and must return a value of the following types: int,
|
||||
float, str (UTF-8 encoded), unicode or buffer.
|
||||
float, str, bytes (UTF-8 encoded) or buffer.
|
||||
|
||||
|
||||
.. function:: complete_statement(sql)
|
||||
|
|
@ -258,7 +258,7 @@ A :class:`Connection` instance has the following attributes and methods:
|
|||
parameters the function accepts, and *func* is a Python callable that is called
|
||||
as the SQL function.
|
||||
|
||||
The function can return any of the types supported by SQLite: unicode, str, int,
|
||||
The function can return any of the types supported by SQLite: bytes, str, int,
|
||||
float, buffer and None.
|
||||
|
||||
Example:
|
||||
|
|
@ -275,7 +275,7 @@ A :class:`Connection` instance has the following attributes and methods:
|
|||
final result of the aggregate.
|
||||
|
||||
The ``finalize`` method can return any of the types supported by SQLite:
|
||||
unicode, str, int, float, buffer and None.
|
||||
bytes, str, int, float, buffer and None.
|
||||
|
||||
Example:
|
||||
|
||||
|
|
@ -354,13 +354,13 @@ A :class:`Connection` instance has the following attributes and methods:
|
|||
.. attribute:: Connection.text_factory
|
||||
|
||||
Using this attribute you can control what objects are returned for the TEXT data
|
||||
type. By default, this attribute is set to :class:`unicode` and the
|
||||
:mod:`sqlite3` module will return Unicode objects for TEXT. If you want to
|
||||
return bytestrings instead, you can set it to :class:`str`.
|
||||
type. By default, this attribute is set to :class:`str` and the
|
||||
:mod:`sqlite3` module will return strings for TEXT. If you want to
|
||||
return bytestrings instead, you can set it to :class:`bytes`.
|
||||
|
||||
For efficiency reasons, there's also a way to return Unicode objects only for
|
||||
non-ASCII data, and bytestrings otherwise. To activate it, set this attribute to
|
||||
:const:`sqlite3.OptimizedUnicode`.
|
||||
For efficiency reasons, there's also a way to return :class:`str` objects
|
||||
only for non-ASCII data, and :class:`bytes` otherwise. To activate it, set
|
||||
this attribute to :const:`sqlite3.OptimizedUnicode`.
|
||||
|
||||
You can also set it to any other callable that accepts a single bytestring
|
||||
parameter and returns the resulting object.
|
||||
|
|
@ -424,7 +424,7 @@ A :class:`Cursor` instance has the following attributes and methods:
|
|||
at once. It issues a COMMIT statement first, then executes the SQL script it
|
||||
gets as a parameter.
|
||||
|
||||
*sql_script* can be a bytestring or a Unicode string.
|
||||
*sql_script* can be an instance of :class:`str` or :class:`bytes`.
|
||||
|
||||
Example:
|
||||
|
||||
|
|
@ -493,21 +493,21 @@ SQLite natively supports the following types: NULL, INTEGER, REAL, TEXT, BLOB.
|
|||
|
||||
The following Python types can thus be sent to SQLite without any problem:
|
||||
|
||||
+------------------------+-------------+
|
||||
+-------------------------------+-------------+
|
||||
| Python type | SQLite type |
|
||||
+========================+=============+
|
||||
+===============================+=============+
|
||||
| ``None`` | NULL |
|
||||
+------------------------+-------------+
|
||||
| ``int`` | INTEGER |
|
||||
+------------------------+-------------+
|
||||
| ``float`` | REAL |
|
||||
+------------------------+-------------+
|
||||
| ``str (UTF8-encoded)`` | TEXT |
|
||||
+------------------------+-------------+
|
||||
| ``unicode`` | TEXT |
|
||||
+------------------------+-------------+
|
||||
| ``buffer`` | BLOB |
|
||||
+------------------------+-------------+
|
||||
+-------------------------------+-------------+
|
||||
| :class:`int` | INTEGER |
|
||||
+-------------------------------+-------------+
|
||||
| :class:`float` | REAL |
|
||||
+-------------------------------+-------------+
|
||||
| :class:`bytes` (UTF8-encoded) | TEXT |
|
||||
+-------------------------------+-------------+
|
||||
| :class:`str` | TEXT |
|
||||
+-------------------------------+-------------+
|
||||
| :class:`buffer` | BLOB |
|
||||
+-------------------------------+-------------+
|
||||
|
||||
This is how SQLite types are converted to Python types by default:
|
||||
|
||||
|
|
@ -520,7 +520,7 @@ This is how SQLite types are converted to Python types by default:
|
|||
+-------------+---------------------------------------------+
|
||||
| ``REAL`` | float |
|
||||
+-------------+---------------------------------------------+
|
||||
| ``TEXT`` | depends on text_factory, unicode by default |
|
||||
| ``TEXT`` | depends on text_factory, str by default |
|
||||
+-------------+---------------------------------------------+
|
||||
| ``BLOB`` | buffer |
|
||||
+-------------+---------------------------------------------+
|
||||
|
|
@ -537,7 +537,7 @@ Using adapters to store additional Python types in SQLite databases
|
|||
As described before, SQLite supports only a limited set of types natively. To
|
||||
use other Python types with SQLite, you must **adapt** them to one of the
|
||||
sqlite3 module's supported types for SQLite: one of NoneType, int, float,
|
||||
str, unicode, buffer.
|
||||
str, bytes, buffer.
|
||||
|
||||
The :mod:`sqlite3` module uses Python object adaptation, as described in
|
||||
:pep:`246` for this. The protocol to use is :class:`PrepareProtocol`.
|
||||
|
|
|
|||
|
|
@ -2070,13 +2070,13 @@ the particular object.
|
|||
.. XXX does this still apply?
|
||||
.. attribute:: file.encoding
|
||||
|
||||
The encoding that this file uses. When Unicode strings are written to a file,
|
||||
The encoding that this file uses. When strings are written to a file,
|
||||
they will be converted to byte strings using this encoding. In addition, when
|
||||
the file is connected to a terminal, the attribute gives the encoding that the
|
||||
terminal is likely to use (that information might be incorrect if the user has
|
||||
misconfigured the terminal). The attribute is read-only and may not be present
|
||||
on all file-like objects. It may also be ``None``, in which case the file uses
|
||||
the system default encoding for converting Unicode strings.
|
||||
the system default encoding for converting strings.
|
||||
|
||||
|
||||
.. attribute:: file.mode
|
||||
|
|
|
|||
|
|
@ -538,7 +538,7 @@ rule:
|
|||
String functions
|
||||
----------------
|
||||
|
||||
The following functions are available to operate on string and Unicode objects.
|
||||
The following functions are available to operate on string objects.
|
||||
They are not available as string methods.
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -1,3 +1,4 @@
|
|||
.. XXX this whole file is outdated
|
||||
|
||||
:mod:`StringIO` --- Read and write strings as files
|
||||
===================================================
|
||||
|
|
@ -9,7 +10,7 @@
|
|||
This module implements a file-like class, :class:`StringIO`, that reads and
|
||||
writes a string buffer (also known as *memory files*). See the description of
|
||||
file objects for operations (section :ref:`bltin-file-objects`). (For
|
||||
standard strings, see :class:`str` and :class:`unicode`.)
|
||||
standard strings, see :class:`str`.)
|
||||
|
||||
|
||||
.. class:: StringIO([buffer])
|
||||
|
|
@ -19,20 +20,13 @@ standard strings, see :class:`str` and :class:`unicode`.)
|
|||
:class:`StringIO` will start empty. In both cases, the initial file position
|
||||
starts at zero.
|
||||
|
||||
The :class:`StringIO` object can accept either Unicode or 8-bit strings, but
|
||||
mixing the two may take some care. If both are used, 8-bit strings that cannot
|
||||
be interpreted as 7-bit ASCII (that use the 8th bit) will cause a
|
||||
:exc:`UnicodeError` to be raised when :meth:`getvalue` is called.
|
||||
|
||||
The following methods of :class:`StringIO` objects require special mention:
|
||||
|
||||
|
||||
.. method:: StringIO.getvalue()
|
||||
|
||||
Retrieve the entire contents of the "file" at any time before the
|
||||
:class:`StringIO` object's :meth:`close` method is called. See the note above
|
||||
for information about mixing Unicode and 8-bit strings; such mixing can cause
|
||||
this method to raise :exc:`UnicodeError`.
|
||||
:class:`StringIO` object's :meth:`close` method is called.
|
||||
|
||||
|
||||
.. method:: StringIO.close()
|
||||
|
|
@ -75,11 +69,11 @@ types, there's no way to build your own version using subclassing. Use the
|
|||
original :mod:`StringIO` module in that case.
|
||||
|
||||
Unlike the memory files implemented by the :mod:`StringIO` module, those
|
||||
provided by this module are not able to accept Unicode strings that cannot be
|
||||
encoded as plain ASCII strings.
|
||||
provided by this module are not able to accept strings that cannot be
|
||||
encoded in plain ASCII.
|
||||
|
||||
Calling :func:`StringIO` with a Unicode string parameter populates
|
||||
the object with the buffer representation of the Unicode string, instead of
|
||||
Calling :func:`StringIO` with a string parameter populates
|
||||
the object with the buffer representation of the string, instead of
|
||||
encoding the string.
|
||||
|
||||
Another difference from the :mod:`StringIO` module is that calling
|
||||
|
|
|
|||
|
|
@ -42,7 +42,7 @@ The module defines the following functions:
|
|||
|
||||
.. function:: print_exc([limit[, file]])
|
||||
|
||||
This is a shorthand for ``print_exception(*sys.exc_info()``.
|
||||
This is a shorthand for ``print_exception(*sys.exc_info())``.
|
||||
|
||||
|
||||
.. function:: format_exc([limit])
|
||||
|
|
@ -172,26 +172,26 @@ exception and traceback::
|
|||
lumberjack()
|
||||
except:
|
||||
exceptionType, exceptionValue, exceptionTraceback = sys.exc_info()
|
||||
print "*** print_tb:"
|
||||
print("*** print_tb:")
|
||||
traceback.print_tb(exceptionTraceback, limit=1, file=sys.stdout)
|
||||
print "*** print_exception:"
|
||||
print("*** print_exception:")
|
||||
traceback.print_exception(exceptionType, exceptionValue, exceptionTraceback,
|
||||
limit=2, file=sys.stdout)
|
||||
print "*** print_exc:"
|
||||
print("*** print_exc:")
|
||||
traceback.print_exc()
|
||||
print "*** format_exc, first and last line:"
|
||||
print("*** format_exc, first and last line:")
|
||||
formatted_lines = traceback.format_exc().splitlines()
|
||||
print formatted_lines[0]
|
||||
print formatted_lines[-1]
|
||||
print "*** format_exception:"
|
||||
print repr(traceback.format_exception(exceptionType, exceptionValue,
|
||||
exceptionTraceback))
|
||||
print "*** extract_tb:"
|
||||
print repr(traceback.extract_tb(exceptionTraceback))
|
||||
print "*** format_tb:"
|
||||
print repr(traceback.format_tb(exceptionTraceback))
|
||||
print "*** tb_lineno:", traceback.tb_lineno(exceptionTraceback)
|
||||
print "*** print_last:"
|
||||
print(formatted_lines[0])
|
||||
print(formatted_lines[-1])
|
||||
print("*** format_exception:")
|
||||
print(repr(traceback.format_exception(exceptionType, exceptionValue,
|
||||
exceptionTraceback)))
|
||||
print("*** extract_tb:")
|
||||
print(repr(traceback.extract_tb(exceptionTraceback)))
|
||||
print("*** format_tb:")
|
||||
print(repr(traceback.format_tb(exceptionTraceback)))
|
||||
print("*** tb_lineno:", traceback.tb_lineno(exceptionTraceback))
|
||||
print("*** print_last:")
|
||||
traceback.print_last()
|
||||
|
||||
|
||||
|
|
@ -249,8 +249,8 @@ The following example shows the different ways to print and format the stack::
|
|||
...
|
||||
>>> def lumberstack():
|
||||
... traceback.print_stack()
|
||||
... print repr(traceback.extract_stack())
|
||||
... print repr(traceback.format_stack())
|
||||
... print(repr(traceback.extract_stack()))
|
||||
... print(repr(traceback.format_stack()))
|
||||
...
|
||||
>>> another_function()
|
||||
File "<doctest>", line 10, in <module>
|
||||
|
|
@ -261,10 +261,10 @@ The following example shows the different ways to print and format the stack::
|
|||
traceback.print_stack()
|
||||
[('<doctest>', 10, '<module>', 'another_function()'),
|
||||
('<doctest>', 3, 'another_function', 'lumberstack()'),
|
||||
('<doctest>', 7, 'lumberstack', 'print repr(traceback.extract_stack())')]
|
||||
('<doctest>', 7, 'lumberstack', 'print(repr(traceback.extract_stack()))')]
|
||||
[' File "<doctest>", line 10, in <module>\n another_function()\n',
|
||||
' File "<doctest>", line 3, in another_function\n lumberstack()\n',
|
||||
' File "<doctest>", line 8, in lumberstack\n print repr(traceback.format_stack())\n']
|
||||
' File "<doctest>", line 8, in lumberstack\n print(repr(traceback.format_stack()))\n']
|
||||
|
||||
|
||||
This last example demonstrates the final few formatting functions::
|
||||
|
|
|
|||
|
|
@ -61,18 +61,6 @@ Undocumented Mac OS modules
|
|||
:synopsis: Rudimentary decoder for AppleSingle format files.
|
||||
|
||||
|
||||
|
||||
:mod:`buildtools` --- Helper module for BuildApplet and Friends
|
||||
---------------------------------------------------------------
|
||||
|
||||
.. module:: buildtools
|
||||
:platform: Mac
|
||||
:synopsis: Helper module for BuildApplet, BuildApplication and macfreeze.
|
||||
|
||||
|
||||
.. deprecated:: 2.4
|
||||
|
||||
|
||||
:mod:`icopen` --- Internet Config replacement for :meth:`open`
|
||||
--------------------------------------------------------------
|
||||
|
||||
|
|
|
|||
|
|
@ -27,72 +27,72 @@ the following functions:
|
|||
.. function:: lookup(name)
|
||||
|
||||
Look up character by name. If a character with the given name is found, return
|
||||
the corresponding Unicode character. If not found, :exc:`KeyError` is raised.
|
||||
the corresponding character. If not found, :exc:`KeyError` is raised.
|
||||
|
||||
|
||||
.. function:: name(unichr[, default])
|
||||
.. function:: name(chr[, default])
|
||||
|
||||
Returns the name assigned to the Unicode character *unichr* as a string. If no
|
||||
Returns the name assigned to the character *chr* as a string. If no
|
||||
name is defined, *default* is returned, or, if not given, :exc:`ValueError` is
|
||||
raised.
|
||||
|
||||
|
||||
.. function:: decimal(unichr[, default])
|
||||
.. function:: decimal(chr[, default])
|
||||
|
||||
Returns the decimal value assigned to the Unicode character *unichr* as integer.
|
||||
Returns the decimal value assigned to the character *chr* as integer.
|
||||
If no such value is defined, *default* is returned, or, if not given,
|
||||
:exc:`ValueError` is raised.
|
||||
|
||||
|
||||
.. function:: digit(unichr[, default])
|
||||
.. function:: digit(chr[, default])
|
||||
|
||||
Returns the digit value assigned to the Unicode character *unichr* as integer.
|
||||
Returns the digit value assigned to the character *chr* as integer.
|
||||
If no such value is defined, *default* is returned, or, if not given,
|
||||
:exc:`ValueError` is raised.
|
||||
|
||||
|
||||
.. function:: numeric(unichr[, default])
|
||||
.. function:: numeric(chr[, default])
|
||||
|
||||
Returns the numeric value assigned to the Unicode character *unichr* as float.
|
||||
Returns the numeric value assigned to the character *chr* as float.
|
||||
If no such value is defined, *default* is returned, or, if not given,
|
||||
:exc:`ValueError` is raised.
|
||||
|
||||
|
||||
.. function:: category(unichr)
|
||||
.. function:: category(chr)
|
||||
|
||||
Returns the general category assigned to the Unicode character *unichr* as
|
||||
Returns the general category assigned to the character *chr* as
|
||||
string.
|
||||
|
||||
|
||||
.. function:: bidirectional(unichr)
|
||||
.. function:: bidirectional(chr)
|
||||
|
||||
Returns the bidirectional category assigned to the Unicode character *unichr* as
|
||||
Returns the bidirectional category assigned to the character *chr* as
|
||||
string. If no such value is defined, an empty string is returned.
|
||||
|
||||
|
||||
.. function:: combining(unichr)
|
||||
.. function:: combining(chr)
|
||||
|
||||
Returns the canonical combining class assigned to the Unicode character *unichr*
|
||||
Returns the canonical combining class assigned to the character *chr*
|
||||
as integer. Returns ``0`` if no combining class is defined.
|
||||
|
||||
|
||||
.. function:: east_asian_width(unichr)
|
||||
.. function:: east_asian_width(chr)
|
||||
|
||||
Returns the east asian width assigned to the Unicode character *unichr* as
|
||||
Returns the east asian width assigned to the character *chr* as
|
||||
string.
|
||||
|
||||
|
||||
.. function:: mirrored(unichr)
|
||||
.. function:: mirrored(chr)
|
||||
|
||||
Returns the mirrored property assigned to the Unicode character *unichr* as
|
||||
Returns the mirrored property assigned to the character *chr* as
|
||||
integer. Returns ``1`` if the character has been identified as a "mirrored"
|
||||
character in bidirectional text, ``0`` otherwise.
|
||||
|
||||
|
||||
.. function:: decomposition(unichr)
|
||||
.. function:: decomposition(chr)
|
||||
|
||||
Returns the character decomposition mapping assigned to the Unicode character
|
||||
*unichr* as string. An empty string is returned in case no such mapping is
|
||||
Returns the character decomposition mapping assigned to the character
|
||||
*chr* as string. An empty string is returned in case no such mapping is
|
||||
defined.
|
||||
|
||||
|
||||
|
|
@ -146,16 +146,16 @@ Examples::
|
|||
|
||||
>>> unicodedata.lookup('LEFT CURLY BRACKET')
|
||||
u'{'
|
||||
>>> unicodedata.name(u'/')
|
||||
>>> unicodedata.name('/')
|
||||
'SOLIDUS'
|
||||
>>> unicodedata.decimal(u'9')
|
||||
>>> unicodedata.decimal('9')
|
||||
9
|
||||
>>> unicodedata.decimal(u'a')
|
||||
>>> unicodedata.decimal('a')
|
||||
Traceback (most recent call last):
|
||||
File "<stdin>", line 1, in ?
|
||||
ValueError: not a decimal
|
||||
>>> unicodedata.category(u'A') # 'L'etter, 'u'ppercase
|
||||
>>> unicodedata.category('A') # 'L'etter, 'u'ppercase
|
||||
'Lu'
|
||||
>>> unicodedata.bidirectional(u'\u0660') # 'A'rabic, 'N'umber
|
||||
>>> unicodedata.bidirectional('\u0660') # 'A'rabic, 'N'umber
|
||||
'AN'
|
||||
|
||||
|
|
|
|||
|
|
@ -146,7 +146,7 @@ them and override existing methods or add new ones. In this way one can add new
|
|||
behaviors to strings.
|
||||
|
||||
It should be noted that these classes are highly inefficient compared to real
|
||||
string or Unicode objects; this is especially the case for
|
||||
string or bytes objects; this is especially the case for
|
||||
:class:`MutableString`.
|
||||
|
||||
The :mod:`UserString` module defines the following classes:
|
||||
|
|
@ -158,9 +158,9 @@ The :mod:`UserString` module defines the following classes:
|
|||
content is kept in a regular string or Unicode string object, which is
|
||||
accessible via the :attr:`data` attribute of :class:`UserString` instances. The
|
||||
instance's contents are initially set to a copy of *sequence*. *sequence* can
|
||||
be either a regular Python string or Unicode string, an instance of
|
||||
:class:`UserString` (or a subclass) or an arbitrary sequence which can be
|
||||
converted into a string using the built-in :func:`str` function.
|
||||
be an instance of :class:`bytes`, :class:`str`, :class:`UserString` (or a
|
||||
subclass) or an arbitrary sequence which can be converted into a string using
|
||||
the built-in :func:`str` function.
|
||||
|
||||
|
||||
.. class:: MutableString([sequence])
|
||||
|
|
@ -173,13 +173,13 @@ The :mod:`UserString` module defines the following classes:
|
|||
mutable object as dictionary key, which would be otherwise very error prone and
|
||||
hard to track down.
|
||||
|
||||
In addition to supporting the methods and operations of string and Unicode
|
||||
In addition to supporting the methods and operations of bytes and string
|
||||
objects (see section :ref:`string-methods`), :class:`UserString` instances
|
||||
provide the following attribute:
|
||||
|
||||
|
||||
.. attribute:: MutableString.data
|
||||
|
||||
A real Python string or Unicode object used to store the content of the
|
||||
A real Python string or bytes object used to store the content of the
|
||||
:class:`UserString` class.
|
||||
|
||||
|
|
|
|||
|
|
@ -169,7 +169,7 @@ also provides these miscellaneous utilities:
|
|||
wrapper = FileWrapper(filelike, blksize=5)
|
||||
|
||||
for chunk in wrapper:
|
||||
print chunk
|
||||
print(chunk)
|
||||
|
||||
|
||||
|
||||
|
|
@ -428,7 +428,7 @@ Paste" library.
|
|||
validator_app = validator(simple_app)
|
||||
|
||||
httpd = make_server('', 8000, validator_app)
|
||||
print "Listening on port 8000...."
|
||||
print("Listening on port 8000....")
|
||||
httpd.serve_forever()
|
||||
|
||||
|
||||
|
|
@ -720,7 +720,7 @@ This is a working "Hello World" WSGI application::
|
|||
return ["Hello World"]
|
||||
|
||||
httpd = make_server('', 8000, hello_world_app)
|
||||
print "Serving on port 8000..."
|
||||
print("Serving on port 8000...")
|
||||
|
||||
# Serve until process is killed
|
||||
httpd.serve_forever()
|
||||
|
|
|
|||
|
|
@ -147,10 +147,10 @@ module documentation. This section lists the differences between the API and
|
|||
document. Encoding this string in an encoding other than UTF-8 is likely
|
||||
incorrect, since UTF-8 is the default encoding of XML.
|
||||
|
||||
With an explicit *encoding* argument, the result is a byte string in the
|
||||
specified encoding. It is recommended that this argument is always specified. To
|
||||
avoid :exc:`UnicodeError` exceptions in case of unrepresentable text data, the
|
||||
encoding argument should be specified as "utf-8".
|
||||
With an explicit *encoding* argument, the result is a :class:`bytes` object
|
||||
in the specified encoding. It is recommended that this argument is always
|
||||
specified. To avoid :exc:`UnicodeError` exceptions in case of unrepresentable
|
||||
text data, the encoding argument should be specified as "utf-8".
|
||||
|
||||
|
||||
.. method:: Node.toprettyxml([indent[, newl[, encoding]]])
|
||||
|
|
@ -212,7 +212,7 @@ rules apply:
|
|||
``boolean`` all map to Python integer objects.
|
||||
|
||||
* The type ``DOMString`` maps to Python strings. :mod:`xml.dom.minidom` supports
|
||||
either byte or Unicode strings, but will normally produce Unicode strings.
|
||||
either bytes or strings, but will normally produce strings.
|
||||
Values of type ``DOMString`` may also be ``None`` where allowed to have the IDL
|
||||
``null`` value by the DOM specification from the W3C.
|
||||
|
||||
|
|
|
|||
|
|
@ -985,7 +985,7 @@ according to the following table.
|
|||
+------------------+-------------------------------------------+
|
||||
|
||||
Additionally, the :class:`DOMString` defined in the recommendation is mapped to
|
||||
a Python string or Unicode string. Applications should be able to handle
|
||||
a bytes or string object. Applications should be able to handle
|
||||
Unicode whenever a string is returned from the DOM.
|
||||
|
||||
The IDL ``null`` value is mapped to ``None``, which may be accepted or
|
||||
|
|
|
|||
|
|
@ -43,10 +43,11 @@ Functions
|
|||
|
||||
.. function:: Comment([text])
|
||||
|
||||
Comment element factory. This factory function creates a special element that
|
||||
will be serialized as an XML comment. The comment string can be either an 8-bit
|
||||
ASCII string or a Unicode string. *text* is a string containing the comment
|
||||
string. Returns an element instance representing a comment.
|
||||
Comment element factory. This factory function creates a special element
|
||||
that will be serialized as an XML comment. The comment string can be either
|
||||
an ASCII-only :class:`bytes` object or a :class:`str` object. *text* is a
|
||||
string containing the comment string. Returns an element instance
|
||||
representing a comment.
|
||||
|
||||
|
||||
.. function:: dump(elem)
|
||||
|
|
@ -67,10 +68,11 @@ Functions
|
|||
dependent, but it will always be compatible with the _ElementInterface class in
|
||||
this module.
|
||||
|
||||
The element name, attribute names, and attribute values can be either 8-bit
|
||||
ASCII strings or Unicode strings. *tag* is the element name. *attrib* is an
|
||||
optional dictionary, containing element attributes. *extra* contains additional
|
||||
attributes, given as keyword arguments. Returns an element instance.
|
||||
The element name, attribute names, and attribute values can be either an
|
||||
ASCII-only :class:`bytes` object or a :class:`str` object. *tag* is the
|
||||
element name. *attrib* is an optional dictionary, containing element
|
||||
attributes. *extra* contains additional attributes, given as keyword
|
||||
arguments. Returns an element instance.
|
||||
|
||||
|
||||
.. function:: fromstring(text)
|
||||
|
|
@ -114,11 +116,11 @@ Functions
|
|||
Subelement factory. This function creates an element instance, and appends it
|
||||
to an existing element.
|
||||
|
||||
The element name, attribute names, and attribute values can be either 8-bit
|
||||
ASCII strings or Unicode strings. *parent* is the parent element. *tag* is the
|
||||
subelement name. *attrib* is an optional dictionary, containing element
|
||||
attributes. *extra* contains additional attributes, given as keyword arguments.
|
||||
Returns an element instance.
|
||||
The element name, attribute names, and attribute values can be an ASCII-only
|
||||
:class:`bytes` object or a :class:`str` object. *parent* is the parent
|
||||
element. *tag* is the subelement name. *attrib* is an optional dictionary,
|
||||
containing element attributes. *extra* contains additional attributes, given
|
||||
as keyword arguments. Returns an element instance.
|
||||
|
||||
|
||||
.. function:: tostring(element[, encoding])
|
||||
|
|
@ -275,7 +277,7 @@ Element objects also support the following sequence type methods for working
|
|||
with subelements: :meth:`__delitem__`, :meth:`__getitem__`, :meth:`__setitem__`,
|
||||
:meth:`__len__`.
|
||||
|
||||
Caution: Because Element objects do not define a :meth:`__nonzero__` method,
|
||||
Caution: Because Element objects do not define a :meth:`__bool__` method,
|
||||
elements with no subelements will test as ``False``. ::
|
||||
|
||||
element = root.find('foo')
|
||||
|
|
@ -426,7 +428,7 @@ TreeBuilder Objects
|
|||
.. method:: TreeBuilder.data(data)
|
||||
|
||||
Adds text to the current element. *data* is a string. This should be either an
|
||||
8-bit string containing ASCII text, or a Unicode string.
|
||||
ASCII-only :class:`bytes` object or a :class:`str` object.
|
||||
|
||||
|
||||
.. method:: TreeBuilder.end(tag)
|
||||
|
|
|
|||
|
|
@ -281,8 +281,8 @@ events in the input document:
|
|||
must come from the same external entity so that the Locator provides useful
|
||||
information.
|
||||
|
||||
*content* may be a Unicode string or a byte string; the ``expat`` reader module
|
||||
produces always Unicode strings.
|
||||
*content* may be a string or bytes instance; the ``expat`` reader module
|
||||
always produces strings.
|
||||
|
||||
.. note::
|
||||
|
||||
|
|
|
|||
|
|
@ -307,7 +307,7 @@ InputSource Objects
|
|||
.. method:: InputSource.setCharacterStream(charfile)
|
||||
|
||||
Set the character stream for this input source. (The stream must be a Python 1.6
|
||||
Unicode-wrapped file-like that performs conversion to Unicode strings.)
|
||||
Unicode-wrapped file-like that performs conversion to strings.)
|
||||
|
||||
If there is a character stream specified, the SAX parser will ignore any byte
|
||||
stream and will not attempt to open a URI connection to the system identifier.
|
||||
|
|
|
|||
|
|
@ -194,7 +194,7 @@ A working example follows. The server code::
|
|||
return n%2 == 0
|
||||
|
||||
server = SimpleXMLRPCServer(("localhost", 8000))
|
||||
print "Listening on port 8000..."
|
||||
print("Listening on port 8000...")
|
||||
server.register_function(is_even, "is_even")
|
||||
server.serve_forever()
|
||||
|
||||
|
|
@ -203,8 +203,8 @@ The client code for the preceding server::
|
|||
import xmlrpclib
|
||||
|
||||
proxy = xmlrpclib.ServerProxy("http://localhost:8000/")
|
||||
print "3 is even: %s" % str(proxy.is_even(3))
|
||||
print "100 is even: %s" % str(proxy.is_even(100))
|
||||
print("3 is even: %s" % str(proxy.is_even(3)))
|
||||
print("100 is even: %s" % str(proxy.is_even(100)))
|
||||
|
||||
.. _datetime-objects:
|
||||
|
||||
|
|
@ -241,7 +241,7 @@ A working example follows. The server code::
|
|||
return xmlrpclib.DateTime(today)
|
||||
|
||||
server = SimpleXMLRPCServer(("localhost", 8000))
|
||||
print "Listening on port 8000..."
|
||||
print("Listening on port 8000...")
|
||||
server.register_function(today, "today")
|
||||
server.serve_forever()
|
||||
|
||||
|
|
@ -255,7 +255,7 @@ The client code for the preceding server::
|
|||
today = proxy.today()
|
||||
# convert the ISO8601 string to a datetime object
|
||||
converted = datetime.datetime.strptime(today.value, "%Y%m%dT%H:%M:%S")
|
||||
print "Today: %s" % converted.strftime("%d.%m.%Y, %H:%M")
|
||||
print("Today: %s" % converted.strftime("%d.%m.%Y, %H:%M"))
|
||||
|
||||
.. _binary-objects:
|
||||
|
||||
|
|
@ -300,7 +300,7 @@ XMLRPC::
|
|||
handle.close()
|
||||
|
||||
server = SimpleXMLRPCServer(("localhost", 8000))
|
||||
print "Listening on port 8000..."
|
||||
print("Listening on port 8000...")
|
||||
server.register_function(python_logo, 'python_logo')
|
||||
|
||||
server.serve_forever()
|
||||
|
|
@ -343,7 +343,7 @@ returning a complex type object. The server code::
|
|||
return x+y+0j
|
||||
|
||||
server = SimpleXMLRPCServer(("localhost", 8000))
|
||||
print "Listening on port 8000..."
|
||||
print("Listening on port 8000...")
|
||||
server.register_function(add, 'add')
|
||||
|
||||
server.serve_forever()
|
||||
|
|
@ -356,9 +356,9 @@ The client code for the preceding server::
|
|||
try:
|
||||
proxy.add(2, 5)
|
||||
except xmlrpclib.Fault, err:
|
||||
print "A fault occured"
|
||||
print "Fault code: %d" % err.faultCode
|
||||
print "Fault string: %s" % err.faultString
|
||||
print("A fault occured")
|
||||
print("Fault code: %d" % err.faultCode)
|
||||
print("Fault string: %s" % err.faultString)
|
||||
|
||||
|
||||
|
||||
|
|
@ -403,11 +403,11 @@ by providing an invalid URI::
|
|||
try:
|
||||
proxy.some_method()
|
||||
except xmlrpclib.ProtocolError, err:
|
||||
print "A protocol error occured"
|
||||
print "URL: %s" % err.url
|
||||
print "HTTP/HTTPS headers: %s" % err.headers
|
||||
print "Error code: %d" % err.errcode
|
||||
print "Error message: %s" % err.errmsg
|
||||
print("A protocol error occured")
|
||||
print("URL: %s" % err.url)
|
||||
print("HTTP/HTTPS headers: %s" % err.headers)
|
||||
print("Error code: %d" % err.errcode)
|
||||
print("Error message: %s" % err.errmsg)
|
||||
|
||||
MultiCall Objects
|
||||
-----------------
|
||||
|
|
@ -444,7 +444,7 @@ A usage example of this class follows. The server code ::
|
|||
|
||||
# A simple server with simple arithmetic functions
|
||||
server = SimpleXMLRPCServer(("localhost", 8000))
|
||||
print "Listening on port 8000..."
|
||||
print("Listening on port 8000...")
|
||||
server.register_multicall_functions()
|
||||
server.register_function(add, 'add')
|
||||
server.register_function(subtract, 'subtract')
|
||||
|
|
@ -464,7 +464,7 @@ The client code for the preceding server::
|
|||
multicall.divide(7,3)
|
||||
result = multicall()
|
||||
|
||||
print "7+3=%d, 7-3=%d, 7*3=%d, 7/3=%d" % tuple(result)
|
||||
print("7+3=%d, 7-3=%d, 7*3=%d, 7/3=%d" % tuple(result))
|
||||
|
||||
|
||||
Convenience Functions
|
||||
|
|
|
|||
|
|
@ -901,7 +901,6 @@ Internal types
|
|||
pair: exception; handler
|
||||
pair: execution; stack
|
||||
single: exc_info (in module sys)
|
||||
single: exc_traceback (in module sys)
|
||||
single: last_traceback (in module sys)
|
||||
single: sys.exc_info
|
||||
single: sys.last_traceback
|
||||
|
|
|
|||
|
|
@ -811,7 +811,7 @@ Examples::
|
|||
260
|
||||
|
||||
>>> from math import pi, sin
|
||||
>>> sine_table = dict((x, sin(x*pi/180)) for x in range(0, 91))
|
||||
>>> sine_table = {x: sin(x*pi/180) for x in range(0, 91)}
|
||||
|
||||
>>> unique_words = set(word for line in page for word in line.split())
|
||||
|
||||
|
|
|
|||
|
|
@ -273,7 +273,7 @@ Here are some nested for loops and other fancy behavior::
|
|||
|
||||
List comprehensions can be applied to complex expressions and nested functions::
|
||||
|
||||
>>> [str(round(355/113.0, i)) for i in range(1, 6)]
|
||||
>>> [str(round(355/113, i)) for i in range(1, 6)]
|
||||
['3.1', '3.14', '3.142', '3.1416', '3.14159']
|
||||
|
||||
|
||||
|
|
@ -437,6 +437,9 @@ Here is a brief demonstration::
|
|||
>>> fruit = set(basket) # create a set without duplicates
|
||||
>>> fruit
|
||||
{'orange', 'pear', 'apple', 'banana'}
|
||||
>>> fruit = {'orange', 'apple'} # {} syntax is equivalent to [] for lists
|
||||
>>> fruit
|
||||
{'orange', 'apple'}
|
||||
>>> 'orange' in fruit # fast membership testing
|
||||
True
|
||||
>>> 'crabgrass' in fruit
|
||||
|
|
@ -457,6 +460,11 @@ Here is a brief demonstration::
|
|||
>>> a ^ b # letters in a or b but not both
|
||||
{'r', 'd', 'b', 'm', 'z', 'l'}
|
||||
|
||||
Like for lists, there is a set comprehension syntax::
|
||||
|
||||
>>> a = {x for x in 'abracadabra' if x not in 'abc'}
|
||||
>>> a
|
||||
{'r', 'd'}
|
||||
|
||||
|
||||
|
||||
|
|
@ -518,12 +526,12 @@ comprehensions can compactly specify the key-value list. ::
|
|||
|
||||
>>> dict([('sape', 4139), ('guido', 4127), ('jack', 4098)])
|
||||
{'sape': 4139, 'jack': 4098, 'guido': 4127}
|
||||
>>> dict([(x, x**2) for x in (2, 4, 6)]) # use a list comprehension
|
||||
{2: 4, 4: 16, 6: 36}
|
||||
|
||||
Later in the tutorial, we will learn about Generator Expressions which are even
|
||||
better suited for the task of supplying key-values pairs to the :func:`dict`
|
||||
constructor.
|
||||
In addition, dict comprehensions can be used to create dictionaries from
|
||||
arbitrary key and value expressions::
|
||||
|
||||
>>> {x: x**2 for x in (2, 4, 6)}
|
||||
{2: 4, 4: 16, 6: 36}
|
||||
|
||||
When the keys are simple strings, it is sometimes easier to specify pairs using
|
||||
keyword arguments::
|
||||
|
|
@ -532,9 +540,8 @@ keyword arguments::
|
|||
{'sape': 4139, 'jack': 4098, 'guido': 4127}
|
||||
|
||||
|
||||
.. XXX Find out the right way to do these DUBOIS
|
||||
.. _tut-loopidioms:
|
||||
.. %
|
||||
Find out the right way to do these DUBOIS
|
||||
|
||||
Looping Techniques
|
||||
==================
|
||||
|
|
|
|||
|
|
@ -121,7 +121,7 @@ The :mod:`math` module gives access to the underlying C library functions for
|
|||
floating point math::
|
||||
|
||||
>>> import math
|
||||
>>> math.cos(math.pi / 4.0)
|
||||
>>> math.cos(math.pi / 4)
|
||||
0.70710678118654757
|
||||
>>> math.log(1024, 2)
|
||||
10.0
|
||||
|
|
@ -264,7 +264,7 @@ documentation::
|
|||
>>> print(average([20, 30, 70]))
|
||||
40.0
|
||||
"""
|
||||
return sum(values, 0.0) / len(values)
|
||||
return sum(values) / len(values)
|
||||
|
||||
import doctest
|
||||
doctest.testmod() # automatically validate the embedded tests
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue