Last active
June 1, 2021 13:50
-
-
Save abhilater/977b213190923a92f68419626ed96c58 to your computer and use it in GitHub Desktop.
This file contains 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
""" | |
config.json | |
{ | |
"stats_key1": { | |
"name": "Stats Key1", | |
"compute": { | |
"function": "calc_stats_key1", | |
"args": {"name": "test1", "age": 1} | |
} | |
}, | |
"stats_key2": { | |
"name": "Stats Key2", | |
"compute": { | |
"function": "calc_stats_key2", | |
"args": {"name": "test2", "age": 2} | |
} | |
} | |
} | |
""" | |
import json | |
class C1: | |
def __init__(self, df): | |
self.df = df | |
def calc_stats_key1(self, **kwargs): | |
# just concat | |
stats_value = kwargs["name"] + str(kwargs["age"]) | |
return {"stats_key1": stats_value} | |
def calc_stats_key2(self, **kwargs): | |
return {"stats_key2": kwargs["name"] + str(kwargs["age"])} | |
obj = C1("dataframe") | |
func_ref_to_func_map = {"calc_stats_key1" : {"obj": obj, "func": C1.calc_stats_key1}, | |
"calc_stats_key2" : {"obj": obj, "func": C1.calc_stats_key2}} | |
if __name__ == '__main__': | |
with open('./config.json') as f: | |
data = json.load(f) | |
stats = {} | |
for item in data.items(): | |
compute = item[1]["compute"] | |
function_name = compute["function"] | |
args = compute["args"] | |
function_entry = func_ref_to_func_map[function_name] | |
function_obj = function_entry["obj"] | |
function_ref = function_entry["func"] | |
stats.update(function_ref(function_obj, **args)) | |
print("Stats: ", stats) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment