Last active
July 19, 2024 17:16
-
-
Save skrawcz/83a77343b1a9774b77fabde35b00ce07 to your computer and use it in GitHub Desktop.
Shows how to potentially get around boilerplate config loading code by parameterizing it
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# %%cell_to_module config_plate --display | |
from hamilton.function_modifiers import parameterize, source, value | |
class TablePaths: | |
pass # your code here | |
table_names = ["vendor", "predictions"] # update this | |
@parameterize( | |
**{ | |
f"{name}_paths": {"table_name": value(name)} | |
for name in table_names | |
} | |
) | |
def table_paths(table_paths: TablePaths, table_name: str) -> dict: | |
_table = getattr(table_paths, table_name) | |
return { | |
f"{table_name}_catalog": _table.catalog, | |
f"{table_name}_schema": _table.schema, | |
f"{table_name}_table": _table.table, | |
} | |
@parameterize( | |
**{ | |
f"{name}_{part}": {"general_dict": source(f"{name}_paths"), "field_name": value(part)} | |
for name in table_names | |
for part in ["catalog", "schema", "table"] | |
} | |
) | |
def parameterized_extract_fields(general_dict: dict, field_name: str) -> str: | |
return general_dict[field_name] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment