(no title)
aston | 3 years ago
def encapsulate(mod):
import types
out = types.SimpleNamespace()
def replace_global_scope(f):
# via https://stackoverflow.com/a/1144561, but tweaked for py3
return types.FunctionType(
f.__code__,
out.__dict__,
f.__name__,
f.__defaults__,
f.__closure__,
)
for name in dir(mod):
val = getattr(mod, name)
if callable(val):
val = replace_global_scope(val)
setattr(out, name, val)
return out
Starlevel001|3 years ago
TekMol|3 years ago
https://www.online-python.com/sTLH5nYGvk
abeyer|3 years ago