ju recipes #3
thorwhalen
started this conversation in
General
Replies: 1 comment
-
Gather the models for a store of json valuesfrom ju import valid_models, truncate_dict_values
import re
# lambda to replace all \W with _
sanitize = lambda s: re.sub(r'\W', '_', s)
def gather_models(json_store, max_iterations=None):
"""Gather models infered from the values of a json_store.
"""
models = set()
model_is_new = lambda v: not next(valid_models(v, models), None)
def mk_new_model(k, reduced_v):
new_model = create_pydantic_model(f"Model_{sanitize(k)}", reduced_v)
models.add(new_model)
return {
'model': new_model,
'key_of_example_data': k,
'reduced_example_data': reduced_v
}
items = iter(json_store.items())
k, v = next(items)
reduced_v = truncate_dict_values(v)
yield mk_new_model(k, v)
for i, (k, v) in enumerate(items, 1):
# print(f"{i}, {k=}")
reduced_v = truncate_dict_values(v)
if model_is_new(reduced_v):
yield mk_new_model(k, reduced_v)
if max_iterations and i >= max_iterations:
break |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Recipes using ju tools.
Beta Was this translation helpful? Give feedback.
All reactions