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 hashes)

Uploaded Source

Built Distribution

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

Uploaded Python 2 Python 3

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