Skip to main content

Context manager for handling temporary variables in Jupyter Notebook, IPython, etc.

Project description

Current Development Version:

https://travis-ci.org/bskinn/tempvars.svg?branch=dev https://codecov.io/gh/bskinn/tempvars/branch/dev/graph/badge.svg

Most Recent Stable Release:

https://img.shields.io/pypi/v/tempvars.svg https://img.shields.io/pypi/pyversions/tempvars.svg

Info:

https://img.shields.io/readthedocs/tempvars/v1.0.1.svg https://img.shields.io/github/license/mashape/apistatus.svg https://img.shields.io/badge/code%20style-black-000000.svg

Use Jupyter Notebook?

Constantly run into problems from obsolete variables hanging around in the namespace?

tempvars can help.

Developing in Jupyter notebooks can sometimes be frustrating. For example, it’s aggravating to debug a worksheet for half an hour, only to discover that a carried-over variable name was hanging around in the notebook namespace and causing problems. Or, to open a notebook that “worked fine” the last time it was used, but only because of random, obsolete variables that happened to be lingering in the namespace. Wrapping notebook code in functions/classes is an effective way of avoiding these sorts of problems, but it’s rarely effective or efficient to do this in the initial exploratory phase of in-notebook development.

TempVars is a context manager that helps to avoid these pitfalls by clearing selected identifiers from the namespace for the duration of its scope, then restoring them afterwards (or not, if desired). Further, any variables created within the managed context that match the TempVars filtering criteria are removed from the namespace upon exiting, ensuring these values do not spuriously contribute to following code. For convenience, all variables removed from the namespace at entry and exit are stored for later reference (see example code below).

Due to the way Python handles non-global scopes, TempVars can only be used at the global scope. Any attempt to use TempVars in non-global contexts will result in a RuntimeError. Viable use-cases include Jupyter notebooks, the IPython and basic Python REPLs, and the outermost scope of executed and imported modules. Preliminary testing indicates it also works with cauldron-notebook, though it may be less helpful there due to its step-local scoping paradigm (shared values must be passed around via cauldron.shared).


After installing with pip install tempvars, import as:

>>> from tempvars import TempVars

For typical use in a Jupyter notebook cell, the recommended approach is to pick a marker to use on all variables that are to be temporary, and enclose the entire cell in a TempVars context. For example, one could prefix all temporary variables with t_ and make use of the starts argument:

>>> foo = 5
>>> with TempVars(starts=['t_']):
...     print(foo)
...     t_bar = 8
...     print(foo + t_bar)
5
13
>>> print('t_bar' in dir())
False

A similar effect can be achieved with a suffix such as _t and the ends argument.

Temporary variable masking can also be introduced to existing code in a more selective fashion via the names argument:

>>> foo = 5
>>> bar = 7
>>> with TempVars(names=['bar']):
...     print(foo)
...     print('bar' in dir())
5
False
>>> print(foo * bar)
35

Setting the restore argument to False instructs TempVars not to restore any masked variables to the namespace after its context exits. This is potentially useful to avoid carryover of common helper variables (arr, df, i, etc.) to downstream cells that may have been created earlier in a notebook:

>>> for k in ['foo', 'bar']:
...     pass
>>> print(k)
bar
>>> with TempVars(names=['k'], restore=False):
...     print('k' in dir())
False
>>> print('k' in dir())
False

TempVars stores the values of variables it removes from the namespace, should they need to be accessed. A bound with/as statement must be used in order to enable this:

>>> foo = 5
>>> with TempVars(names=['foo']) as tv:
...     print('foo' in dir())
...     print(tv.stored_nsvars['foo'])
...     foo = 8
...     print(foo)
False
5
8
>>> print(foo)
5
>>> print(tv.retained_tempvars['foo'])
8

Available on PyPI: pip install tempvars.

Full documentation at Read the Docs.

Source on GitHub. Bug reports and feature requests are welcomed at the Issues page there. If you like the idea of an enhancement already in the Issues list, please comment to say so; it’ll help with prioritization.

Copyright (c) Brian Skinn 2017-2018

License: The MIT License. See LICENSE.txt for full license terms.

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

tempvars-1.0.1.tar.gz (9.1 kB view details)

Uploaded Source

Built Distribution

tempvars-1.0.1-py3-none-any.whl (8.0 kB view details)

Uploaded Python 3

File details

Details for the file tempvars-1.0.1.tar.gz.

File metadata

  • Download URL: tempvars-1.0.1.tar.gz
  • Upload date:
  • Size: 9.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.20.1 setuptools/40.6.2 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.6.6

File hashes

Hashes for tempvars-1.0.1.tar.gz
Algorithm Hash digest
SHA256 14ebf82073098134dfe870d86e547471e122f5eed238501f46d0c36f93040852
MD5 02f88fc3061a06d492d779d1bf0ea259
BLAKE2b-256 2d4432295c80c0d59ea11d82ca30ae40f5021551ed72eed7aa64c9a84b6235a0

See more details on using hashes here.

File details

Details for the file tempvars-1.0.1-py3-none-any.whl.

File metadata

  • Download URL: tempvars-1.0.1-py3-none-any.whl
  • Upload date:
  • Size: 8.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.20.1 setuptools/40.6.2 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.6.6

File hashes

Hashes for tempvars-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 1ac7b3c2c6a77b57997c61a86b57a0acfbb68ce969a0187a020adbea8501ef58
MD5 5ce5cc0342b6c601d961e666766896c9
BLAKE2b-256 f7fdc7bcf9e587fe2edf7df533f2c02df7a910f0a7f975aa90c15e6073a7a858

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