This message was deleted.
# hamilton-help
s
This message was deleted.
e
Good Q -- to use decorators easily you have to decorate the function, meaning that you'll have to import hamilton in that module. Our goal is to make hamilton extremely lightweight -- not sure whether you're using pandas or not, but here are the requirements: https://github.com/stitchfix/hamilton/blob/main/requirements.txt. So I'd think of it less as "coupled" -- you can use the functions outside of Hamilton, and none of the decorators mess with the way the functions operate. That said, if you're super set on not having it as a dependency (I can respect the desire towards having very low-dependency modules), there are a host of other options you could go for (including making a "bridge" of sorts or decorating the decorator with a try/catch of import... 😆 ).
z
I guess I could have a
try / catch
for the
import hamilton
and in the catch make the decorators as no-ops 😛
s
@Zouhair Mahboubi yeah not a bad hack to try
e
Yep that was the degree of evil that I was hinting at 😂 e.g. you could create a mock function_modifiers whose getattr returns a no-op decorator…. Might even be a nice part of the framework to include? that said our goal is really to make hamilton lightweight so having it as a dependency is not the end of the world…
s
I might have another way but need to prototype it
e
OK I'm both proud and ashamed of this code:
Copy code
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())
Hoping @Stefan Krawczyk's cleaner approach is less clever 😆
👍 1
s
Here’s what I’ve prototyped — requires a change to Hamilton though and coming up with a convention — https://github.com/stitchfix/hamilton/pull/217
but yeah you’d need to manage two modules — which maybe worse than just managing the import statement…