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.
__all__ = (
'KVDELETE',
'KVFactory',
'KVJson',
'DbConnection',
@ -66,6 +67,9 @@ class KVProtocol(abc.ABC):
raise NotImplementedError
KVDELETE = object()
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).
that functionality may be added in the future, though, probably, only for custom DbConnection implementations.
@ -73,8 +77,6 @@ note: unstable signature."""
__slots__ = ()
DELETE = object()
@abc.abstractmethod
def line(self, key: Any, value: Any, /) -> str:
"""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)
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)
else:
db[key] = value
@ -108,7 +110,7 @@ note: unstable signature."""
return self.filter_value(value, default)
def filter_value(self, value: Any, default: Any, /):
if value is self.DELETE:
if value is KVDELETE:
return default
else:
return value
@ -168,7 +170,7 @@ class KVJson(KVFactory):
__slots__ = ()
def line(self, key: Any, value: Any, /) -> str:
if value is self.DELETE:
if value is KVDELETE:
obj = {'key': key}
else:
obj = {'key': key, 'value': value}
@ -188,7 +190,7 @@ class KVJson(KVFactory):
def fromline(self, line: str, /) -> tuple[Any, Any]:
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):