-
Notifications
You must be signed in to change notification settings - Fork 126
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
Modifications to enable decorating functions from another module #50
Comments
Comment by elijahbenizzy I like my (somewhat hacky) approach more -- what you suggested above. This is easy, can be copy/pasted anywhere, and doesn't require a framework change: class mock_modifier:
def __getattr__(self, attr):
return self
def __call__(*args, **kwargs):
def identity(fn):
return fn
return identity
try:
from hamilton import function_modifiers
except ImportError as e:
print('Cannot import hamilton, using a mock function modifier')
function_modifiers = mock_modifier()
@function_modifiers.config.when(foo=1)
def bar() -> int:
return 1
@function_modifiers.extract_fields({'a' : int, 'b' : int})
def baz() -> dict:
return {'a' : 1, 'b' : 1}
if __name__ == '__main__':
print(bar())
print(baz()) |
Comment by skrawcz another idea I had, was to have a dummy python module that one could import e.g. sf-hamilton-no-op ... and it would have 0 dependencies and just ensure that decorators did nothing basically. |
Comment by skrawcz
would this work for the case someone imports from the module? It wouldn't right? from hamilton.function_modifiers import config |
Comment by elijahbenizzy
Yeah it would have to be adjusted... |
Issue by skrawcz
Thursday Oct 27, 2022 at 17:06 GMT
Originally opened as stitchfix/hamilton#217
If the user wants to reuse hamilton functions without hamilton, this commit shows one way to do it -- and what would be required on Hamilton's side to support it.
Basically we'd need a convention. Not sure it's a good idea. Probably easier to instead have boilerplate code people use to try/except the import if hamilton does not exist and have the decorators be identity functions...
skrawcz included the following code: https://github.com/stitchfix/hamilton/pull/217/commits
The text was updated successfully, but these errors were encountered: