@@ -307,9 +307,9 @@ The :mod:`threading` module's locks and condition variables also support the
307
307
The lock is acquired before the block is executed and always released once the
308
308
block is complete.
309
309
310
- The :func: `localcontext ` function in the :mod: `decimal ` module makes it easy
311
- to save and restore the current decimal context, which encapsulates the desired
312
- precision and rounding characteristics for computations::
310
+ The :func: `decimal. localcontext ` function in the :mod: `decimal ` module makes
311
+ it easy to save and restore the current decimal context, which encapsulates
312
+ the desired precision and rounding characteristics for computations::
313
313
314
314
from decimal import Decimal, Context, localcontext
315
315
@@ -337,12 +337,12 @@ underlying implementation and should keep reading.
337
337
A high-level explanation of the context management protocol is:
338
338
339
339
* The expression is evaluated and should result in an object called a "context
340
- manager". The context manager must have :meth: `~object.__enter__ ` and :meth: ` ~object.__exit__ `
341
- methods.
340
+ manager". The context manager must have :meth: `~object.__enter__ ` and
341
+ :meth: ` ~object.__exit__ ` methods.
342
342
343
- * The context manager's :meth: `~object.__enter__ ` method is called. The value returned
344
- is assigned to *VAR *. If no ``as VAR `` clause is present, the value is simply
345
- discarded.
343
+ * The context manager's :meth: `~object.__enter__ ` method is called. The value
344
+ returned is assigned to *VAR *. If no ``as VAR `` clause is present, the
345
+ value is simply discarded.
346
346
347
347
* The code in *BLOCK * is executed.
348
348
@@ -431,14 +431,15 @@ The contextlib module
431
431
The :mod: `contextlib ` module provides some functions and a decorator that
432
432
are useful when writing objects for use with the ':keyword: `with `' statement.
433
433
434
- The decorator is called :func: `~contextlib.contextmanager `, and lets you write a single
435
- generator function instead of defining a new class. The generator should yield
436
- exactly one value. The code up to the :keyword: `yield ` will be executed as the
437
- :meth: `~object.__enter__ ` method, and the value yielded will be the method's return
438
- value that will get bound to the variable in the ':keyword: `with `' statement's
439
- :keyword: `!as ` clause, if any. The code after the :keyword: `!yield ` will be
440
- executed in the :meth: `~object.__exit__ ` method. Any exception raised in the block will
441
- be raised by the :keyword: `!yield ` statement.
434
+ The decorator is called :func: `~contextlib.contextmanager `, and lets you write
435
+ a single generator function instead of defining a new class. The generator
436
+ should yield exactly one value. The code up to the :keyword: `yield ` will be
437
+ executed as the :meth: `~object.__enter__ ` method, and the value yielded will
438
+ be the method's return value that will get bound to the variable in the
439
+ ':keyword: `with `' statement's :keyword: `!as ` clause, if any. The code after
440
+ the :keyword: `!yield ` will be executed in the :meth: `~object.__exit__ ` method.
441
+ Any exception raised in the block will be raised by the :keyword: `!yield `
442
+ statement.
442
443
443
444
Using this decorator, our database example from the previous section
444
445
could be written as::
@@ -571,8 +572,9 @@ approach of the module is still similar. The fundamental class
571
572
is the :class: `~multiprocessing.Process `, which is passed a callable object and
572
573
a collection of arguments. The :meth: `~multiprocessing.Process.start ` method
573
574
sets the callable running in a subprocess, after which you can call
574
- the :meth: `~multiprocessing.Process.is_alive ` method to check whether the subprocess is still running
575
- and the :meth: `~multiprocessing.Process.join ` method to wait for the process to exit.
575
+ the :meth: `~multiprocessing.Process.is_alive ` method to check whether the
576
+ subprocess is still running and the :meth: `~multiprocessing.Process.join `
577
+ method to wait for the process to exit.
576
578
577
579
Here's a simple example where the subprocess will calculate a
578
580
factorial. The function doing the calculation is written strangely so
@@ -619,13 +621,15 @@ the object to communicate. (If the parent were to change the value of
619
621
the global variable, the child's value would be unaffected, and vice
620
622
versa.)
621
623
622
- Two other classes, :class: `~multiprocessing.pool.Pool ` and :class: `~multiprocessing.Manager `, provide
623
- higher-level interfaces. :class: `~multiprocessing.pool.Pool ` will create a fixed number of
624
- worker processes, and requests can then be distributed to the workers
625
- by calling :meth: `~multiprocessing.pool.Pool.apply ` or :meth: `~multiprocessing.pool.Pool.apply_async ` to add a single request,
626
- and :meth: `map ` or :meth: `~multiprocessing.pool.Pool.map_async ` to add a number of
627
- requests. The following code uses a :class: `~multiprocessing.pool.Pool ` to spread requests
628
- across 5 worker processes and retrieve a list of results::
624
+ Two other classes, :class: `~multiprocessing.pool.Pool ` and
625
+ :class: `~multiprocessing.Manager `, provide higher-level interfaces.
626
+ :class: `~multiprocessing.pool.Pool ` will create a fixed number of worker
627
+ processes, and requests can then be distributed to the workers by calling
628
+ :meth: `~multiprocessing.pool.Pool.apply ` or
629
+ :meth: `~multiprocessing.pool.Pool.apply_async ` to add a single request, and
630
+ :meth: `map ` or :meth: `~multiprocessing.pool.Pool.map_async ` to add a number of
631
+ requests. The following code uses a :class: `~multiprocessing.pool.Pool ` to
632
+ spread requests across 5 worker processes and retrieve a list of results::
629
633
630
634
from multiprocessing import Pool
631
635
@@ -646,15 +650,18 @@ This produces the following output::
646
650
33452526613163807108170062053440751665152000000000
647
651
...
648
652
649
- The other high-level interface, the :class: `~multiprocessing.Manager ` class, creates a
650
- separate server process that can hold master copies of Python data
653
+ The other high-level interface, the :class: `~multiprocessing.Manager ` class,
654
+ creates a separate server process that can hold master copies of Python data
651
655
structures. Other processes can then access and modify these data
652
656
structures using proxy objects. The following example creates a
653
657
shared dictionary by calling the :meth: `dict ` method; the worker
654
658
processes then insert values into the dictionary. (Locking is not
655
659
done for you automatically, which doesn't matter in this example.
656
- :class: `~multiprocessing.Manager `'s methods also include :meth: `~multiprocessing.managers.SyncManager.Lock `, :meth: `~multiprocessing.managers.SyncManager.RLock `,
657
- and :meth: `~multiprocessing.managers.SyncManager.Semaphore ` to create shared locks.)
660
+ :class: `~multiprocessing.Manager `'s methods also include
661
+ :meth: `~multiprocessing.managers.SyncManager.Lock `,
662
+ :meth: `~multiprocessing.managers.SyncManager.RLock `,
663
+ and :meth: `~multiprocessing.managers.SyncManager.Semaphore ` to create
664
+ shared locks.)
658
665
659
666
::
660
667
@@ -824,7 +831,7 @@ documentation for a :ref:`complete list <formatstrings>`; here's a sample:
824
831
format, followed by a percent sign.
825
832
===== ========================================================================
826
833
827
- Classes and types can define a :meth: ` __format__ ` method to control how they're
834
+ Classes and types can define a `` __format__ ` ` method to control how they're
828
835
formatted. It receives a single argument, the format specifier::
829
836
830
837
def __format__(self, format_spec):
@@ -834,7 +841,7 @@ formatted. It receives a single argument, the format specifier::
834
841
return str(self)
835
842
836
843
There's also a :func: `format ` builtin that will format a single
837
- value. It calls the type's :meth: ` __format__ ` method with the
844
+ value. It calls the type's `` __format__ ` ` method with the
838
845
provided specifier::
839
846
840
847
>>> format(75.6564, '.2f')
@@ -1029,22 +1036,23 @@ PEP 3116: New I/O Library
1029
1036
1030
1037
Python's built-in file objects support a number of methods, but
1031
1038
file-like objects don't necessarily support all of them. Objects that
1032
- imitate files usually support :meth: `~io.TextIOBase.read ` and :meth: ` ~io.TextIOBase.write `, but they
1033
- may not support :meth: `readline `, for example. Python 3.0 introduces
1034
- a layered I/O library in the :mod: `io ` module that separates buffering
1035
- and text-handling features from the fundamental read and write
1036
- operations.
1039
+ imitate files usually support :meth: `~io.TextIOBase.read ` and
1040
+ :meth: ` ~io.TextIOBase.write `, but they may not support :meth: `readline `,
1041
+ for example. Python 3.0 introduces a layered I/O library in the :mod: `io `
1042
+ module that separates buffering and text-handling features from the
1043
+ fundamental read and write operations.
1037
1044
1038
1045
There are three levels of abstract base classes provided by
1039
1046
the :mod: `io ` module:
1040
1047
1041
1048
* :class: `~io.RawIOBase ` defines raw I/O operations: :meth: `~io.RawIOBase.read `,
1042
- :meth: `~io.RawIOBase.readinto `,
1043
- :meth: `~io.RawIOBase.write `, :meth: ` ~io. IOBase.seek `, :meth: `~io.IOBase.tell `, :meth: `~io.IOBase.truncate `,
1044
- and :meth: `~io.IOBase.close `.
1049
+ :meth: `~io.RawIOBase.readinto `, :meth: ` ~io.RawIOBase.write `,
1050
+ :meth: `~io.IOBase.seek `, :meth: `~io.IOBase.tell `, :meth: `~io.IOBase.truncate `,
1051
+ and :meth: `~io.IOBase.close `.
1045
1052
Most of the methods of this class will often map to a single system call.
1046
- There are also :meth: `~io.IOBase.readable `, :meth: `~io.IOBase.writable `, and :meth: `~io.IOBase.seekable `
1047
- methods for determining what operations a given object will allow.
1053
+ There are also :meth: `~io.IOBase.readable `, :meth: `~io.IOBase.writable `,
1054
+ and :meth: `~io.IOBase.seekable ` methods for determining what operations a
1055
+ given object will allow.
1048
1056
1049
1057
Python 3.0 has concrete implementations of this class for files and
1050
1058
sockets, but Python 2.6 hasn't restructured its file and socket objects
@@ -1054,7 +1062,8 @@ the :mod:`io` module:
1054
1062
buffers data in memory to reduce the number of
1055
1063
system calls used, making I/O processing more efficient.
1056
1064
It supports all of the methods of :class: `~io.RawIOBase `,
1057
- and adds a :attr: `~io.BufferedIOBase.raw ` attribute holding the underlying raw object.
1065
+ and adds a :attr: `~io.BufferedIOBase.raw ` attribute holding the underlying
1066
+ raw object.
1058
1067
1059
1068
There are five concrete classes implementing this ABC.
1060
1069
:class: `~io.BufferedWriter ` and :class: `~io.BufferedReader ` are for objects
@@ -1173,8 +1182,8 @@ dictionary-style access. The phrase "dictionary-style" is vague, however.
1173
1182
It probably means that accessing items with ``obj[1] `` works.
1174
1183
Does it imply that setting items with ``obj[2] = value `` works?
1175
1184
Or that the object will have :meth: `!keys `, :meth: `!values `, and :meth: `!items `
1176
- methods? What about the iterative variants such as :meth: `!iterkeys `? :meth: ` !copy `
1177
- and :meth: `!update `? Iterating over the object with :func: `! iter `?
1185
+ methods? What about the iterative variants such as :meth: `!iterkeys `?
1186
+ :meth: ` !copy` and :meth: `!update`? Iterating over the object with :func: `iter `?
1178
1187
1179
1188
The Python 2.6 :mod: `collections ` module includes a number of
1180
1189
different ABCs that represent these distinctions. :class: `Iterable `
0 commit comments