Skip to main content

Context managers for changing directory, setting attributes/envvars, etc.

Project description

Project Status: Active — The project has reached a stable, usable state and is being actively developed. CI Status https://codecov.io/gh/jwodder/morecontext/branch/master/graph/badge.svg https://img.shields.io/pypi/pyversions/morecontext.svg MIT License

GitHub | PyPI | Issues | Changelog

morecontext provides context managers for some common minor operations: specifically, changing the current working directory, an object’s attribute, a dict field, or an environment variable and then setting it back afterwards. Sure, it’s easy enough to implement these on your own, but why bother doing that over & over again when you can let this package do it for you once?

Type annotated! Fully tested!

Installation

morecontext requires Python 3.6 or higher. Just use pip for Python 3 (You have pip, right?) to install morecontext:

python3 -m pip install morecontext

Examples

>>> import os
>>> import morecontext
>>> os.getcwd()
'/some/dir'
>>> with morecontext.dirchanged('/some/other/dir'):
...     # Now we're in /some/other/dir
...     os.getcwd()
...
'/some/other/dir'
>>> # Out of the `with`, back to /some/dir
>>> os.getcwd()
'/some/dir'
>>> d = {"foo": 42}
>>> with morecontext.itemset(d, "foo", "bar"):
...     # d["foo"] == "bar" in here
...     d["foo"]
...     # If we change d["foo"] in here, it'll still be set back to 42 on exit
...     d["foo"] = 3.14
...
'bar'
>>> # Out of the `with`, it's back to 42
>>> d["foo"]
42

API

Functions

All of the following context manager functions are defined with contextlib.contextmanager, so they can be used as function decorators as well. They also all return None on entry, so there’s no point in writing “with dirchanged(path) as foo:”; just do “with dirchanged(path):”.

These functions are not thread-safe.

dirchanged(dirpath: Union[str, bytes, os.PathLike]) -> ContextManager[None]

Temporarily change the current working directory.

dirchanged(dirpath) returns a context manager. On entry, it stores the current working directory path and then changes the current directory to dirpath. On exit, it changes the current directory back to the stored path.

dirrollback() -> ContextManager[None]

Save & restore the current working directory.

dirrollback() returns a context manager that stores the current working directory on entry and changes back to that directory on exit.

attrset(obj: Any, name: str, value: Any) -> ContextManager[None]

Temporarily change the value of an object’s attribute.

attrset(obj, name, value) returns a context manager. On entry, it stores the current value of the attribute of obj with name name, and then it sets that attribute to value. On exit, it sets the attribute back to the stored value.

If the given attribute is unset on entry, the context manager will unset it on exit.

attrdel(obj: Any, name: str) -> ContextManager[None]

Temporarily unset an object’s attribute.

attrdel(obj, name) returns a context manager. On entry, it stores the current value of the attribute of obj with name name, and then it unsets that attribute. On exit, it sets the attribute back to the stored value.

If the given attribute is unset on entry, the context manager will unset it on exit.

attrrollback(obj: Any, name: str, copy: bool = False, deepcopy: bool = False) -> ContextManager[None]

Save & restore the value of an object’s attribute.

attrrollback(obj, name) returns a context manager that stores the value of the attribute of obj with name name on entry and sets the attribute back to that value on exit. If the given attribute is unset on entry, the context manager will unset it on exit.

If copy is true, a shallow copy of the attribute will be saved & restored. If deepcopy is true, a deep copy of the attribute will be saved & restored. If both options are true, deepcopy takes precedence.

itemset(d: MutableMapping[K,V], key: K, value: V) -> ContextManager[None]

Temporarily change the value of a mapping’s entry.

itemset(d, key, value) returns a context manager. On entry, it stores the current value of d[key], and then it sets that field to value. On exit, it sets the field back to the stored value.

If the given field is unset on entry, the context manager will unset it on exit.

itemdel(d: MutableMapping[K, Any], key: K) -> ContextManager[None]

Temporarily unset a mapping’s entry.

