Skip to content

Commit ad810ff

Browse files
committed
Make example code more compact
1 parent d2d58ca commit ad810ff

15 files changed

+38
-71
lines changed

Doc/faq/programming.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,7 @@ Why are default values shared between objects?
340340
This type of bug commonly bites neophyte programmers. Consider this function::
341341

342342
def foo(mydict={}): # Danger: shared reference to one dict for all calls
343-
# ... compute something ...
343+
# compute something
344344
mydict[key] = value
345345
return mydict
346346

@@ -382,8 +382,8 @@ requested again. This is called "memoizing", and can be implemented like this::
382382
return _cache[(arg1, arg2)]
383383

384384
# Calculate the value
385-
result = ... # ... expensive computation ...
386-
_cache[(arg1, arg2)] = result # Store result in the cache
385+
result = ... # expensive computation
386+
_cache[(arg1, arg2)] = result # Store result in the cache
387387
return result
388388

389389
You could use a global variable containing a dictionary instead of the default

Doc/howto/ipaddress.rst

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -254,8 +254,7 @@ It also means that network objects lend themselves to using the list
254254
membership test syntax like this::
255255

256256
if address in network:
257-
# do something
258-
...
257+
... # do something
259258

260259
Containment testing is done efficiently based on the network prefix::
261260

Doc/howto/urllib2.rst

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -359,8 +359,7 @@ Number 1
359359
print('We failed to reach a server.')
360360
print('Reason: ', e.reason)
361361
else:
362-
# everything is fine
363-
...
362+
... # everything is fine
364363

365364

366365
.. note::
@@ -386,8 +385,7 @@ Number 2
386385
print('The server couldn\'t fulfill the request.')
387386
print('Error code: ', e.code)
388387
else:
389-
# everything is fine
390-
...
388+
... # everything is fine
391389

392390

393391
info and geturl

Doc/library/asyncio-eventloop.rst

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1655,8 +1655,7 @@ Do not instantiate the :class:`Server` class directly.
16551655
srv = await loop.create_server(...)
16561656

16571657
async with srv:
1658-
# some code
1659-
...
1658+
... # some code
16601659

16611660
# At this point, srv is closed and no longer accepts new connections.
16621661

Doc/library/asyncio-sync.rst

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,7 @@ Lock
5151

5252
# ... later
5353
async with lock:
54-
# access shared state
55-
...
54+
... # access shared state
5655

5756
which is equivalent to::
5857

@@ -61,8 +60,7 @@ Lock
6160
# ... later
6261
await lock.acquire()
6362
try:
64-
# access shared state
65-
...
63+
... # access shared state
6664
finally:
6765
lock.release()
6866

@@ -301,8 +299,7 @@ Semaphore
301299

302300
# ... later
303301
async with sem:
304-
# work with shared resource
305-
...
302+
... # work with shared resource
306303

307304
which is equivalent to::
308305

@@ -311,8 +308,7 @@ Semaphore
311308
# ... later
312309
await sem.acquire()
313310
try:
314-
# work with shared resource
315-
...
311+
... # work with shared resource
316312
finally:
317313
sem.release()
318314

Doc/library/contextlib.rst

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -145,8 +145,7 @@ Functions and classes provided:
145145

146146
@timeit()
147147
async def main():
148-
# ... async code ...
149-
...
148+
... # async code
150149

151150
When used as a decorator, a new generator instance is implicitly created on
152151
each function call. This allows the otherwise "one-shot" context managers
@@ -242,8 +241,7 @@ Functions and classes provided:
242241
# Do not ignore any exceptions, cm has no effect.
243242
cm = contextlib.nullcontext()
244243
with cm:
245-
# Do something
246-
...
244+
... # Do something
247245

248246
An example using *enter_result*::
249247

@@ -256,8 +254,7 @@ Functions and classes provided:
256254
cm = nullcontext(file_or_path)
257255

258256
with cm as file:
259-
# Perform processing on the file
260-
...
257+
... # Perform processing on the file
261258

262259
It can also be used as a stand-in for
263260
:ref:`asynchronous context managers <async-context-managers>`::
@@ -271,8 +268,7 @@ Functions and classes provided:
271268
cm = nullcontext(session)
272269

273270
async with cm as session:
274-
# Send http requests with session
275-
...
271+
... # Send http requests with session
276272

277273
.. versionadded:: 3.7
278274

@@ -442,15 +438,13 @@ Functions and classes provided:
442438

443439
def f():
444440
with cm():
445-
# Do stuff
446-
...
441+
... # Do stuff
447442

448443
``ContextDecorator`` lets you instead write::
449444

450445
@cm()
451446
def f():
452-
# Do stuff
453-
...
447+
... # Do stuff
454448

455449
It makes it clear that the ``cm`` applies to the whole function, rather than
456450
just a piece of it (and saving an indentation level is nice, too).
@@ -712,12 +706,10 @@ protocol can be separated slightly in order to allow this::
712706
try:
713707
x = stack.enter_context(cm)
714708
except Exception:
715-
# handle __enter__ exception
716-
...
709+
... # handle __enter__ exception
717710
else:
718711
with stack:
719-
# Handle normal case
720-
...
712+
... # Handle normal case
721713

