Skip to content

Commit aa9900d

Browse files
committed
feat: implement Converter class.
1 parent 280dc36 commit aa9900d

File tree

2 files changed

+75
-3
lines changed

2 files changed

+75
-3
lines changed

interactions/ext/converter.py

Lines changed: 64 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
from typing import List
2+
3+
14
class Converter:
25
"""
36
A class representing the "conversion" or consistent mapping between
@@ -7,10 +10,68 @@ class Converter:
710
__slots__ = ("_obj1", "_obj2")
811

912
def __init__(self, __obj1: object, __obj2: object) -> None:
13+
"""
14+
:param __obj1: The first object to be converted.
15+
:type __obj1: object
16+
:param __obj2: The second object to be converted.
17+
:type __obj2: object
18+
"""
1019
self._obj1 = __obj1
1120
self._obj2 = __obj2
1221
self._map_attr()
1322

14-
def _map_attr(self) -> None:
15-
"""Maps the attributes of the two objects for conversion."""
16-
...
23+
def __repr__(self) -> str:
24+
return self._obj2
25+
26+
def _map_attr(self) -> dict:
27+
"""
28+
Maps the attributes between the models for conversion reference.
29+
30+
:return: A dictionary of attributes mapped.
31+
:rtype: dict
32+
"""
33+
for attr1 in self._obj1.keys():
34+
for attr2 in self._obj2.keys():
35+
self.__dict__.update({attr1: attr2})
36+
37+
return self.__dict__
38+
39+
def get_attr(self, attr: str) -> str:
40+
"""
41+
Gets a mapped attribute.
42+
43+
:param attr: The attribute to get.
44+
:type attr: str
45+
:return: The mapped attribute.
46+
:rtype: str
47+
"""
48+
return self.__dict__.get(attr)
49+
50+
def get_attrs(self) -> List[str]:
51+
"""
52+
Gets a list of mapped attributes.
53+
54+
:return: The list of mapped attributes.
55+
:rtype: list
56+
"""
57+
return self.__dict__
58+
59+
@property
60+
def ref(self) -> object:
61+
"""
62+
Gets the "referenced" model, or first.
63+
64+
:return: The referenced model.
65+
:rtype: object
66+
"""
67+
return self._obj1
68+
69+
@property
70+
def diff(self) -> List[dict]:
71+
"""
72+
Gets a list of keys and values that the models don't share in common.
73+
74+
:return: A list of dictionaries
75+
:rtype: List[dict]
76+
"""
77+
return [{key: val} for key, val in self._obj2 if key not in self._obj1]

interactions/ext/converter.pyi

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,14 @@
1+
from typing import List
2+
13
class Converter:
24
_obj1: object
35
_obj2: object
6+
def __init__(self, __obj1: object, __obj2: object) -> None: ...
7+
def __repr__(self) -> str: ...
8+
def _map_attr(self) -> dict: ...
9+
def get_attr(self, attr: str) -> str: ...
10+
def get_attrs(self) -> List[str]: ...
11+
@property
12+
def ref(self) -> object: ...
13+
@property
14+
def diff(self) -> List[dict]: ...

0 commit comments

Comments
 (0)