Reformatted code base using black.

This commit is contained in:
Goffi 2024-06-19 18:44:57 +02:00
commit 53fdaa9d09

View file

@ -34,7 +34,9 @@ class PersistentDict:
r"""A dictionary which save persistently each value assigned
/!\ be careful, each assignment means a database write
/!\ Memory must be initialised before loading/setting value with instances of this class"""
/!\ Memory must be initialised before loading/setting value with instances of this class
"""
storage = None
binary = False
@ -60,9 +62,11 @@ class PersistentDict:
need to be called before any other operation
@return: defers the PersistentDict instance itself
"""
d = defer.ensureDeferred(self.storage.get_privates(
self.namespace, binary=self.binary, profile=self.profile
))
d = defer.ensureDeferred(
self.storage.get_privates(
self.namespace, binary=self.binary, profile=self.profile
)
)
d.addCallback(self._set_cache)
d.addCallback(lambda __: self)
return d
@ -130,7 +134,9 @@ class PersistentDict:
def clear(self):
"""Delete all values from this namespace"""
self._cache.clear()
return self.storage.del_private_namespace(self.namespace, self.binary, self.profile)
return self.storage.del_private_namespace(
self.namespace, self.binary, self.profile
)
def get(self, key, default=None):
return self._cache.get(key, default)
@ -148,7 +154,8 @@ class PersistentDict:
"""Async del, return a Deferred fired when value is actually deleted"""
self._cache.__delitem__(key)
return self.storage.del_private_value(
self.namespace, key, self.binary, self.profile)
self.namespace, key, self.binary, self.profile
)
def setdefault(self, key, default):
try:
@ -171,6 +178,7 @@ class PersistentDict:
class PersistentBinaryDict(PersistentDict):
"""Persistent dict where value can be any python data (instead of string only)"""
binary = True
@ -181,6 +189,7 @@ class LazyPersistentBinaryDict(PersistentBinaryDict):
to save memory.
/!\ most of methods return a Deferred
"""
# TODO: missing methods should be implemented using database access
# TODO: a cache would be useful (which is deleted after a timeout)
@ -192,16 +201,20 @@ class LazyPersistentBinaryDict(PersistentBinaryDict):
raise NotImplementedError
def items(self):
d = defer.ensureDeferred(self.storage.get_privates(
self.namespace, binary=self.binary, profile=self.profile
))
d = defer.ensureDeferred(
self.storage.get_privates(
self.namespace, binary=self.binary, profile=self.profile
)
)
d.addCallback(lambda data_dict: data_dict.items())
return d
def all(self):
return defer.ensureDeferred(self.storage.get_privates(
self.namespace, binary=self.binary, profile=self.profile
))
return defer.ensureDeferred(
self.storage.get_privates(
self.namespace, binary=self.binary, profile=self.profile
)
)
def __repr__(self):
return self.__str__()
@ -231,7 +244,7 @@ class LazyPersistentBinaryDict(PersistentBinaryDict):
raise NotImplementedError
def __hash__(self):
return hash(str(self.__class__) + self.namespace + (self.profile or ''))
return hash(str(self.__class__) + self.namespace + (self.profile or ""))
def __bool__(self):
raise NotImplementedError
@ -252,9 +265,11 @@ class LazyPersistentBinaryDict(PersistentBinaryDict):
def __getitem__(self, key):
"""get the value as a Deferred"""
d = defer.ensureDeferred(self.storage.get_privates(
self.namespace, keys=[key], binary=self.binary, profile=self.profile
))
d = defer.ensureDeferred(
self.storage.get_privates(
self.namespace, keys=[key], binary=self.binary, profile=self.profile
)
)
d.addCallback(self._data2value, key)
return d
@ -291,7 +306,8 @@ class LazyPersistentBinaryDict(PersistentBinaryDict):
"""Async del, return a Deferred fired when value is actually deleted"""
# XXX: similar as PersistentDict.adel, but doesn't use cache
return self.storage.del_private_value(
self.namespace, key, self.binary, self.profile)
self.namespace, key, self.binary, self.profile
)
def setdefault(self, key, default):
raise NotImplementedError
@ -314,4 +330,6 @@ class LazyPersistentBinaryDict(PersistentBinaryDict):
@param key(unicode): key to delete
@return (D): A deferred fired when delete is done
"""
return self.storage.del_private_value(self.namespace, key, self.binary, self.profile)
return self.storage.del_private_value(
self.namespace, key, self.binary, self.profile
)