Skip to content

Commit

Permalink
Adds missing dict methods in RedisDict (#862)
Browse files Browse the repository at this point in the history
* Adds missing dict methods

* remove set class

---------

Co-authored-by: noO0ob <[email protected]>
  • Loading branch information
noO0oOo0ob and noO0ob authored Jun 21, 2024
1 parent aa198bc commit 66dafa5
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 22 deletions.
65 changes: 44 additions & 21 deletions lyrebird/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -553,6 +553,25 @@ def destory(self):
self.redis.delete(self.uuid)
self.redis.close()

def copy(self):
# Use the copy method with caution, especially during the create and release phases.
return type(self)(host=self.host, port=self.port, db=self.db, param_uuid=self.uuid)

def __eq__(self, other):
if not isinstance(other, type(self)):
return NotImplemented
if self.uuid != other.uuid:
return False
if self.host != other.host:
return False
if self.port != other.port:
return False
if self.db != other.db:
return False
return True

def __ne__(self, other):
return not self.__eq__(other)

def __getstate__(self):
return pickle.dumps({
Expand Down Expand Up @@ -599,6 +618,9 @@ def __delitem__(self, key):
def __contains__(self, key):
return self.redis.hexists(self.uuid, key)

def __iter__(self):
return self.raw()

def keys(self):
return [key.decode() for key in self.redis.hkeys(self.uuid)]

Expand All @@ -618,13 +640,31 @@ def update(self, data):
for key, value in data.items():
self[key] = value
self.redis.expire(self.uuid, REDIS_EXPIRE_TIME)


def pop(self, key, default=None):
if key not in self:
return default
else:
value = self.get(key, default)
del self[key]
return value

def setdefault(self, key, default=None):
try:
return self[key]
except KeyError:
self[key] = default
return default

def clear(self):
self.redis.delete(self.uuid)

def raw(self):
return {key.decode(): json.loads(value.decode()) for key, value in self.redis.hgetall(self.uuid).items()}

def __hash__(self):
return hash((self.uuid, self.host, self.port, self.db))

def __len__(self):
return len(self.redis.hkeys(self.uuid))

Expand All @@ -640,8 +680,6 @@ def _hook_value(parent, key, value):
return RedisHookedDict(parent, key, value)
elif isinstance(value, list):
return RedisHookedList(parent, key, value)
elif isinstance(value, set):
return RedisHookedSet(parent, key, value)
else:
return value

Expand All @@ -665,7 +703,7 @@ def __getitem__(self, key):
return _hook_value(self, key, dict.__getitem__(self, key))

def __setitem__(self, key, value):
dict.__setitem__(self, key, value)
dict.__setitem__(self, key, _hook_value(self, key, value))
self.parent[self.key] = self

def __delitem__(self, key):
Expand All @@ -679,6 +717,7 @@ def update(self, *args, **kwargs):
def __deepcopy__(self, memo):
return deepcopy(dict(self), memo)


class RedisHookedList(RedisHook, list):
def __init__(self, parent, key, value):
list.__init__(self, value)
Expand All @@ -688,7 +727,7 @@ def __getitem__(self, index):
return _hook_value(self, index, list.__getitem__(self, index))

def __setitem__(self, index, value):
list.__setitem__(self, index, _hook_value(value))
list.__setitem__(self, index, _hook_value(self, index, value))
self.parent[self.key] = self

def __delitem__(self, index):
Expand All @@ -701,19 +740,3 @@ def append(self, value):

def __deepcopy__(self, memo):
return deepcopy(list(self), memo)

class RedisHookedSet(RedisHook, set):
def __init__(self, parent, key, value):
set.__init__(self, value)
RedisHook.__init__(self, parent, key)

def add(self, value):
set.add(self, _hook_value(value))
self.parent[self.key] = self

def remove(self, value):
set.remove(self, value)
self.parent[self.key] = self

def __deepcopy__(self, memo):
return deepcopy(set(self), memo)
2 changes: 1 addition & 1 deletion lyrebird/version.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
IVERSION = (3, 0, 3)
IVERSION = (3, 0, 4)
VERSION = ".".join(str(i) for i in IVERSION)
LYREBIRD = "Lyrebird " + VERSION

0 comments on commit 66dafa5

Please sign in to comment.