diff options
author | Serhiy Storchaka <storchaka@gmail.com> | 2016-05-10 09:01:23 (GMT) |
---|---|---|
committer | Serhiy Storchaka <storchaka@gmail.com> | 2016-05-10 09:01:23 (GMT) |
commit | dba903993a8d3e13d2cf83d6a8912e908025b17b (patch) | |
tree | b0f7d957452d40ce384e5d0a1382067e3379f60f /Doc/howto | |
parent | 387235085c5a6a1d823b0af3fabb42830c88f984 (diff) | |
download | cpython-dba903993a8d3e13d2cf83d6a8912e908025b17b.zip cpython-dba903993a8d3e13d2cf83d6a8912e908025b17b.tar.gz cpython-dba903993a8d3e13d2cf83d6a8912e908025b17b.tar.bz2 |
Issue #23921: Standardized documentation whitespace formatting.
Original patch by James Edwards.
Diffstat (limited to 'Doc/howto')
-rw-r--r-- | Doc/howto/descriptor.rst | 64 | ||||
-rw-r--r-- | Doc/howto/functional.rst | 10 | ||||
-rw-r--r-- | Doc/howto/logging-cookbook.rst | 56 | ||||
-rw-r--r-- | Doc/howto/logging.rst | 4 | ||||
-rw-r--r-- | Doc/howto/regex.rst | 12 | ||||
-rw-r--r-- | Doc/howto/unicode.rst | 2 | ||||
-rw-r--r-- | Doc/howto/urllib2.rst | 12 |
7 files changed, 81 insertions, 79 deletions
diff --git a/Doc/howto/descriptor.rst b/Doc/howto/descriptor.rst index 530f34b..d370eb5 100644 --- a/Doc/howto/descriptor.rst +++ b/Doc/howto/descriptor.rst @@ -104,7 +104,7 @@ like:: "Emulate type_getattro() in Objects/typeobject.c" v = object.__getattribute__(self, key) if hasattr(v, '__get__'): - return v.__get__(None, self) + return v.__get__(None, self) return v The important points to remember are: @@ -163,9 +163,9 @@ descriptor is useful for monitoring just a few chosen attributes:: self.val = val >>> class MyClass(object): - x = RevealAccess(10, 'var "x"') - y = 5 - + ... x = RevealAccess(10, 'var "x"') + ... y = 5 + ... >>> m = MyClass() >>> m.x Retrieving var "x" @@ -287,15 +287,15 @@ this:: Running the interpreter shows how the function descriptor works in practice:: >>> class D(object): - def f(self, x): - return x - + ... def f(self, x): + ... return x + ... >>> d = D() - >>> D.__dict__['f'] # Stored internally as a function + >>> D.__dict__['f'] # Stored internally as a function <function f at 0x00C45070> - >>> D.f # Get from a class becomes an unbound method + >>> D.f # Get from a class becomes an unbound method <unbound method D.f> - >>> d.f # Get from an instance becomes a bound method + >>> d.f # Get from an instance becomes a bound method <bound method D.f of <__main__.D object at 0x00B18C90>> The output suggests that bound and unbound methods are two different types. @@ -358,10 +358,10 @@ Since staticmethods return the underlying function with no changes, the example calls are unexciting:: >>> class E(object): - def f(x): - print(x) - f = staticmethod(f) - + ... def f(x): + ... print(x) + ... f = staticmethod(f) + ... >>> print(E.f(3)) 3 >>> print(E().f(3)) @@ -371,23 +371,23 @@ Using the non-data descriptor protocol, a pure Python version of :func:`staticmethod` would look like this:: class StaticMethod(object): - "Emulate PyStaticMethod_Type() in Objects/funcobject.c" + "Emulate PyStaticMethod_Type() in Objects/funcobject.c" - def __init__(self, f): - self.f = f + def __init__(self, f): + self.f = f - def __get__(self, obj, objtype=None): - return self.f + def __get__(self, obj, objtype=None): + return self.f Unlike static methods, class methods prepend the class reference to the argument list before calling the function. This format is the same for whether the caller is an object or a class:: >>> class E(object): - def f(klass, x): - return klass.__name__, x - f = classmethod(f) - + ... def f(klass, x): + ... return klass.__name__, x + ... f = classmethod(f) + ... >>> print(E.f(3)) ('E', 3) >>> print(E().f(3)) @@ -419,15 +419,15 @@ Using the non-data descriptor protocol, a pure Python version of :func:`classmethod` would look like this:: class ClassMethod(object): - "Emulate PyClassMethod_Type() in Objects/funcobject.c" + "Emulate PyClassMethod_Type() in Objects/funcobject.c" - def __init__(self, f): - self.f = f + def __init__(self, f): + self.f = f - def __get__(self, obj, klass=None): - if klass is None: - klass = type(obj) - def newfunc(*args): - return self.f(klass, *args) - return newfunc + def __get__(self, obj, klass=None): + if klass is None: + klass = type(obj) + def newfunc(*args): + return self.f(klass, *args) + return newfunc diff --git a/Doc/howto/functional.rst b/Doc/howto/functional.rst index 80ff710..6e21f93 100644 --- a/Doc/howto/functional.rst +++ b/Doc/howto/functional.rst @@ -395,14 +395,14 @@ equivalent to the following Python code:: continue # Skip this element for expr2 in sequence2: if not (condition2): - continue # Skip this element + continue # Skip this element ... for exprN in sequenceN: - if not (conditionN): - continue # Skip this element + if not (conditionN): + continue # Skip this element - # Output the value of - # the expression. + # Output the value of + # the expression. This means that when there are multiple ``for...in`` clauses but no ``if`` clauses, the length of the resulting output will be equal to the product of the diff --git a/Doc/howto/logging-cookbook.rst b/Doc/howto/logging-cookbook.rst index e784acc..99b4cdc 100644 --- a/Doc/howto/logging-cookbook.rst +++ b/Doc/howto/logging-cookbook.rst @@ -63,6 +63,7 @@ Here is the auxiliary module:: def __init__(self): self.logger = logging.getLogger('spam_application.auxiliary.Auxiliary') self.logger.info('creating an instance of Auxiliary') + def do_something(self): self.logger.info('doing something') a = 1 + 1 @@ -360,7 +361,7 @@ classes, which would eat up one thread per handler for no particular benefit. An example of using these two classes follows (imports omitted):: - que = queue.Queue(-1) # no limit on size + que = queue.Queue(-1) # no limit on size queue_handler = QueueHandler(que) handler = logging.StreamHandler() listener = QueueListener(que, handler) @@ -656,21 +657,21 @@ script:: return True if __name__ == '__main__': - levels = (logging.DEBUG, logging.INFO, logging.WARNING, logging.ERROR, logging.CRITICAL) - logging.basicConfig(level=logging.DEBUG, - format='%(asctime)-15s %(name)-5s %(levelname)-8s IP: %(ip)-15s User: %(user)-8s %(message)s') - a1 = logging.getLogger('a.b.c') - a2 = logging.getLogger('d.e.f') - - f = ContextFilter() - a1.addFilter(f) - a2.addFilter(f) - a1.debug('A debug message') - a1.info('An info message with %s', 'some parameters') - for x in range(10): - lvl = choice(levels) - lvlname = logging.getLevelName(lvl) - a2.log(lvl, 'A message at %s level with %d %s', lvlname, 2, 'parameters') + levels = (logging.DEBUG, logging.INFO, logging.WARNING, logging.ERROR, logging.CRITICAL) + logging.basicConfig(level=logging.DEBUG, + format='%(asctime)-15s %(name)-5s %(levelname)-8s IP: %(ip)-15s User: %(user)-8s %(message)s') + a1 = logging.getLogger('a.b.c') + a2 = logging.getLogger('d.e.f') + + f = ContextFilter() + a1.addFilter(f) + a2.addFilter(f) + a1.debug('A debug message') + a1.info('An info message with %s', 'some parameters') + for x in range(10): + lvl = choice(levels) + lvlname = logging.getLevelName(lvl) + a2.log(lvl, 'A message at %s level with %d %s', lvlname, 2, 'parameters') which, when run, produces something like:: @@ -764,10 +765,10 @@ the basis for code meeting your own specific requirements:: while True: try: record = queue.get() - if record is None: # We send this as a sentinel to tell the listener to quit. + if record is None: # We send this as a sentinel to tell the listener to quit. break logger = logging.getLogger(record.name) - logger.handle(record) # No level or filter logic applied - just do it! + logger.handle(record) # No level or filter logic applied - just do it! except Exception: import sys, traceback print('Whoops! Problem:', file=sys.stderr) @@ -790,10 +791,11 @@ the basis for code meeting your own specific requirements:: # Note that on Windows you can't rely on fork semantics, so each process # will run the logging configuration code when it starts. def worker_configurer(queue): - h = logging.handlers.QueueHandler(queue) # Just the one handler needed + h = logging.handlers.QueueHandler(queue) # Just the one handler needed root = logging.getLogger() root.addHandler(h) - root.setLevel(logging.DEBUG) # send all messages, for demo; no other level or filter logic applied. + # send all messages, for demo; no other level or filter logic applied. + root.setLevel(logging.DEBUG) # This is the worker process top-level loop, which just logs ten events with # random intervening delays before terminating. @@ -821,7 +823,7 @@ the basis for code meeting your own specific requirements:: workers = [] for i in range(10): worker = multiprocessing.Process(target=worker_process, - args=(queue, worker_configurer)) + args=(queue, worker_configurer)) workers.append(worker) worker.start() for w in workers: @@ -1245,12 +1247,12 @@ You can use a :class:`QueueHandler` subclass to send messages to other kinds of queues, for example a ZeroMQ 'publish' socket. In the example below,the socket is created separately and passed to the handler (as its 'queue'):: - import zmq # using pyzmq, the Python binding for ZeroMQ - import json # for serializing records portably + import zmq # using pyzmq, the Python binding for ZeroMQ + import json # for serializing records portably ctx = zmq.Context() - sock = zmq.Socket(ctx, zmq.PUB) # or zmq.PUSH, or other suitable value - sock.bind('tcp://*:5556') # or wherever + sock = zmq.Socket(ctx, zmq.PUB) # or zmq.PUSH, or other suitable value + sock.bind('tcp://*:5556') # or wherever class ZeroMQSocketHandler(QueueHandler): def enqueue(self, record): @@ -1288,7 +1290,7 @@ of queues, for example a ZeroMQ 'subscribe' socket. Here's an example:: def __init__(self, uri, *handlers, **kwargs): self.ctx = kwargs.get('ctx') or zmq.Context() socket = zmq.Socket(self.ctx, zmq.SUB) - socket.setsockopt(zmq.SUBSCRIBE, '') # subscribe to everything + socket.setsockopt(zmq.SUBSCRIBE, '') # subscribe to everything socket.connect(uri) def dequeue(self): @@ -2116,7 +2118,7 @@ class, as shown in the following example:: Format an exception so that it prints on a single line. """ result = super(OneLineExceptionFormatter, self).formatException(exc_info) - return repr(result) # or format into one line however you want to + return repr(result) # or format into one line however you want to def format(self, record): s = super(OneLineExceptionFormatter, self).format(record) diff --git a/Doc/howto/logging.rst b/Doc/howto/logging.rst index d66770f..51e8430 100644 --- a/Doc/howto/logging.rst +++ b/Doc/howto/logging.rst @@ -103,8 +103,8 @@ A simple example A very simple example is:: import logging - logging.warning('Watch out!') # will print a message to the console - logging.info('I told you so') # will not print anything + logging.warning('Watch out!') # will print a message to the console + logging.info('I told you so') # will not print anything If you type these lines into a script and run it, you'll see:: diff --git a/Doc/howto/regex.rst b/Doc/howto/regex.rst index 909420b..de3f461 100644 --- a/Doc/howto/regex.rst +++ b/Doc/howto/regex.rst @@ -1115,19 +1115,19 @@ which can be either a string or a function, and the string to be processed. Here's a simple example of using the :meth:`sub` method. It replaces colour names with the word ``colour``:: - >>> p = re.compile( '(blue|white|red)') - >>> p.sub( 'colour', 'blue socks and red shoes') + >>> p = re.compile('(blue|white|red)') + >>> p.sub('colour', 'blue socks and red shoes') 'colour socks and colour shoes' - >>> p.sub( 'colour', 'blue socks and red shoes', count=1) + >>> p.sub('colour', 'blue socks and red shoes', count=1) 'colour socks and red shoes' The :meth:`subn` method does the same work, but returns a 2-tuple containing the new string value and the number of replacements that were performed:: - >>> p = re.compile( '(blue|white|red)') - >>> p.subn( 'colour', 'blue socks and red shoes') + >>> p = re.compile('(blue|white|red)') + >>> p.subn('colour', 'blue socks and red shoes') ('colour socks and colour shoes', 2) - >>> p.subn( 'colour', 'no colours at all') + >>> p.subn('colour', 'no colours at all') ('no colours at all', 0) Empty matches are replaced only when they're not adjacent to a previous match. diff --git a/Doc/howto/unicode.rst b/Doc/howto/unicode.rst index 24051bf..50a09ba 100644 --- a/Doc/howto/unicode.rst +++ b/Doc/howto/unicode.rst @@ -687,7 +687,7 @@ with the ``surrogateescape`` error handler:: # make changes to the string 'data' with open(fname + '.new', 'w', - encoding="ascii", errors="surrogateescape") as f: + encoding="ascii", errors="surrogateescape") as f: f.write(data) The ``surrogateescape`` error handler will decode any non-ASCII bytes diff --git a/Doc/howto/urllib2.rst b/Doc/howto/urllib2.rst index b4e2157..24a4156 100644 --- a/Doc/howto/urllib2.rst +++ b/Doc/howto/urllib2.rst @@ -175,10 +175,10 @@ Explorer [#]_. :: url = 'http://www.someserver.com/cgi-bin/register.cgi' user_agent = 'Mozilla/5.0 (Windows NT 6.1; Win64; x64)' - values = {'name' : 'Michael Foord', - 'location' : 'Northampton', - 'language' : 'Python' } - headers = { 'User-Agent' : user_agent } + values = {'name': 'Michael Foord', + 'location': 'Northampton', + 'language': 'Python' } + headers = {'User-Agent': user_agent} data = urllib.parse.urlencode(values) data = data.encode('ascii') @@ -215,7 +215,7 @@ e.g. :: >>> req = urllib.request.Request('http://www.pretend_server.org') >>> try: urllib.request.urlopen(req) ... except urllib.error.URLError as e: - ... print(e.reason) #doctest: +SKIP + ... print(e.reason) #doctest: +SKIP ... (4, 'getaddrinfo failed') @@ -372,7 +372,7 @@ Number 2 :: from urllib.request import Request, urlopen - from urllib.error import URLError + from urllib.error import URLError req = Request(someurl) try: response = urlopen(req) |