Kaydet (Commit) 5f2dc0bf authored tarafından Andrew M. Kuchling's avatar Andrew M. Kuchling

Edit four more sections

üst 3ffe5639
...@@ -498,12 +498,15 @@ Python's :option:`-m` switch allows running a module as a script. ...@@ -498,12 +498,15 @@ Python's :option:`-m` switch allows running a module as a script.
When you ran a module that was located inside a package, relative When you ran a module that was located inside a package, relative
imports didn't work correctly. imports didn't work correctly.
The fix in Python 2.6 adds a :attr:`__package__` attribute to modules. The fix for Python 2.6 adds a :attr:`__package__` attribute to
When present, relative imports will be relative to the value of this modules. When this attribute is present, relative imports will be
attribute instead of the :attr:`__name__` attribute. PEP 302-style relative to the value of this attribute instead of the
importers can then set :attr:`__package__`. The :mod:`runpy` module :attr:`__name__` attribute.
that implements the :option:`-m` switch now does this, so relative imports
can now be used in scripts running from inside a package. PEP 302-style importers can then set :attr:`__package__` as necessary.
The :mod:`runpy` module that implements the :option:`-m` switch now
does this, so relative imports will now work correctly in scripts
running from inside a package.
.. ====================================================================== .. ======================================================================
...@@ -512,10 +515,10 @@ can now be used in scripts running from inside a package. ...@@ -512,10 +515,10 @@ can now be used in scripts running from inside a package.
PEP 370: Per-user ``site-packages`` Directory PEP 370: Per-user ``site-packages`` Directory
===================================================== =====================================================
When you run Python, the module search path ``sys.modules`` usually When you run Python, the module search path ``sys.path`` usually
includes a directory whose path ends in ``"site-packages"``. This includes a directory whose path ends in ``"site-packages"``. This
directory is intended to hold locally-installed packages available to directory is intended to hold locally-installed packages available to
all users on a machine or using a particular site installation. all users using a machine or a particular site installation.
Python 2.6 introduces a convention for user-specific site directories. Python 2.6 introduces a convention for user-specific site directories.
The directory varies depending on the platform: The directory varies depending on the platform:
...@@ -568,8 +571,8 @@ the :meth:`is_alive` method to check whether the subprocess is still running ...@@ -568,8 +571,8 @@ the :meth:`is_alive` method to check whether the subprocess is still running
and the :meth:`join` method to wait for the process to exit. and the :meth:`join` method to wait for the process to exit.
Here's a simple example where the subprocess will calculate a Here's a simple example where the subprocess will calculate a
factorial. The function doing the calculation is a bit strange; it's factorial. The function doing the calculation is written strangely so
written to take significantly longer when the input argument is a that it takes significantly longer when the input argument is a
multiple of 4. multiple of 4.
:: ::
...@@ -604,28 +607,31 @@ multiple of 4. ...@@ -604,28 +607,31 @@ multiple of 4.
result = queue.get() result = queue.get()
print 'Factorial', N, '=', result print 'Factorial', N, '=', result
A :class:`Queue` object is created and stored as a global. The child A :class:`Queue` is used to communicate the input parameter *N* and
process will use the value of the variable when the child was created; the result. The :class:`Queue` object is stored in a global variable.
because it's a :class:`Queue`, parent and child can use the object to The child process will use the value of the variable when the child
communicate. (If the parent were to change the value of the global was created; because it's a :class:`Queue`, parent and child can use
variable, the child's value would be unaffected, and vice versa.) the object to communicate. (If the parent were to change the value of
the global variable, the child's value would be unaffected, and vice
versa.)
Two other classes, :class:`Pool` and :class:`Manager`, provide Two other classes, :class:`Pool` and :class:`Manager`, provide
higher-level interfaces. :class:`Pool` will create a fixed number of higher-level interfaces. :class:`Pool` will create a fixed number of
worker processes, and requests can then be distributed to the workers worker processes, and requests can then be distributed to the workers
by calling :meth:`apply` or `apply_async`, adding a single request, by calling :meth:`apply` or `apply_async` to add a single request,
and :meth:`map` or :meth:`map_async` to distribute a number of and :meth:`map` or :meth:`map_async` to add a number of
requests. The following code uses a :class:`Pool` to spread requests requests. The following code uses a :class:`Pool` to spread requests
across 5 worker processes, receiving a list of results back. across 5 worker processes and retrieve a list of results::
::
from multiprocessing import Pool from multiprocessing import Pool
def factorial(N, dictionary):
"Compute a factorial."
...
p = Pool(5) p = Pool(5)
result = p.map(factorial, range(1, 1000, 10)) result = p.map(factorial, range(1, 1000, 10))
for v in result: for v in result:
print v print v
This produces the following output:: This produces the following output::
...@@ -636,14 +642,15 @@ This produces the following output:: ...@@ -636,14 +642,15 @@ This produces the following output::
33452526613163807108170062053440751665152000000000 33452526613163807108170062053440751665152000000000
... ...
The :class:`Manager` class creates a separate server process that can The other high-level interface, the :class:`Manager` class, creates a
hold master copies of Python data structures. Other processes can separate server process that can hold master copies of Python data
then access and modify these data structures by using proxy objects. structures. Other processes can then access and modify these data
The following example creates a shared dictionary by calling the structures using proxy objects. The following example creates a
:meth:`dict` method; the worker processes then insert values into the shared dictionary by calling the :meth:`dict` method; the worker
dictionary. (No locking is done automatically, which doesn't matter processes then insert values into the dictionary. (Locking is not
in this example. :class:`Manager`'s methods also include done for you automatically, which doesn't matter in this example.
:meth:`Lock`, :meth:`RLock`, and :meth:`Semaphore` to create shared locks. :class:`Manager`'s methods also include :meth:`Lock`, :meth:`RLock`,
and :meth:`Semaphore` to create shared locks.)
:: ::
...@@ -686,7 +693,7 @@ This will produce the output:: ...@@ -686,7 +693,7 @@ This will produce the output::
21 51090942171709440000 21 51090942171709440000
31 8222838654177922817725562880000000 31 8222838654177922817725562880000000
41 33452526613163807108170062053440751665152000000000 41 33452526613163807108170062053440751665152000000000
51 1551118753287382280224243016469303211063259720016986112000000000000 51 15511187532873822802242430164693032110632597200169861120000...
.. seealso:: .. seealso::
...@@ -716,9 +723,8 @@ The formatting template uses curly brackets (`{`, `}`) as special characters:: ...@@ -716,9 +723,8 @@ The formatting template uses curly brackets (`{`, `}`) as special characters::
"User ID: {0}".format("root") -> "User ID: root" "User ID: {0}".format("root") -> "User ID: root"
# Use the named keyword arguments # Use the named keyword arguments
uid = 'root' 'User ID: {uid} Last seen: {last_login}'.format(
uid='root',
'User ID: {uid} Last seen: {last_login}'.format(uid='root',
last_login = '5 Mar 2008 07:20') -> last_login = '5 Mar 2008 07:20') ->
'User ID: root Last seen: 5 Mar 2008 07:20' 'User ID: root Last seen: 5 Mar 2008 07:20'
...@@ -736,9 +742,9 @@ supply compound field names that read attributes or access dictionary keys:: ...@@ -736,9 +742,9 @@ supply compound field names that read attributes or access dictionary keys::
Python version: 2.6a1+ (trunk:61261M, Mar 5 2008, 20:29:41) \n Python version: 2.6a1+ (trunk:61261M, Mar 5 2008, 20:29:41) \n
[GCC 4.0.1 (Apple Computer, Inc. build 5367)]' [GCC 4.0.1 (Apple Computer, Inc. build 5367)]'
import mimetypes import mimetypes
'Content-type: {0[.mp4]}'.format(mimetypes.types_map) -> 'Content-type: {0[.mp4]}'.format(mimetypes.types_map) ->
'Content-type: video/mp4' 'Content-type: video/mp4'
Note that when using dictionary-style notation such as ``[.mp4]``, you Note that when using dictionary-style notation such as ``[.mp4]``, you
don't need to put any quotation marks around the string; it will look don't need to put any quotation marks around the string; it will look
...@@ -753,18 +759,24 @@ adding a colon followed by a format specifier. For example:: ...@@ -753,18 +759,24 @@ adding a colon followed by a format specifier. For example::
# Field 0: left justify, pad to 15 characters # Field 0: left justify, pad to 15 characters
# Field 1: right justify, pad to 6 characters # Field 1: right justify, pad to 6 characters
fmt = '{0:15} ${1:>6}' fmt = '{0:15} ${1:>6}'
fmt.format('Registration', 35) -> fmt.format('Registration', 35) ->
'Registration $ 35' 'Registration $ 35'
fmt.format('Tutorial', 50) -> fmt.format('Tutorial', 50) ->
'Tutorial $ 50' 'Tutorial $ 50'
fmt.format('Banquet', 125) -> fmt.format('Banquet', 125) ->
'Banquet $ 125' 'Banquet $ 125'
Format specifiers can reference other fields through nesting:: Format specifiers can reference other fields through nesting::
fmt = '{0:{1}}' fmt = '{0:{1}}'
fmt.format('Invoice #1234', 15) ->
width = 15
fmt.format('Invoice #1234', width) ->
'Invoice #1234 ' 'Invoice #1234 '
width = 35 width = 35
fmt.format('Invoice #1234', width) -> fmt.format('Invoice #1234', width) ->
'Invoice #1234 ' 'Invoice #1234 '
...@@ -819,8 +831,9 @@ formatted. It receives a single argument, the format specifier:: ...@@ -819,8 +831,9 @@ formatted. It receives a single argument, the format specifier::
else: else:
return str(self) return str(self)
There's also a format() built-in that will format a single value. It calls There's also a :func:`format` built-in that will format a single
the type's :meth:`__format__` method with the provided specifier:: value. It calls the type's :meth:`__format__` method with the
provided specifier::
>>> format(75.6564, '.2f') >>> format(75.6564, '.2f')
'75.66' '75.66'
...@@ -829,7 +842,7 @@ the type's :meth:`__format__` method with the provided specifier:: ...@@ -829,7 +842,7 @@ the type's :meth:`__format__` method with the provided specifier::
.. seealso:: .. seealso::
:ref:`formatstrings` :ref:`formatstrings`
The reference format fields. The reference documentation for format fields.
:pep:`3101` - Advanced String Formatting :pep:`3101` - Advanced String Formatting
PEP written by Talin. Implemented by Eric Smith. PEP written by Talin. Implemented by Eric Smith.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment