Add base mutable class.

This commit is contained in:
voussoir 2021-12-27 22:32:13 -08:00
parent 10a312b996
commit d3d5bd1fa2
No known key found for this signature in database
GPG key ID: 5F7554F8C26DACCB

View file

@ -1,70 +1,52 @@
class Boolean: class MutableBase:
def __init__(self, value): def __init__(self, value):
self.set(value) self.set(value)
def __repr__(self):
return f'{self.__module__}.{self.__class__.__name__}({repr(self._value)})'
@property
def value(self):
return self._value
def get(self):
return self._value
def set(self, value):
if type(value) not in self._types:
raise TypeError(value)
self._value = value
class Boolean(MutableBase):
_types = [bool]
def __bool__(self): def __bool__(self):
return self.__value return self._value
def get(self): class Bytes(MutableBase):
return self.__value _types = [bytes]
def set(self, value): class Float(MutableBase):
if type(value) is not bool: _types = [int, float]
raise TypeError(value)
self.__value = value def __int__(self):
return int(self._value)
class Bytes: def __float__(self):
def __init__(self, value): return float(self._value)
self.set(value)
def get(self): class Integer(MutableBase):
return self.__value _types = [int]
def set(self, value): def __int__(self):
if type(value) is not bytes: return int(self._value)
raise TypeError(value)
self.__value = value def __float__(self):
return float(self._value)
class Float: class String(MutableBase):
def __init__(self, value): _types = [str]
self.set(value)
def get(self):
return self.__value
def set(self, value):
if type(value) not in (int, float):
raise TypeError(value)
self.__value = value
class Integer:
def __init__(self, value):
self.set(value)
def get(self):
return self.__value
def set(self, value):
if type(value) is not int:
raise TypeError(value)
self.__value = value
class String:
def __init__(self, value):
self.set(value)
def __str__(self): def __str__(self):
return self.__value return self._value
def get(self):
return self.__value
def set(self, value):
if type(value) is not str:
raise TypeError(value)
self.__value = value