Hi, can I use hamilton to invoke a function multip...
# hamilton-help
p
Hi, can I use hamilton to invoke a function multiple times in a sequence. Say f1->f2->f3->f1 ?
đź‘€ 1
e
Hey! So the same node can’t show up twice, but there are a variety of ways to do this effectively in a clean manner. I’ll do a very quick overview, but first — what’s the underlying goal? Transforming data, reusing functions, etc..? Some simple options for you to consider: Use helper functions • simple way to do it — hamilton functions define the concrete steps, helpers define the logic • Full visibility into the steps, quite static
Copy code
def _foo_helper(df):
    ...

def foo(df: pd.DataFrame) -> pd.DataFrame:
    return _foo_helper(df)

def bar(foo: pd.DataFrame) -> pd.DataFrame:
    ...

def foo_again(bar: pd.DataFrame) -> pd.DataFrame:
    return _foo_helper(df)
Group together • as you would without Hamilton, just a bigger hamilton node • less visibility
Copy code
def transformed_df(df: pd.DtaFrame) -> pd.DataFrame:
    df = _foo_helper(df)
    ...
    df = _foo_helper(df)
    return df
pipe_output • Nice way to chain transforms • Full visibility into the application • Uses a more complex construct
Copy code
@pipe_output(
    step(_foo_helper),
    step(_bar), # define elsewhere
    step(_foo_helper), # run aagin
    ...
)
def df():
    ... # return some dataframe
There are a few other approaches as well but I think the ones above are nice and simple
s
yes there’s several ways to reuse functions @Prasanta Midya (e.g. @subdag, @mutate, …) Note: a primary top level design decision with Hamilton is that you think in terms of “assets” or things you could create / get out. So rather than thinking what transformations I want to apply with f1->f2->f3->f1, it’s more like what are they creating: raw_data -> normalized_data -> filtered_data -> etc. or text -> final prompt -> llm response -> etc.