Python package for preventing use of variables from global scope.
Project description
safescope
safescope is a small python package useful for development in jupyter notebooks.
The goal is to prevent function from using global variables, and in stead raise a NameError
.
This is achieved by mimicking a module, and define all functions in that module (named side_scope
).
Example
The main part of safescope is the decorator @safescope
. This mimics actually writing the function in a file side_scope.py
, and importing the function to the notebook. Hence, functions decorated with @safescope
will not have access to variables declared in the notebook.
In the example below, only foo(1)
will execute, as bar(1)
returns a NameError
.
from safescope import safescope
x = 9
def foo(y):
return x + y
@safescope
def bar(y):
return x + y
foo(1) # Returns 10
bar(1) # Raise "NameError: name 'x' is not defined"
By using Imports
, the import are added to both the main scope and the side_scope
, and can therefore be used by functions decorated with @safescope
.
from safescope import safescope, Imports
with Imports():
import numpy as np
@safescope
def arange(n):
return np.arange(n)
arange(4) # Returns array([0, 1, 2, 3])
Installation
Next, safescope can be installed from pypi:
pip3 install -U safescope
or from github with pip:
pip3 install git+git://github.com/havakv/safescope.git
or by cloning the repo:
git clone https://github.com/havakv/safescope.git
cd safescope
python setup.py install
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for safescope-0.2.5-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | bb588da1b1fb3eb199bf0f1d25db44955ae5a2053e578a77ff6b6fb655f06058 |
|
MD5 | 21c15c462430e4fd7de12d1183a4f246 |
|
BLAKE2b-256 | 1c1602d2dee5e76f02382c146cb110c5875281f2dd6020d237e2f4f184303d49 |