-
Notifications
You must be signed in to change notification settings - Fork 133
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ISSUE-50 using external decorators #192
Conversation
I found an interesting behavior. When the For example, this causes an error:
While this doesn't (it works as intended):
In a way, it prevents accidentally loading external_decorators.py and trying to use its empty functions, but the error is not informative for that case. On the negative, it prevents this usage pattern (which I believe would be ideal):
But this works:
The error trace:
|
Oh interesting -- I think this is pretty natural/to be expected, given that the decorators, when called, perform some validation. Not sure we want to change that, so we have a few options:
Happy to do (2) for now, especially to get this out. As this is a recipe, people can tweak it their own way. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good, some minor notes about justification/context.
@@ -0,0 +1,21 @@ | |||
# External decorators demo | |||
|
|||
This example is in response to this [GitHub issue](https://github.com/DAGWorks-Inc/hamilton/issues/50). However, it showcases more general mechanisms of how to parse "Hamilton modules" and dynamically alter source code. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Mention the overall goal/motivation first, then that it is in response to the issue. Good to have a "why".
Also worth calling it something other than "External decorators" -- E.G. "Reusing Hamilton Functions without depending on Hamilton" or something like that.
Use an existing Python module that has no Python dependencies and decorated it with Hamilton functionalities without altering the source file. | ||
|
||
# Content | ||
The script run.py will: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK, so we have two different approaches here, and they're a little confusing. Let's highlight the two use-cases separately, and make run.py
do both of them distinctly.
examples/external_decorators/run.py
Outdated
|
||
module_uuid = str(uuid.uuid4()) | ||
module_object = ModuleType(module_uuid, reader.module_docstring) | ||
sys.modules[module_uuid] = module_object |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is funky -- mind leaving some comments?
examples/external_decorators/run.py
Outdated
return module_object | ||
|
||
|
||
if __name__ == "__main__": |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Worth using click
to break this into multiple (2) comands that each to a use-case. Then they can take it in as sys.argv.
fixed typo
Clarification on version control
External decorators demo
This example is in response to this GitHub issue. However, it showcases more general mechanisms of how to parse "Hamilton modules" and dynamically alter source code.
Objective
Use an existing Python module that has no Python dependencies and decorated it with Hamilton functionalities without altering the source file.
Content
The script run.py will:
my_functions.py
, remove decorators and hamilton imports and generatemy_functions_no_hamilton.py
external_decorators.py
, which defines decorated empty functions and map them ontomy_functions_no_hamilton.py
newly_decorated.py
or load it unto a temporary Python module (never needs file writing access)Methods
Notes and limitations
Checklist