Skip to content

Commit

Permalink
add support for lists in set_attr_via_path_accessor to be able to use…
Browse files Browse the repository at this point in the history
… any_of; adding a test (#146)
  • Loading branch information
djarecka authored Sep 5, 2024
1 parent 0c320e5 commit bdde85d
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 3 deletions.
12 changes: 9 additions & 3 deletions schemasheets/schemamaker.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,11 @@ def set_attr_via_path_accessor(obj: Union[dict, YAMLRoot], path: Union[str, List
tok = toks[0]
toks = toks[1:]
logging.debug(f"[{depth}] Setting attr {tok} / {toks} in {obj} to {value}")
if isinstance(obj, dict):
if isinstance(obj, list):
new_dict = {}
set_attr_via_path_accessor(new_dict, path, value, depth=depth)
obj.append(new_dict)
elif isinstance(obj, dict):
if not toks:
obj[tok] = value
else:
Expand Down Expand Up @@ -286,14 +290,16 @@ def add_row(self, row: Dict[str, Any], table_config: TableConfig):
raise ValueError(f'Cannot reset value for {k}, was {curr_val}, now {v}')
if cc.settings.inner_key:
obj_to_set = getattr(actual_element, cc.maps_to)
if isinstance(getattr(actual_element, cc.maps_to), list):
if isinstance(obj_to_set, list):
if '|' in v:
vs = v.split('|')
else:
vs = [v]
# not sure if this is a valid scenario, but raising error in case it is
if obj_to_set:
raise Exception(f'The case when list has multiple element set by inner_key is not implemented yet')
for v1 in vs:
set_attr_via_path_accessor(obj_to_set, cc.settings.inner_key, v1)
# setattr(actual_element, cc.maps_to, [{cc.settings.inner_key: v} for v in vs])
else:
set_attr_via_path_accessor(obj_to_set, cc.settings.inner_key, v)
# getattr(actual_element, cc.maps_to)[cc.settings.inner_key] = v
Expand Down
9 changes: 9 additions & 0 deletions tests/input/personinfo_anyof.tsv
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
record field key range any_of_range desc
> class slot identifier range any_of description
> inner_key: range
id yes string any identifier
description no string a textual description
Person n/a n/a a person,living or dead
Person id yes string identifier for a person
Person|Organization name no string full name
Person age no decimal|integer age in years
14 changes: 14 additions & 0 deletions tests/test_schemamaker.py
Original file line number Diff line number Diff line change
Expand Up @@ -282,3 +282,17 @@ def test_load_table_config():
assert 'wikidata:Q215627' in person_cls.exact_mappings
assert 'sdo:Person' in person_cls.exact_mappings


def test_classes_slots_anyof():
"""testing any_of in the slots"""
sm = SchemaMaker()
schema = sm.create_schema(os.path.join(INPUT_DIR, 'personinfo_anyof.tsv'))
logging.info(f'SCHEMA={schema}')
logging.info(f'SCHEMA.cl={schema.classes}')
logging.info(f'SCHEMA.sl={schema.slots}')
yaml_dumper.dump(schema, to_file=os.path.join(OUTPUT_DIR, 'personinfo.yaml'))
yaml = yaml_dumper.dumps(schema)
logging.info(yaml)
person_cls = schema.classes['Person']
assert person_cls.slot_usage["age"].any_of[0].range == "decimal"
assert person_cls.slot_usage["age"].any_of[1].range == "integer"

0 comments on commit bdde85d

Please sign in to comment.