Skip to content

Commit

Permalink
Factor our RedisDict._encode_dict() (#571)
Browse files Browse the repository at this point in the history
Reuse `._encode_dict()` between `RedisDict._populate()` and
`RedisCounter._populate()`.
  • Loading branch information
brainix authored Dec 29, 2021
1 parent 906cac2 commit 6161d8e
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 8 deletions.
5 changes: 1 addition & 4 deletions pottery/counter.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,7 @@ def _populate(self, # type: ignore
dict_[key] = original + sign * value

dict_ = {key: self[key] + value for key, value in dict_.items()}
encoded_dict = {
self._encode(key): self._encode(value)
for key, value in dict_.items()
}
encoded_dict = self._encode_dict(dict_)
if encoded_dict:
pipeline.multi()
pipeline.hset(self.key, mapping=encoded_dict) # type: ignore
Expand Down
14 changes: 10 additions & 4 deletions pottery/dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,7 @@ def _populate(self,
arg = cast(InitMap, arg).items()
items = itertools.chain(cast(InitIter, arg), kwargs.items())
dict_ = dict(items)
encoded_dict = {
self._encode(key): self._encode(value)
for key, value in dict_.items()
}
encoded_dict = self.__encode_dict(dict_)
if encoded_dict:
pipeline.multi()
pipeline.hset(self.key, mapping=encoded_dict) # type: ignore
Expand All @@ -86,6 +83,15 @@ def _populate(self,
# https://stackoverflow.com/a/38534939
__populate = _populate

def _encode_dict(self, dict_):
encoded_dict = {
self._encode(key): self._encode(value)
for key, value in dict_.items()
}
return encoded_dict

__encode_dict = _encode_dict

# Methods required by collections.abc.MutableMapping:

def __getitem__(self, key: JSONTypes) -> JSONTypes:
Expand Down

0 comments on commit 6161d8e

Please sign in to comment.