Created
March 26, 2020 01:14
-
-
Save eigenfoo/5e69bba2ab7ec6d6a2f53c13cbfd7b48 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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