Skip to content

Instantly share code, notes, and snippets.

@ax3l
Last active May 8, 2024 22:23
Show Gist options
  • Save ax3l/59d92c6e1edefcef85ac2540eb056da3 to your computer and use it in GitHub Desktop.
Save ax3l/59d92c6e1edefcef85ac2540eb056da3 to your computer and use it in GitHub Desktop.
Useful Noglobal in Python
# License:
# I hereby state this snippet is below "threshold of originality" where applicable (public domain).
#
# Otherwise, since initially posted on Stackoverflow, use as:
# CC-BY-SA 3.0 skyking, Glenn Maynard, Axel Huebl
# http://stackoverflow.com/a/31047259/2719194
# http://stackoverflow.com/a/4858123/2719194
import types
def imports():
for name, val in globals().items():
# module imports
if isinstance(val, types.ModuleType):
yield name, val
# functions / callables
if hasattr(val, '__call__'):
yield name, val
noglobal = lambda fn: types.FunctionType(fn.__code__, dict(imports()))
# usage example
import numpy as np
import matplotlib.pyplot as plt
import h5py
a = 1
@noglobal
def f(b):
h5py.is_hdf5("a.tmp")
# only np. shall be known, not numpy.
np.arange(4)
#numpy.arange(4)
# this var access shall break when called
#print(a)
print(b)
f(2)
@bilzard
Copy link

bilzard commented Nov 19, 2022

Thank you for sharing this code.
I made noglobal package from this snippet, and published to PyPI.
You seems to hold CC-BY-SA 3.0 license to this snippet, but the FAQ page1 says creative commons license is not suitable to software. So I published this package with MIT license, and name you in attribution section on README.
If you mind this, please let me know.

Footnotes

  1. https://creativecommons.org/faq/#can-i-apply-a-creative-commons-license-to-software

@ax3l
Copy link
Author

ax3l commented Nov 19, 2022

Hi,

Cool, thanks.

As I stated in line 2, I think my snippet is below "threshold of originality" where applicable (public domain). It is based on ideas of the two linked SO posts, which are all by default under CC-BY-SA. I saw that you credited these as well, thanks 👍

As far as it concerns my content, applying the MIT license is fine. Thanks again!

@bilzard
Copy link

bilzard commented Nov 20, 2022

All right. Thanks.

@bilzard
Copy link

bilzard commented Nov 20, 2022

And thank you for the star.

@ax3l
Copy link
Author

ax3l commented Nov 20, 2022

Of course, and thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment