Skip to content
This repository has been archived by the owner on Jul 3, 2023. It is now read-only.

Modifications to enable decorating functions from another module #217

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

skrawcz
Copy link
Collaborator

@skrawcz skrawcz commented Oct 27, 2022

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...

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 skrawcz requested a review from elijahbenizzy October 27, 2022 17:44
@elijahbenizzy
Copy link
Collaborator

elijahbenizzy commented Oct 31, 2022

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

@skrawcz
Copy link
Collaborator Author

skrawcz commented Jan 25, 2023

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.

@skrawcz skrawcz changed the title Example showing what we would need to modify to enable user question Modifications to enable decorating functions from another module Jan 27, 2023
@skrawcz
Copy link
Collaborator Author

skrawcz commented Jan 27, 2023

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

@elijahbenizzy
Copy link
Collaborator

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...

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants