Skip to content

Instantly share code, notes, and snippets.

@eigenfoo
Created March 26, 2020 01:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eigenfoo/5e69bba2ab7ec6d6a2f53c13cbfd7b48 to your computer and use it in GitHub Desktop.
Save eigenfoo/5e69bba2ab7ec6d6a2f53c13cbfd7b48 to your computer and use it in GitHub Desktop.
class Model:
""" pm.Model decorator. """
def __init__(self, func):
self.func = func
# Introspect wrapped function, instead of the decorator class.
functools.update_wrapper(self, func)
# Uncompile wrapped function.
uncompiled = uncompile(func.__code__)
# Parse AST and modify it.
tree = parse_snippet(*uncompiled)
tree = FunctionToGenerator().visit(tree)
uncompiled[0] = tree
# Recompile wrapped function.
self.recompiled = recompile(*uncompiled)
# Execute recompiled code (defines `_pm_compiled_model_generator`)
# in the locals() namespace and assign it to an attribute.
# Refer to http://lucumr.pocoo.org/2011/2/1/exec-in-python/
exec(self.recompiled, None, locals())
self.model_generator = locals()["_pm_compiled_model_generator"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment