Skip to content

Commit

Permalink
Preserve Open-Closed Principle with name mangling (#582)
Browse files Browse the repository at this point in the history
  • Loading branch information
brainix authored Jan 8, 2022
1 parent 36b2be3 commit 0a57ef5
Showing 1 changed file with 10 additions and 10 deletions.
20 changes: 10 additions & 10 deletions pottery/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,19 +115,22 @@ def __del__(self) -> None:

@property
def redis(self) -> Redis:
return self._redis
# Preserve the Open-Closed Principle with name mangling.
# https://youtu.be/miGolgp9xq8?t=2086
# https://stackoverflow.com/a/38534939
return self.__redis

@redis.setter
def redis(self, value: Optional[Redis]) -> None:
self._redis = _default_redis if value is None else value
self.__redis = _default_redis if value is None else value

@property
def key(self) -> str:
return self._key # type: ignore
return self.__key # type: ignore

@key.setter
def key(self, value: str) -> None:
self._key = value or self.__random_key()
self.__key = value or self.__random_key()

def _random_key(self) -> str:
key = random_key(redis=self.redis, prefix=self._RANDOM_KEY_PREFIX)
Expand All @@ -138,9 +141,6 @@ def _random_key(self) -> str:
)
return key

# Preserve the Open-Closed Principle with name mangling.
# https://youtu.be/miGolgp9xq8?t=2086
# https://stackoverflow.com/a/38534939
__random_key = _random_key


Expand Down Expand Up @@ -309,7 +309,7 @@ class Primitive(metaclass=abc.ABCMeta):
def KEY_PREFIX(self) -> str:
'Redis key prefix/namespace.'

__slots__ = ('_key', 'masters', 'raise_on_redis_errors')
__slots__ = ('__key', 'masters', 'raise_on_redis_errors')

_DEFAULT_MASTERS: ClassVar[FrozenSet[Redis]] = frozenset({_default_redis})

Expand All @@ -325,11 +325,11 @@ def __init__(self,

@property
def key(self) -> str:
return self._key
return self.__key

@key.setter
def key(self, value: str) -> None:
self._key = f'{self.KEY_PREFIX}:{value}'
self.__key = f'{self.KEY_PREFIX}:{value}'

def _check_enough_masters_up(self,
raise_on_redis_errors: Optional[bool],
Expand Down

0 comments on commit 0a57ef5

Please sign in to comment.