From c1d9a2b4079ec46ee84266fc92feb67197ea6040 Mon Sep 17 00:00:00 2001 From: James Walston Date: Mon, 14 Feb 2022 22:26:19 -0500 Subject: [PATCH] feat: implement `Converter` class. --- interactions/ext/converter.py | 67 ++++++++++++++++++++++++++++++++-- interactions/ext/converter.pyi | 11 ++++++ 2 files changed, 75 insertions(+), 3 deletions(-) diff --git a/interactions/ext/converter.py b/interactions/ext/converter.py index 84a86dff5..fe4d36c22 100644 --- a/interactions/ext/converter.py +++ b/interactions/ext/converter.py @@ -1,3 +1,6 @@ +from typing import List + + class Converter: """ A class representing the "conversion" or consistent mapping between @@ -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] diff --git a/interactions/ext/converter.pyi b/interactions/ext/converter.pyi index e8d37aff2..b1b2316e4 100644 --- a/interactions/ext/converter.pyi +++ b/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]: ...