Skip to content

Commit

Permalink
feat: implement Converter class.
Browse files Browse the repository at this point in the history
  • Loading branch information
i0bs committed Feb 25, 2022
1 parent ce26388 commit c1d9a2b
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 3 deletions.
67 changes: 64 additions & 3 deletions interactions/ext/converter.py
@@ -1,3 +1,6 @@
from typing import List


class Converter:
"""
A class representing the "conversion" or consistent mapping between
Expand All @@ -7,10 +10,68 @@ class Converter:
__slots__ = ("_obj1", "_obj2")

def __init__(self, __obj1: object, __obj2: object) -> None:
"""
:param __obj1: The first object to be converted.
:type __obj1: object
:param __obj2: The second object to be converted.
:type __obj2: object
"""
self._obj1 = __obj1
self._obj2 = __obj2
self._map_attr()

def _map_attr(self) -> None:
"""Maps the attributes of the two objects for conversion."""
...
def __repr__(self) -> str:
return self._obj2

def _map_attr(self) -> dict:
"""
Maps the attributes between the models for conversion reference.
:return: A dictionary of attributes mapped.
:rtype: dict
"""
for attr1 in self._obj1.keys():
for attr2 in self._obj2.keys():
self.__dict__.update({attr1: attr2})

return self.__dict__

def get_attr(self, attr: str) -> str:
"""
Gets a mapped attribute.
:param attr: The attribute to get.
:type attr: str
:return: The mapped attribute.
:rtype: str
"""
return self.__dict__.get(attr)

def get_attrs(self) -> List[str]:
"""
Gets a list of mapped attributes.
:return: The list of mapped attributes.
:rtype: list
"""
return self.__dict__

@property
def ref(self) -> object:
"""
Gets the "referenced" model, or first.
:return: The referenced model.
:rtype: object
"""
return self._obj1

@property
def diff(self) -> List[dict]:
"""
Gets a list of keys and values that the models don't share in common.
:return: A list of dictionaries
:rtype: List[dict]
"""
return [{key: val} for key, val in self._obj2 if key not in self._obj1]
11 changes: 11 additions & 0 deletions interactions/ext/converter.pyi
@@ -1,3 +1,14 @@
from typing import List

class Converter:
_obj1: object
_obj2: object
def __init__(self, __obj1: object, __obj2: object) -> None: ...
def __repr__(self) -> str: ...
def _map_attr(self) -> dict: ...
def get_attr(self, attr: str) -> str: ...
def get_attrs(self) -> List[str]: ...
@property
def ref(self) -> object: ...
@property
def diff(self) -> List[dict]: ...

0 comments on commit c1d9a2b

Please sign in to comment.