1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
|
__all__ = [
'MixinStringWithFormat',
]
__author__ = 'Pyramidev'
import abc
from typing import NoReturn, Optional, Type, TypeVar
T = TypeVar('T', bound='MixinStringWithFormat')
class MixinStringWithFormat:
__value: str
# pylint: disable=inconsistent-return-statements
@classmethod
def create_or_raise(cls: Type[T], value: str) -> T:
result = cls.create_or_None(value)
if result is not None:
return result
cls._raise_exception(value)
# pylint: enable=inconsistent-return-statements
@classmethod
def create_or_None(cls: Type[T], value: str) -> Optional[T]:
if cls.valid_format(value):
result: T = cls.__new__(cls)
result.__value = value # pylint: disable=protected-access
return result
return None
@classmethod
@abc.abstractmethod
def valid_format(cls, value: str) -> bool:
pass
@classmethod
@abc.abstractmethod
def _raise_exception(cls, value: str) -> NoReturn:
pass
def __str__(self) -> str:
return self.__value
def __eq__(self, other: object) -> bool:
if type(self) != type(other): # pylint: disable=unidiomatic-typecheck
return False
return str(self) == str(other)
def __hash__(self) -> int:
return hash(self.__value) |
Partager