Skip to content
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

Closed
HamiltonRepoMigrationBot opened this issue Feb 26, 2023 · 4 comments

Comments

@HamiltonRepoMigrationBot
Copy link
Collaborator

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

@HamiltonRepoMigrationBot
Copy link
Collaborator Author

Comment by elijahbenizzy
Monday Oct 31, 2022 at 15:28 GMT


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

@HamiltonRepoMigrationBot
Copy link
Collaborator Author

Comment by skrawcz
Wednesday Jan 25, 2023 at 01:16 GMT


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.

@HamiltonRepoMigrationBot
Copy link
Collaborator Author

Comment by skrawcz
Friday Jan 27, 2023 at 22:44 GMT


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

would this work for the case someone imports from the module? It wouldn't right?
E.g.

from hamilton.function_modifiers import config

@HamiltonRepoMigrationBot
Copy link
Collaborator Author

Comment by elijahbenizzy
Friday Jan 27, 2023 at 23:03 GMT


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

would this work for the case someone imports from the module? It wouldn't right? E.g.

from hamilton.function_modifiers import config

Yeah it would have to be adjusted...

@zilto zilto closed this as not planned Won't fix, can't repro, duplicate, stale May 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants