t | from inspect import get_annotations | t | from inspect import get_annotations |
| | | |
| def fancy_print(self): | | def fancy_print(self): |
| ans = [] | | ans = [] |
| for el in get_annotations(self.__class__).keys(): | | for el in get_annotations(self.__class__).keys(): |
| try: | | try: |
| ans.append(el + '=' + str(getattr(self, str(el)))) | | ans.append(el + '=' + str(getattr(self, str(el)))) |
| except Exception as e: | | except Exception as e: |
| pass | | pass |
| return ' '.join(ans) | | return ' '.join(ans) |
| | | |
| class positioned(type): | | class positioned(type): |
| | | |
| def __new__(cls, name, bases, dct): | | def __new__(cls, name, bases, dct): |
| | | |
| def __init__(self, *args, **kwargs): | | def __init__(self, *args, **kwargs): |
| self.__class__.__match_args__ = tuple([*get_annotations(self.__class__).keys()]) | | self.__class__.__match_args__ = tuple([*get_annotations(self.__class__).keys()]) |
| anotat = [*get_annotations(self.__class__).keys()] | | anotat = [*get_annotations(self.__class__).keys()] |
| for item in kwargs.items(): | | for item in kwargs.items(): |
| if item[0] in anotat: | | if item[0] in anotat: |
| setattr(self, item[0], item[1]) | | setattr(self, item[0], item[1]) |
| k = 0 | | k = 0 |
| for i, el in enumerate(args): | | for i, el in enumerate(args): |
| while anotat[k] in kwargs.keys(): | | while anotat[k] in kwargs.keys(): |
| k += 1 | | k += 1 |
| if k == len(anotat): | | if k == len(anotat): |
| break | | break |
| if k == len(anotat): | | if k == len(anotat): |
| break | | break |
| setattr(self, anotat[k], el) | | setattr(self, anotat[k], el) |
| k += 1 | | k += 1 |
| if k == len(anotat): | | if k == len(anotat): |
| break | | break |
| dct['__str__'] = lambda self: fancy_print(self) | | dct['__str__'] = lambda self: fancy_print(self) |
| dct['__init__'] = __init__ | | dct['__init__'] = __init__ |
| return super().__new__(cls, name, bases, dct) | | return super().__new__(cls, name, bases, dct) |