Hi, I just try Hamilton today and have a question....
# hamilton-help
c
Hi, I just try Hamilton today and have a question. Is there any way to reuse a function (as node) in N times? The idea is to create a for-loop but in node-level.
👀 1
s
Yep - have a look at https://www.tryhamilton.dev/tutorial-extras/parameterization and see whether it meets your needs.
If you need it to be dynamic — then you can connect it with resolve.
y
Is it possible to dynamically generate the
parameterize
list?
s
How dynamic? Something that you pass into the driver? Or just a list you define in the module? Or?
Short, answer is yes it can be. Can point to code. (See resolve link above for example).
y
Something passed into the driver. Let’s say in the main script, we load some config parameters from yaml, then construct a list of parameters from the yaml config, then pass the list to the driver
s
yep, so
@resolve
sounds like the approach you want
👀 1
you’d want something like:
Copy code
from hamilton.function_modifiers import resolve_with_config, parameterize, source, value

@resolve_with_config(
    decorate_with=lambda config_key1: parameterize(
        **{f"node_name_{x}": {"s1": ..., "s2": ...} for x in config_key1}
    )
)
def summation(s1: pd.Series, s2: pd.Series) -> pd.Series:
    return s1 + s2
then in the driver:
Copy code
from hamilton import settings
_config = {settings.ENABLE_POWER_USER_MODE:True}

# this should match the name in the lambda
_config["config_key1"] = ["sub_metric_1", "sub_metric_2"]

# Then in the driver building pass in the configuration:
.with_config(_config)
note: you can break the inside of the decorator out into functions if it gets complex/harder to read.
y
wow that’s cool 👍
🙂 1