diff --git a/pottery/counter.py b/pottery/counter.py index 27ffae79..3ce8e4d2 100644 --- a/pottery/counter.py +++ b/pottery/counter.py @@ -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 diff --git a/pottery/dict.py b/pottery/dict.py index 3e7023bc..65b89dfa 100644 --- a/pottery/dict.py +++ b/pottery/dict.py @@ -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 @@ -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: