mirror of https://github.com/python/cpython
Formatting fixes in contextlib docs (#98111)
This commit is contained in:
parent
e3bf125c81
commit
3b33c2010a
|
@ -66,6 +66,8 @@ Functions and classes provided:
|
||||||
# Code to release resource, e.g.:
|
# Code to release resource, e.g.:
|
||||||
release_resource(resource)
|
release_resource(resource)
|
||||||
|
|
||||||
|
The function can then be used like this::
|
||||||
|
|
||||||
>>> with managed_resource(timeout=3600) as resource:
|
>>> with managed_resource(timeout=3600) as resource:
|
||||||
... # Resource is released at the end of this block,
|
... # Resource is released at the end of this block,
|
||||||
... # even if code in the block raises an exception
|
... # even if code in the block raises an exception
|
||||||
|
@ -140,9 +142,9 @@ Functions and classes provided:
|
||||||
finally:
|
finally:
|
||||||
print(f'it took {time.monotonic() - now}s to run')
|
print(f'it took {time.monotonic() - now}s to run')
|
||||||
|
|
||||||
@timeit()
|
@timeit()
|
||||||
async def main():
|
async def main():
|
||||||
# ... async code ...
|
# ... async code ...
|
||||||
|
|
||||||
When used as a decorator, a new generator instance is implicitly created on
|
When used as a decorator, a new generator instance is implicitly created on
|
||||||
each function call. This allows the otherwise "one-shot" context managers
|
each function call. This allows the otherwise "one-shot" context managers
|
||||||
|
@ -249,15 +251,15 @@ Functions and classes provided:
|
||||||
:ref:`asynchronous context managers <async-context-managers>`::
|
:ref:`asynchronous context managers <async-context-managers>`::
|
||||||
|
|
||||||
async def send_http(session=None):
|
async def send_http(session=None):
|
||||||
if not session:
|
if not session:
|
||||||
# If no http session, create it with aiohttp
|
# If no http session, create it with aiohttp
|
||||||
cm = aiohttp.ClientSession()
|
cm = aiohttp.ClientSession()
|
||||||
else:
|
else:
|
||||||
# Caller is responsible for closing the session
|
# Caller is responsible for closing the session
|
||||||
cm = nullcontext(session)
|
cm = nullcontext(session)
|
||||||
|
|
||||||
async with cm as session:
|
async with cm as session:
|
||||||
# Send http requests with session
|
# Send http requests with session
|
||||||
|
|
||||||
.. versionadded:: 3.7
|
.. versionadded:: 3.7
|
||||||
|
|
||||||
|
@ -396,6 +398,8 @@ Functions and classes provided:
|
||||||
print('Finishing')
|
print('Finishing')
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
The class can then be used like this::
|
||||||
|
|
||||||
>>> @mycontext()
|
>>> @mycontext()
|
||||||
... def function():
|
... def function():
|
||||||
... print('The bit in the middle')
|
... print('The bit in the middle')
|
||||||
|
@ -466,6 +470,8 @@ Functions and classes provided:
|
||||||
print('Finishing')
|
print('Finishing')
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
The class can then be used like this::
|
||||||
|
|
||||||
>>> @mycontext()
|
>>> @mycontext()
|
||||||
... async def function():
|
... async def function():
|
||||||
... print('The bit in the middle')
|
... print('The bit in the middle')
|
||||||
|
|
Loading…
Reference in New Issue