This message was deleted.
# hamilton-help
s
This message was deleted.
s
Yes if at driver construction time one of those options is picked.
You either @config.when some set of functions, or place them in different modules, or both, and at driver construction time either pass in the right configuration, or choose the right module to pass to the driver.
g
gotcha so the downstream function uses a config element to determine which function it is linked from, rather than having a named reference to the function yeah?
s
No, we don't really have that capability (unless you use @resolve with @inject - see @Elijah Ben Izzy's comment below). For the simpler explicit route, you'd just have to have two implementations that depend on the right upstream node, and configure which one to use via configuration or module swapping.
Or if you want to run both upstream options you can introduce an intermediate node that takes in both upstream values and then uses an input parameter to choose which one to pass on.
e
Jumping in — there is a way to do this with a bit of magic using
resolve
and
inject
if you’re doing this at construction, not runtime. It would be a little ugly but if this is a common operation you can bury this in a custom decorator. Happy to share some code in a bit when I’m back at my desk!
So yeah, not 100% sure that this is what you want, but going to leave this hear for the lurkers 🙂 OK, pseudocode:
Copy code
@function_modifiers.resolve(
    when=ResolveAt.CONFIG_AVAILABLE,
    decorate_with=lambda which_item: inject(item=source(which_item)) # whatever your logic is
)
def do_something(item: T) -> T:
    return _do_something(item)
Then in the driver:
Copy code
dr = driver.Driver({"hamilton.enable_power_user_mode" : True, "which_item" : 1})
...
So, as you’re well aware, this has the potential to get ugly with bespoke logic. One trick that I’ve observed people do, is to create a custom decorator:
Copy code
def with_specified_input(available_items: List[str]):
    return function_modifiers.resolve(
        when=ResolveAt.CONFIG_AVAILABLE,
        decorate_with=lambda which_item: inject(item=source(which_item)) # whatever your logic is
    )
Note that this is one-time-use (we use the parameter, so it has to be called
which_item
, but you can easily find a way to hack in your signature to have a custom element that you pass in. This is part of a broader pattern of flexible decorators, which allows you to build some very powerful, domain-specific macros. Highly encourage you to keep a bank of the custom decorators you like, and even add some back to OS when you feel inclined!