Skip to main content

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:

  1. 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.

  2. 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.

  3. 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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

dynamic-function-loader-0.0.3.tar.gz (2.8 kB view details)

Uploaded Source

Built Distribution

dynamic_function_loader-0.0.3-py2.py3-none-any.whl (7.2 kB view details)

Uploaded Python 2 Python 3

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

Hashes for dynamic-function-loader-0.0.3.tar.gz
Algorithm Hash digest
SHA256 f56382197292f66742cbfa4915b6ba3c5203adaa461d1878e3b8bb0731f1d95e
MD5 6a0d8baa99b090e71d8903418bb18242
BLAKE2b-256 47102466547c46d232d2fa36a65fbe22696cb7ce01b73642f76c2b6261d290d6

See more details on using hashes here.

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

Hashes for dynamic_function_loader-0.0.3-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 6b8ab6b410ed1ee491ca37aa16c7dcc9ea7006f03e4a7aa986829f42c4e43942
MD5 404df6c8a33dfc9a090b77eb913fa0bc
BLAKE2b-256 ac08bc611c9e7d9da37889e5693de61575d9baa1465a5bb55762a2e3f37365ad

See more details on using hashes here.

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