|
| 1 | +# !/usr/bin/env python |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | +# |
| 4 | +# bases.py |
| 5 | +""" |
| 6 | +Useful base classes |
| 7 | +""" |
| 8 | +# |
| 9 | +# Copyright © 2020 Dominic Davis-Foster <[email protected]> |
| 10 | +# |
| 11 | +# This program is free software; you can redistribute it and/or modify |
| 12 | +# it under the terms of the GNU Lesser General Public License as published by |
| 13 | +# the Free Software Foundation; either version 3 of the License, or |
| 14 | +# (at your option) any later version. |
| 15 | +# |
| 16 | +# This program is distributed in the hope that it will be useful, |
| 17 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 | +# GNU Lesser General Public License for more details. |
| 20 | +# |
| 21 | +# You should have received a copy of the GNU Lesser General Public License |
| 22 | +# along with this program; if not, write to the Free Software |
| 23 | +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
| 24 | +# MA 02110-1301, USA. |
| 25 | +# |
| 26 | + |
| 27 | + |
| 28 | + |
| 29 | +# stdlib |
| 30 | +from abc import ABC, abstractmethod |
| 31 | +from collections import UserList |
| 32 | +from pprint import pformat |
| 33 | + |
| 34 | +# 3rd party |
| 35 | +import pydash |
| 36 | + |
| 37 | + |
| 38 | +class Dictable(ABC): |
| 39 | + """ |
| 40 | + The basic structure of a class than can be converted into a dictionary |
| 41 | + """ |
| 42 | + |
| 43 | + @abstractmethod |
| 44 | + def __init__(self, *args, **kwargs): |
| 45 | + pass |
| 46 | + |
| 47 | + def __str__(self): |
| 48 | + return self.__repr__() |
| 49 | + |
| 50 | + def __iter__(self): |
| 51 | + for key, value in self.__dict__().items(): |
| 52 | + yield key, value |
| 53 | + |
| 54 | + def __getstate__(self): |
| 55 | + return self.__dict__() |
| 56 | + |
| 57 | + def __setstate__(self, state): |
| 58 | + self.__init__(**state) |
| 59 | + |
| 60 | + def __copy__(self): |
| 61 | + return self.__class__(**self.__dict__()) |
| 62 | + |
| 63 | + def __deepcopy__(self, memodict={}): |
| 64 | + return self.__copy__() |
| 65 | + |
| 66 | + @abstractmethod |
| 67 | + def __dict__(self): |
| 68 | + return dict() |
| 69 | + |
| 70 | + def __eq__(self, other): |
| 71 | + if isinstance(other, self.__class__): |
| 72 | + return pydash.predicates.is_match(other.__dict__(), self.__dict__()) |
| 73 | + |
| 74 | + return NotImplemented |
| 75 | + |
| 76 | + |
| 77 | +def namedlist(name="NamedList"): |
| 78 | + """ |
| 79 | + A factory function to return a custom list subclass with a name |
| 80 | +
|
| 81 | + :param name: |
| 82 | + :type name: |
| 83 | + :return: |
| 84 | + :rtype: |
| 85 | + """ |
| 86 | + |
| 87 | + class NamedList(UserList): |
| 88 | + """ |
| 89 | + A list with a name |
| 90 | + """ |
| 91 | + |
| 92 | + def __repr__(self): |
| 93 | + return f"{super().__repr__()}" |
| 94 | + |
| 95 | + def __str__(self): |
| 96 | + return f"{self.__class__.__name__}{pformat(list(self))}" |
| 97 | + |
| 98 | + NamedList.__name__ = name |
| 99 | + |
| 100 | + return NamedList |
0 commit comments