global KVDELETE

This commit is contained in:
AF 2023-02-01 02:08:54 +00:00
parent 1cd39ad061
commit 3b622984bf

View File

@ -1,6 +1,7 @@
# Licensed under MIT License. Copyright: 2022-2023 PARRRATE TNV. # Licensed under MIT License. Copyright: 2022-2023 PARRRATE TNV.
__all__ = ( __all__ = (
'KVDELETE',
'KVFactory', 'KVFactory',
'KVJson', 'KVJson',
'DbConnection', 'DbConnection',
@ -66,6 +67,9 @@ class KVProtocol(abc.ABC):
raise NotImplementedError raise NotImplementedError
KVDELETE = object()
class KVFactory(KVProtocol): class KVFactory(KVProtocol):
"""this class is for working with already normalised data values, not for data transformation (e.g. reducing keys to a common form). """this class is for working with already normalised data values, not for data transformation (e.g. reducing keys to a common form).
that functionality may be added in the future, though, probably, only for custom DbConnection implementations. that functionality may be added in the future, though, probably, only for custom DbConnection implementations.
@ -73,8 +77,6 @@ note: unstable signature."""
__slots__ = () __slots__ = ()
DELETE = object()
@abc.abstractmethod @abc.abstractmethod
def line(self, key: Any, value: Any, /) -> str: def line(self, key: Any, value: Any, /) -> str:
"""line must contain exactly one '\\n' at exactly the end if the line is not empty. """line must contain exactly one '\\n' at exactly the end if the line is not empty.
@ -95,7 +97,7 @@ note: unstable signature."""
self._dbset(db, key, value, reduce) self._dbset(db, key, value, reduce)
def _dbset(self, db: dict, key: Any, value: Any, reduce: bool, /): def _dbset(self, db: dict, key: Any, value: Any, reduce: bool, /):
if reduce and value is self.DELETE: if reduce and value is KVDELETE:
db.pop(key, None) db.pop(key, None)
else: else:
db[key] = value db[key] = value
@ -108,7 +110,7 @@ note: unstable signature."""
return self.filter_value(value, default) return self.filter_value(value, default)
def filter_value(self, value: Any, default: Any, /): def filter_value(self, value: Any, default: Any, /):
if value is self.DELETE: if value is KVDELETE:
return default return default
else: else:
return value return value
@ -168,7 +170,7 @@ class KVJson(KVFactory):
__slots__ = () __slots__ = ()
def line(self, key: Any, value: Any, /) -> str: def line(self, key: Any, value: Any, /) -> str:
if value is self.DELETE: if value is KVDELETE:
obj = {'key': key} obj = {'key': key}
else: else:
obj = {'key': key, 'value': value} obj = {'key': key, 'value': value}
@ -188,7 +190,7 @@ class KVJson(KVFactory):
def fromline(self, line: str, /) -> tuple[Any, Any]: def fromline(self, line: str, /) -> tuple[Any, Any]:
d = json.loads(line) d = json.loads(line)
return self._load_key(d['key']), d.get('value', self.DELETE) return self._load_key(d['key']), d.get('value', KVDELETE)
class TransactionRequest(LineRequest): class TransactionRequest(LineRequest):