Dynamically loads a function from code represented as a string.
Project description
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
None
or{}
. 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)
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
File details
Details for the file dynamic-function-loader-0.0.3.tar.gz
.
File metadata
- Download URL: dynamic-function-loader-0.0.3.tar.gz
- Upload date:
- Size: 2.8 kB
- Tags: Source
- Uploaded using 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
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | f56382197292f66742cbfa4915b6ba3c5203adaa461d1878e3b8bb0731f1d95e |
|
MD5 | 6a0d8baa99b090e71d8903418bb18242 |
|
BLAKE2b-256 | 47102466547c46d232d2fa36a65fbe22696cb7ce01b73642f76c2b6261d290d6 |
File details
Details for the file dynamic_function_loader-0.0.3-py2.py3-none-any.whl
.
File metadata
- Download URL: dynamic_function_loader-0.0.3-py2.py3-none-any.whl
- Upload date:
- Size: 7.2 kB
- Tags: Python 2, Python 3
- Uploaded using 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
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6b8ab6b410ed1ee491ca37aa16c7dcc9ea7006f03e4a7aa986829f42c4e43942 |
|
MD5 | 404df6c8a33dfc9a090b77eb913fa0bc |
|
BLAKE2b-256 | ac08bc611c9e7d9da37889e5693de61575d9baa1465a5bb55762a2e3f37365ad |