dynamic-function-loader
Installation
pip install dynamic-function-loader
Overview
dynamic-function-loader allows dynamically loading a Python function into the current execution
environment from a string.
Usage
import dynamic_function_loader
f = dynamic_function_loader.load("def foo():\n return 'bar'")
f()
Globals
All functions (and other Python objects) are provided a two symbol tables upon creation; globals and locals.
dynamic_function_loader.load takes an optional argument called globals_dict.
This is used during function creation to provide the loaded function with it's
globals.
There are several use cases regarding this:
-
You wish to load the function as-if it was defined within your loading script: In this case, simply pass in
globals(). The loadedfunction will have access to the loading script's symbol table. -
You wish to load the function as-if it was defined in a seperate module: In this case, either do not pass a value or pass in
Noneor{}. The loaded function will not have access to the loading script's symbol table. -
You wish to load the function with some of the symbol table from the loading script: In this case, copy the result from
globals()in the loading script and pare the items from the script that you do not wish the loaded function to have access to and pass in the result. Be careful! Python will let you pare down to an empty dict ({}), but your loaded function will have no access to even the built-ins of Python. Generally, if you do this, it should at least contain those items that begin and end with__. For example:
func_globals = {}
# You must convert to a list because every
# variable you declare alters the symbol table
for k, v in list(globals().items()):
if k.startswith("__") and k.endswith("__"):
func_globals[k] = v
Note
All Python packages used by the imported function must be loaded within the function itself if the globals_dict
that you pass in does not contain that module already (options 2 and possibly 3 from above).
For example, if re was not already in the global symbol table you would have to import it as follows:
def foo(regex):
import re
return re.compile(regex)
Release files for dynamic-function-loader 0.0.3
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| dynamic-function-loader-0.0.3.tar.gz | 2.8 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| dynamic_function_loader-0.0.3-py2.py3-none-any.whl | Python 2, Python 3 | none | any | Details |
Total release size: 10.0 kB
Release files / dynamic-function-loader-0.0.3.tar.gz
| Download URL | dynamic-function-loader-0.0.3.tar.gz |
|---|---|
| Size | 2.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
f56382197292f66742cbfa4915b6ba3c5203adaa461d1878e3b8bb0731f1d95e
|
|
BLAKE2b-256 checksum How to use checksums |
47102466547c46d232d2fa36a65fbe22696cb7ce01b73642f76c2b6261d290d6
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.4.1 importlib_metadata/3.10.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.7.10
|
Release files / dynamic_function_loader-0.0.3-py2.py3-none-any.whl
| Download URL | dynamic_function_loader-0.0.3-py2.py3-none-any.whl |
|---|---|
| Size | 7.2 kB |
| Tags | Python 2 Python 3 |
|
SHA-256 checksum How to use checksums |
6b8ab6b410ed1ee491ca37aa16c7dcc9ea7006f03e4a7aa986829f42c4e43942
|
|
BLAKE2b-256 checksum How to use checksums |
ac08bc611c9e7d9da37889e5693de61575d9baa1465a5bb55762a2e3f37365ad
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.4.1 importlib_metadata/3.10.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.7.10
|