Skip to content

Instantly share code, notes, and snippets.

@skrawcz
Last active October 26, 2022 22:44
Show Gist options
  • Save skrawcz/18c5ce2347f7dce274d83043bc33f982 to your computer and use it in GitHub Desktop.
Save skrawcz/18c5ce2347f7dce274d83043bc33f982 to your computer and use it in GitHub Desktop.
Custom Result Builder Example
from typing import Tuple
from hamilton import base
class PandasDFWithDebugResultBuilder(base.ResultMixin):
"""This class is an example to show how you can extend the result building functionality of Hamilton.
This result builder returns a dataframe, and dictionary of outputs that we don't want to make into a dataframe,
but are useful for debugging (for example). Caveat: this wont work for ray, dask, or spark usage without some tweaks.
Example Usage::
from hamilton import base
from hamilton import driver
import my_functions # import where your functions live
import my_builder # were the custom builder lives
config = {...}
modules = [my_functions]
outputs_requested = [...]
debug_outputs = [...] # name of the debug outputs you want
builder = my_builder.PandasDFWithDebugResultBuilder(debug_outputs)
adapter = base.SimplePythonGraphAdapter(builder)
dr = driver.Driver(config, *modules, adapter=adapter)
df, debug_dict = dr.execute(outputs_requested + debug_outputs)
"""
def __init__(self, debug_outputs: List[str]):
self.debug_outputs = debug_outputs
def build_result(self, **outputs: Dict[str, Any]) -> Tuple[pd.DataFrame, Dict[str, Any]]:
"""This builds a resulting dataframe while also returning a set of 'debug' outputs."""
debug_dict: dict = {key: outputs[key] for key in self.debug_outputs}
dataframe_dict = {key: value for key, value in outputs.items()
if key not in debug_dict}
dataframe = pd.DataFrame(dataframe_dict)
return dataframe, debug_dict
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment