💾 Archived View for tris.fyi › pydoc › contextlib captured on 2023-04-26 at 13:28:08. Gemini links have been rewritten to link to archived content
⬅️ Previous capture (2023-01-29)
-=-=-=-=-=-=-
Utilities for with-statement contexts. See PEP 343.
An abstract base class for asynchronous context managers.
An abstract base class for context managers.
A base class or mixin that enables async context managers to work as decorators.
Async context manager for dynamic management of a stack of exit callbacks. For example: async with AsyncExitStack() as stack: connections = [await stack.enter_async_context(get_connection()) for i in range(5)] # All opened connections will automatically be released at the # end of the async with statement, even if attempts to open a # connection later in the list raise an exception.
aclose(self) Immediately unwind the context stack.
callback(self, callback, /, *args, **kwds) Registers an arbitrary callback and arguments. Cannot suppress exceptions.
enter_async_context(self, cm) Enters the supplied async context manager. If successful, also pushes its __aexit__ method as a callback and returns the result of the __aenter__ method.
enter_context(self, cm) Enters the supplied context manager. If successful, also pushes its __exit__ method as a callback and returns the result of the __enter__ method.
pop_all(self) Preserve the context stack by transferring it to a new instance.
push(self, exit) Registers a callback with the standard __exit__ method signature. Can suppress exceptions the same way __exit__ method can. Also accepts any object with an __exit__ method (registering a call to the method instead of the object itself).
push_async_callback(self, callback, /, *args, **kwds) Registers an arbitrary coroutine function and arguments. Cannot suppress exceptions.
push_async_exit(self, exit) Registers a coroutine function with the standard __aexit__ method signature. Can suppress exceptions the same way __aexit__ method can. Also accepts any object with an __aexit__ method (registering a call to the method instead of the object itself).
A base class or mixin that enables context managers to work as decorators.
Context manager for dynamic management of a stack of exit callbacks. For example: with ExitStack() as stack: files = [stack.enter_context(open(fname)) for fname in filenames] # All opened files will automatically be closed at the end of # the with statement, even if attempts to open files later # in the list raise an exception.
callback(self, callback, /, *args, **kwds) Registers an arbitrary callback and arguments. Cannot suppress exceptions.
close(self) Immediately unwind the context stack.
enter_context(self, cm) Enters the supplied context manager. If successful, also pushes its __exit__ method as a callback and returns the result of the __enter__ method.
pop_all(self) Preserve the context stack by transferring it to a new instance.
push(self, exit) Registers a callback with the standard __exit__ method signature. Can suppress exceptions the same way __exit__ method can. Also accepts any object with an __exit__ method (registering a call to the method instead of the object itself).
Represent a PEP 585 generic type E.g. for t = list[int], t.__origin__ is list and t.__args__ is (int,).
method(function, instance) Create a bound instance method object.
Async context manager for safely finalizing an asynchronously cleaned-up resource such as an async generator, calling its ``aclose()`` method. Code like this: async with aclosing(<module>.fetch(<arguments>)) as agen: <block> is equivalent to this: agen = <module>.fetch(<arguments>) try: <block> finally: await agen.aclose()
Context to automatically close something at the end of a block. Code like this: with closing(<module>.open(<arguments>)) as f: <block> is equivalent to this: f = <module>.open(<arguments>) try: <block> finally: f.close()
deque([iterable[, maxlen]]) --> deque object A list-like sequence optimized for data accesses near its endpoints.
append(...) Add an element to the right side of the deque.
appendleft(...) Add an element to the left side of the deque.
clear(...) Remove all elements from the deque.
copy(...) Return a shallow copy of a deque.
count(...) D.count(value) -> integer -- return number of occurrences of value
extend(...) Extend the right side of the deque with elements from the iterable
extendleft(...) Extend the left side of the deque with elements from the iterable
index(...) D.index(value, [start, [stop]]) -> integer -- return first index of value. Raises ValueError if the value is not present.
insert(...) D.insert(index, object) -- insert object before index
pop(...) Remove and return the rightmost element.
popleft(...) Remove and return the leftmost element.
remove(...) D.remove(value) -- remove first occurrence of value.
reverse(...) D.reverse() -- reverse *IN PLACE*
rotate(...) Rotate the deque n steps to the right (default n=1). If n is negative, rotates left.
maxlen = <attribute 'maxlen' of 'collections.deque' objects> maximum size of a deque or None if unbounded
Context manager that does no additional processing. Used as a stand-in for a normal context manager, when a particular block of code is only sometimes used with a normal context manager: cm = optional_cm if condition else nullcontext() with cm: # Perform operation, using optional_cm if condition is True
Context manager for temporarily redirecting stderr to another file.
Context manager for temporarily redirecting stdout to another file. # How to send help() to stderr with redirect_stdout(sys.stderr): help(dir) # How to write help() to a file with open('help.txt', 'w') as f: with redirect_stdout(f): help(pow)
Context manager to suppress specified exceptions After the exception is suppressed, execution proceeds with the next statement following the with statement. with suppress(FileNotFoundError): os.remove(somefile) # Execution still resumes here if the file was already removed
asynccontextmanager(func) @asynccontextmanager decorator. Typical usage: @asynccontextmanager async def some_async_generator(<arguments>): <setup> try: yield <value> finally: <cleanup> This makes this: async with some_async_generator(<arguments>) as <variable>: <body> equivalent to this: <setup> try: <variable> = <value> <body> finally: <cleanup>
contextmanager(func) @contextmanager decorator. Typical usage: @contextmanager def some_generator(<arguments>): <setup> try: yield <value> finally: <cleanup> This makes this: with some_generator(<arguments>) as <variable>: <body> equivalent to this: <setup> try: <variable> = <value> <body> finally: <cleanup>
wraps(wrapped, assigned=('__module__', '__name__', '__qualname__', '__doc__', '__annotations__'), updated=('__dict__',)) Decorator factory to apply update_wrapper() to a wrapper function Returns a decorator that invokes update_wrapper() with the decorated function as the wrapper argument and the arguments to wraps() as the remaining arguments. Default arguments are as for update_wrapper(). This is a convenience function to simplify applying partial() to update_wrapper().