itemdel(d, key) returns a context manager. On entry, it stores the current value of d[key], and then it unsets that field. On exit, it sets the field back to the stored value.

If the given field is unset on entry, the context manager will unset it on exit.

itemrollback(d: MutableMapping[K, Any], key: K, copy: bool = False, deepcopy: bool = False) -> ContextManager[None]

Save & restore the value of a mapping’s entry.

itemrollback(d, key) returns a context manager that stores the value of d[key] on entry and sets the field back to that value on exit. If the given field is unset on entry, the context manager will unset it on exit.

If copy is true, a shallow copy of the field will be saved & restored. If deepcopy is true, a deep copy of the field will be saved & restored. If both options are true, deepcopy takes precedence.

envset(name: str, value: str) -> ContextManager[None]

Temporarily set an environment variable.

envset(name, value) returns a context manager. On entry, it stores the current value of the environment variable name, and then it sets that environment variable to value. On exit, it sets the environment variable back to the stored value.

If the given environment variable is unset on entry, the context manager will unset it on exit.

envdel(name: str) -> ContextManager[None]

Temporarily unset an environment variable.

envdel(name) returns a context manager. On entry, it stores the current value of the environment variable name, and then it unsets that environment variable. On exit, it sets the environment variable back to the stored value.

If the given environment variable is unset on entry, the context manager will unset it on exit.

envrollback(name: str) -> ContextManager[None]

Save & restore the value of an environment variable.

envrollback(name) returns a context manager that stores the value of the environment variable name on entry and sets the environment variable back to that value on exit. If the given environment variable is unset on entry, the context manager will unset it on exit.

additem(lst: MutableSequence[T], value: T, prepend: bool = False) -> ContextManager[None]

Temporarily add a value to a sequence.

additem(lst, value) returns a context manager that appends value to the sequence lst on entry and removes the last item (if any) in lst that equals value on exit.

If prepend is true, value is instead prepended to lst on entry, and the first item in lst that equals value is removed on exit.

Classes

class OpenClosable:
    def open(self) -> None:
        ...

    def close(self) -> None:
        ...

A base class for creating simple reentrant context managers. OpenClosable defines __enter__ and __exit__ methods that keep track of the number of nested with statements in effect and call the instance’s open() and close() methods when entering & exiting the outermost with.

Subclasses should override open() and/or close() with the desired code to run on entering & exiting the outermost with; the default open() and close() methods defined by OpenClosable do nothing.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

morecontext-0.6.0.tar.gz (12.5 kB view details)

Uploaded Source

Built Distribution

morecontext-0.6.0-py3-none-any.whl (7.4 kB view details)

Uploaded Python 3

File details

Details for the file morecontext-0.6.0.tar.gz.

File metadata

  • Download URL: morecontext-0.6.0.tar.gz
  • Upload date:
  • Size: 12.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.6.0 importlib_metadata/4.8.2 pkginfo/1.8.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.9

File hashes

Hashes for morecontext-0.6.0.tar.gz
Algorithm Hash digest
SHA256 fffedb3f853b9fcb81296ebd650d8f774c84d604af8b09a6b531a4cd46a5dc35
MD5 b2a6e70391cb19309d309a32ab931c67
BLAKE2b-256 4406de0f22b2ed7d5744d1eebe81b150e25545fe91dc839f4263ab73137fa61c

See more details on using hashes here.

File details

Details for the file morecontext-0.6.0-py3-none-any.whl.

File metadata

  • Download URL: morecontext-0.6.0-py3-none-any.whl
  • Upload date:
  • Size: 7.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.6.0 importlib_metadata/4.8.2 pkginfo/1.8.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.9

File hashes

Hashes for morecontext-0.6.0-py3-none-any.whl
Algorithm Hash digest
SHA256 7246495a924a9999db1c27f49d5739cf7c7b77404e24a1429185e1e6160e6364
MD5 ba89ec710afb42855b854dfcf2dad5df
BLAKE2b-256 1f43692c55383ffe2333bbee52337848b687414e75bca5249f379beb0d0bcd90

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page