722714
Actually needing to do this is likely to indicate that the underlying API
723715
should be providing a direct resource management interface for use with

Doc/library/inspect.rst

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1404,8 +1404,7 @@ is considered deprecated and may be removed in the future.
14041404
def handle_stackframe_without_leak():
14051405
frame = inspect.currentframe()
14061406
try:
1407-
# do something with the frame
1408-
...
1407+
... # do something with the frame
14091408
finally:
14101409
del frame
14111410

Doc/library/logging.rst

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1175,8 +1175,7 @@ functions.
11751175
not undo customizations already applied by other code. For example::
11761176

11771177
class MyLogger(logging.getLoggerClass()):
1178-
# ... override behaviour here
1179-
...
1178+
... # override behaviour here
11801179

11811180

11821181
.. function:: getLogRecordFactory()

Doc/library/multiprocessing.rst

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2935,8 +2935,7 @@ Explicitly pass resources to child processes
29352935
from multiprocessing import Process, Lock
29362936

29372937
def f():
2938-
# ... do something using "lock" ...
2939-
...
2938+
... # do something using "lock"
29402939

29412940
if __name__ == '__main__':
29422941
lock = Lock()
@@ -2948,8 +2947,7 @@ Explicitly pass resources to child processes
29482947
from multiprocessing import Process, Lock
29492948

29502949
def f(l):
2951-
# ... do something using "l" ...
2952-
...
2950+
... # do something using "l"
29532951

29542952
if __name__ == '__main__':
29552953
lock = Lock()

Doc/library/sys.rst

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1461,8 +1461,7 @@ always available. Unless explicitly noted otherwise, all variables are read-only
14611461
recommended to use the following idiom::
14621462

14631463
if sys.platform.startswith('sunos'):
1464-
# SunOS-specific code here...
1465-
...
1464+
... # SunOS-specific code here
14661465

14671466
.. versionchanged:: 3.3
14681467
On Linux, :data:`sys.platform` doesn't contain the major version anymore.

Doc/library/test.rst

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -62,30 +62,25 @@ A basic boilerplate is often used::
6262
# Only use setUp() and tearDown() if necessary
6363

6464
def setUp(self):
65-
# ... code to execute in preparation for tests ...
66-
...
65+
... # code to execute in preparation for tests
6766

6867
def tearDown(self):
69-
# ... code to execute to clean up after tests ...
70-
...
68+
... # code to execute to clean up after tests
7169

7270
def test_feature_one(self):
7371
# Test feature one.
74-
# ... testing code ...
75-
...
72+
... # testing code
7673

7774
def test_feature_two(self):
7875
# Test feature two.
79-
# ... testing code ...
80-
...
76+
... # testing code
8177

82-
# ... more test methods ...
78+
# more test methods
8379

8480
class MyTestCase2(unittest.TestCase):
85-
# ... same structure as MyTestCase1 ...
86-
...
81+
... # same structure as MyTestCase1
8782

88-
# ... more test classes ...
83+
# more test classes
8984

9085
if __name__ == '__main__':
9186
unittest.main()
@@ -1687,8 +1682,7 @@ The :mod:`test.support.warnings_helper` module provides support for warnings tes
16871682

16881683
@warning_helper.ignore_warnings(category=DeprecationWarning)
16891684
def test_suppress_warning():
1690-
# do something
1691-
...
1685+
... # do something
16921686

16931687
.. versionadded:: 3.8
16941688

Doc/library/threading.rst

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -982,8 +982,7 @@ when they need to connect to the server::
982982
with pool_sema:
983983
conn = connectdb()
984984
try:
985-
# ... use connection ...
986-
...
985+
... # use connection
987986
finally:
988987
conn.close()
989988

@@ -1202,15 +1201,13 @@ entered, and ``release`` will be called when the block is exited. Hence,
12021201
the following snippet::
12031202

12041203
with some_lock:
1205-
# do something...
1206-
...
1204+
... # do something
12071205

12081206
is equivalent to::
12091207

12101208
some_lock.acquire()
12111209
try:
1212-
# do something...
1213-
...
1210+
... # do something
12141211
finally:
12151212
some_lock.release()
12161213

Doc/library/weakref.rst

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -587,8 +587,7 @@ third party, such as running code when a module is unloaded::
587587

588588
import weakref, sys
589589
def unloading_module():
590-
# implicit reference to the module globals from the function body
591-
...
590+
... # implicit reference to the module globals from the function body
592591
weakref.finalize(sys.modules[__name__], unloading_module)
593592

594593

Doc/whatsnew/3.7.rst

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -716,8 +716,7 @@ include:
716716
srv = await loop.create_server(...)
717717

718718
async with srv:
719-
# some code
720-
...
719+
... # some code
721720

722721
# At this point, srv is closed and no longer accepts new connections.
723722

Doc/whatsnew/3.8.rst

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -180,8 +180,7 @@ is an excerpt from code in the :mod:`collections` module::
180180
class Counter(dict):
181181

182182
def __init__(self, iterable=None, /, **kwds):
183-
# Note "iterable" is a possible keyword argument
184-
...
183+
... # Note "iterable" is a possible keyword argument
185184

186185
See :pep:`570` for a full description.
187186

0 commit comments

Comments
 (0)