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. https://travis-ci.com/jwodder/morecontext.svg?branch=master https://codecov.io/gh/jwodder/morecontext/branch/master/graph/badge.svg https://img.shields.io/pypi/pyversions/morecontext.svg MIT License

GitHub | PyPI | Issues

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

All of the context managers in morecontext are defined with contextlib.contextmanager, so they can be used as function decorators as well. All context managers 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: os.PathLike) -> ContextManager[None]

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.

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

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]

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.

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

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]

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.

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

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]

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.

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.1.0.tar.gz (6.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

morecontext-0.1.0-py3-none-any.whl (5.3 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: morecontext-0.1.0.tar.gz
  • Upload date:
  • Size: 6.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.4.2 requests/2.21.0 setuptools/49.1.2 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.6.9

File hashes

Hashes for morecontext-0.1.0.tar.gz
Algorithm Hash digest
SHA256 dd6e2ceed11af92f674e1b1f1e18182b303e87e1e00200d5a89f8122da671c8d
MD5 ae1608734a5a4309d414c0340a5d369f
BLAKE2b-256 583bb02d8ec10d8fbfa95bcf0516e0a3d36b35c5b0b8f1c59358635296a923f1

See more details on using hashes here.

File details

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

File metadata

  • Download URL: morecontext-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 5.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.4.2 requests/2.21.0 setuptools/49.1.2 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.6.9

File hashes

Hashes for morecontext-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 d87619a0af7474c3965ed64a58bd011c355192bd4840a5fd1ed3e7effdd49e27
MD5 e2a6be47b53ba78b8ce445ed05e06433
BLAKE2b-256 cfdd57c3f4c51db651d6e1b270e702790d455b5987b0efc663228e3090968f1d

See more details on using hashes here.

Supported by

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