Skip to main content

Python package for preventing use of variables from global scope.

Project description

safescope

Build Status PyPI PyPI - Python Version License

safescope is a small python package useful for development in jupyter notebooks. The goal is to prevent functions from using global variables and instead 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 writing the function in a file side_scope.py and importing this function to the notebook. Hence, functions decorated with @safescope will not have access to variables declared in the notebook environment.

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 imports are added to both the main scope and the side_scope, making them available for the 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

safescope can either be installed from pypi with pip/pip3:

pip install safescope

or from github with:

pip install git+git://github.com/havakv/safescope.git

or from source by cloning the repo:

git clone https://github.com/havakv/safescope.git
cd safescope
python setup.py install

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

safescope-0.2.6.tar.gz (4.3 kB view hashes)

Uploaded Source

Built Distribution

safescope-0.2.6-py2.py3-none-any.whl (4.6 kB view hashes)

Uploaded Python 2 Python 3

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