Skip to content

Commit

Permalink
Refactor _Common class (#618)
Browse files Browse the repository at this point in the history
* Refactor _Common class

* Simplify _Common.__init__() method signature

Ensure that the `key` argument is always a string.

* Properly format _Common.__init__()
  • Loading branch information
brainix authored Feb 2, 2022
1 parent 9ebca1d commit e91bc53
Show file tree
Hide file tree
Showing 6 changed files with 10 additions and 14 deletions.
12 changes: 4 additions & 8 deletions pottery/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,13 +97,9 @@ class _Common:

_RANDOM_KEY_PREFIX: ClassVar[str] = 'pottery:'

def __init__(self,
*,
redis: Redis | None = None,
key: str | None = None,
) -> None:
self.redis = cast(Redis, redis)
self.key = cast(str, key)
def __init__(self, *, redis: Redis | None = None, key: str = '') -> None:
self.redis = redis or _default_redis
self.key = key or self.__random_key()

def __del__(self) -> None:
if self.key.startswith(self._RANDOM_KEY_PREFIX):
Expand All @@ -123,7 +119,7 @@ def redis(self) -> Redis:

@redis.setter
def redis(self, value: Redis | None) -> None:
self.__redis = _default_redis if value is None else value
self.__redis = value or _default_redis

@property
def key(self) -> str:
Expand Down
2 changes: 1 addition & 1 deletion pottery/deque.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def __init__(self,
maxlen: int | None = None,
*,
redis: Redis | None = None,
key: str | None = None,
key: str = '',
) -> None:
'Initialize the RedisDeque. O(n)'
if maxlen is not None and not isinstance(maxlen, int):
Expand Down
2 changes: 1 addition & 1 deletion pottery/dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def __init__(self,
arg: InitArg = tuple(),
*,
redis: Redis | None = None,
key: str | None = None,
key: str = '',
**kwargs: JSONTypes,
) -> None:
'Initialize the RedisDict. O(n)'
Expand Down
4 changes: 2 additions & 2 deletions pottery/hyper.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ def __init__(self,
iterable: Iterable[RedisValues] = frozenset(),
*,
redis: Redis | None = None,
key: str | None = None,
key: str = '',
) -> None:
'''Initialize the HyperLogLog. O(n)
Expand Down Expand Up @@ -149,7 +149,7 @@ def update(self, *objs: HyperLogLog | Iterable[RedisValues]) -> None:
def union(self,
*objs: Iterable[RedisValues],
redis: Redis | None = None,
key: str | None = None,
key: str = '',
) -> HyperLogLog:
new_hll = self.__class__(redis=redis, key=key)
new_hll.update(self, *objs)
Expand Down
2 changes: 1 addition & 1 deletion pottery/list.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ def __init__(self,
iterable: Iterable[JSONTypes] = tuple(),
*,
redis: Redis | None = None,
key: str | None = None,
key: str = '',
) -> None:
'Initialize the RedisList. O(n)'
super().__init__(redis=redis, key=key)
Expand Down
2 changes: 1 addition & 1 deletion pottery/set.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def __init__(self,
iterable: Iterable[JSONTypes] = tuple(),
*,
redis: Redis | None = None,
key: str | None = None,
key: str = '',
) -> None:
'Initialize the RedisSet. O(n)'
super().__init__(redis=redis, key=key)
Expand Down

0 comments on commit e91bc53

Please sign in to comment.