Package: tempvars
Current Development Version:Most Recent Stable Release:Info:Use Jupyter Notebook?Constantly run into problems from obsolete variables hanging around
in the namespace?tempvarscan 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.TempVarsis 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 theTempVarsfiltering 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,TempVarscan only be used at the global scope.Any attempt
to useTempVarsin non-global contexts will
result in aRuntimeError. 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 withcauldron-notebook, though
it may be less helpful there due to its step-local scoping paradigm
(shared values must be passed around viacauldron.shared).After installing withpip install tempvars, import as:>>>fromtempvarsimportTempVarsFor 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 aTempVarscontext. For example,
one could prefix all temporary variables witht_and make use
of thestartsargument:>>>foo=5>>>withTempVars(starts=['t_']):...print(foo)...t_bar=8...print(foo+t_bar)513>>>print('t_bar'indir())FalseA similar effect can be achieved with a suffix such as_tand
theendsargument.Temporary variable masking can also be introduced to existing
code in a more selective fashion via thenamesargument:>>>foo=5>>>bar=7>>>withTempVars(names=['bar']):...print(foo)...print('bar'indir())5False>>>print(foo*bar)35Setting therestoreargument toFalseinstructsTempVarsnot 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:>>>forkin['foo','bar']:...pass>>>print(k)bar>>>withTempVars(names=['k'],restore=False):...print('k'indir())False>>>print('k'indir())FalseTempVarsstores the values of variables it removes from the namespace,
should they need to be accessed. A boundwith/asstatement must be
used in order to enable this:>>>foo=5>>>withTempVars(names=['foo'])astv:...print('foo'indir())...print(tv.stored_nsvars['foo'])...foo=8...print(foo)False58>>>print(foo)5>>>print(tv.retained_tempvars['foo'])8Available onPyPI:pip install tempvars.Full documentation atRead the Docs.Source onGitHub.
Bug reports and feature requests are welcomed at theIssuespage 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-2018License: The MIT License. SeeLICENSE.txtfor full license terms.