Skip to content

Commit

Permalink
update EntityPath attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
msaipraneeth committed Dec 30, 2023
1 parent c7b03fa commit d236967
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 25 deletions.
27 changes: 27 additions & 0 deletions cmem_plugin_base/dataintegration/entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,19 @@ def __init__(self, path: str,
self.is_relation = is_relation
self.is_single_value = is_single_value

def __repr__(self):
obj = {
'path': self.path, 'is_relation': self.is_relation,
'is_single_value': self.is_single_value
}
return f"EntityPath({obj})"

def __eq__(self, other):
return (isinstance(other, EntityPath)
and self.path == other.path
and self.is_relation == other.is_relation
and self.is_single_value == other.is_single_value)


class EntitySchema:
"""An entity schema.
Expand All @@ -40,6 +53,20 @@ def __init__(self,
self.path_to_root = path_to_root
self.sub_schemata = sub_schemata

def __repr__(self):
obj = {
"type_uri": self.type_uri, "paths": self.paths,
"path_to_root": self.path_to_root
}
return f"EntitySchema({obj})"

def __eq__(self, other):
return (isinstance(other, EntitySchema)
and self.type_uri == other.type_uri
and self.paths == other.paths
and self.path_to_root == other.path_to_root
and self.sub_schemata == other.sub_schemata)


class Entity:
"""An Entity can represent an instance of any given concept.
Expand Down
10 changes: 5 additions & 5 deletions cmem_plugin_base/dataintegration/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,8 +188,8 @@ def _get_schema(data: Union[dict, list]):
schema_paths.append(
EntityPath(
path=_key,
is_uri=_type in ('dict', 'list_dict'),
is_attribute=_type not in ('list', 'list_dict')
is_relation=_type in ('dict', 'list_dict'),
is_single_value=_type not in ('list', 'list_dict')
)
)
schema = EntitySchema(
Expand Down Expand Up @@ -236,15 +236,15 @@ def _get_entity(
for _ in schema.paths:
if data.get(_.path) is None:
values.append([''])
elif not _.is_uri:
elif not _.is_relation:
values.append(
[f"{data.get(_.path)}"]
if _.is_attribute
if _.is_single_value
else
[f"{_v}" for _v in data.get(_.path)]
)
else:
_data = [data.get(_.path)] if _.is_attribute else data.get(_.path)
_data = [data.get(_.path)] if _.is_single_value else data.get(_.path)
sub_entities_uri = []
for _v in _data:
sub_entity_path = f"{path_from_root}/{_.path}"
Expand Down
40 changes: 20 additions & 20 deletions tests/test_utils_build_entities_from_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ def test_single_object():
assert entities.schema == EntitySchema(
type_uri="",
paths=[
EntityPath("name", False, is_attribute=True),
EntityPath("email", False, is_attribute=True),
EntityPath("name", False, is_single_value=True),
EntityPath("email", False, is_single_value=True),
]
)

Expand Down Expand Up @@ -51,9 +51,9 @@ def test_single_object_one_level():
assert entities.schema == EntitySchema(
type_uri="",
paths=[
EntityPath("name", False, is_attribute=True),
EntityPath("email", False, is_attribute=True),
EntityPath("city", True, is_attribute=True),
EntityPath("name", False, is_single_value=True),
EntityPath("email", False, is_single_value=True),
EntityPath("city", True, is_single_value=True),
]
)
# Validate sub entities
Expand All @@ -64,8 +64,8 @@ def test_single_object_one_level():
assert _.schema == EntitySchema(
type_uri="",
paths=[
EntityPath("name", False, is_attribute=True),
EntityPath("country", False, is_attribute=True)
EntityPath("name", False, is_single_value=True),
EntityPath("country", False, is_single_value=True)
]
)

Expand Down Expand Up @@ -97,9 +97,9 @@ def test_single_object_one_level_array():
assert entities.schema == EntitySchema(
type_uri="",
paths=[
EntityPath("name", False, is_attribute=True),
EntityPath("email", False, is_attribute=True),
EntityPath("city", True, is_attribute=False),
EntityPath("name", False, is_single_value=True),
EntityPath("email", False, is_single_value=True),
EntityPath("city", True, is_single_value=False),
]
)
# Validate sub entities
Expand All @@ -110,8 +110,8 @@ def test_single_object_one_level_array():
assert _.schema == EntitySchema(
type_uri="",
paths=[
EntityPath("name", False, is_attribute=True),
EntityPath("country", False, is_attribute=True)
EntityPath("name", False, is_single_value=True),
EntityPath("country", False, is_single_value=True)
]
)

Expand Down Expand Up @@ -153,9 +153,9 @@ def test_single_object_two_level_array():
assert entities.schema == EntitySchema(
type_uri="",
paths=[
EntityPath("name", False, is_attribute=True),
EntityPath("email", False, is_attribute=True),
EntityPath("city", True, is_attribute=False),
EntityPath("name", False, is_single_value=True),
EntityPath("email", False, is_single_value=True),
EntityPath("city", True, is_single_value=False),
]
)
# Validate sub entities
Expand All @@ -167,16 +167,16 @@ def test_single_object_two_level_array():
assert city_entities.schema == EntitySchema(
type_uri="",
paths=[
EntityPath("name", False, is_attribute=True),
EntityPath("country", False, is_attribute=True),
EntityPath("geo_location", True, is_attribute=True),
EntityPath("name", False, is_single_value=True),
EntityPath("country", False, is_single_value=True),
EntityPath("geo_location", True, is_single_value=True),
]
)

assert location_entities.schema == EntitySchema(
type_uri="",
paths=[
EntityPath("lat", False, is_attribute=True),
EntityPath("long", False, is_attribute=True),
EntityPath("lat", False, is_single_value=True),
EntityPath("long", False, is_single_value=True),
]
)

0 comments on commit d236967

Please sign in to comment.