Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify code to reduce cognitive load #348

Merged
merged 4 commits into from
Feb 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions pottery/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@ def random_key(*,
'Find/return a random key that does not exist in the Redis instance.'
if not isinstance(num_tries, int):
raise TypeError('num_tries must be an int >= 0')
elif num_tries < 0:
if num_tries < 0:
raise ValueError('num_tries must be an int >= 0')
elif num_tries <= 0:
if num_tries == 0:
raise RandomKeyError(redis)

all_chars = string.digits + string.ascii_letters
Expand Down
3 changes: 1 addition & 2 deletions pottery/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -303,8 +303,7 @@ def __retry(self, callable: Callable[[], Any], try_num: int = 0) -> Any:
except WatchError: # pragma: no cover
if try_num < self._num_tries - 1:
return self.__retry(callable, try_num=try_num+1)
else:
raise
raise

@_set_expiration
def update(self, arg: InitArg = tuple(), **kwargs: JSONTypes) -> None: # type: ignore
Expand Down
7 changes: 3 additions & 4 deletions pottery/list.py
Original file line number Diff line number Diff line change
Expand Up @@ -267,10 +267,9 @@ def remove(self, value: JSONTypes) -> None:
for index, element in enumerate(self):
if element == value:
self.__delete(pipeline, index)
break
else:
class_ = self.__class__.__name__
raise ValueError(f'{class_}.remove(x): x not in {class_}')
return
class_name = self.__class__.__name__
raise ValueError(f'{class_name}.remove(x): x not in {class_name}')

def to_list(self) -> List[JSONTypes]:
return list(self)
22 changes: 11 additions & 11 deletions pottery/redlock.py
Original file line number Diff line number Diff line change
Expand Up @@ -281,10 +281,10 @@ def __acquire_masters(self) -> bool:

if quorum and max(validity_time, 0):
return True
else:
with contextlib.suppress(ReleaseUnlockedLock):
self.__release()
return False

with contextlib.suppress(ReleaseUnlockedLock):
self.__release()
return False

def acquire(self, *, blocking: bool = True, timeout: int = -1) -> bool:
'''Lock the lock.
Expand Down Expand Up @@ -335,13 +335,13 @@ def acquire(self, *, blocking: bool = True, timeout: int = -1) -> bool:
while timeout == -1 or timer.elapsed() / 1000 < timeout:
if self.__acquire_masters():
return True
else:
time.sleep(random.uniform(0, self.RETRY_DELAY/1000))
time.sleep(random.uniform(0, self.RETRY_DELAY/1000))
return False
elif timeout == -1:

if timeout == -1:
return self.__acquire_masters()
else:
raise ValueError("can't specify a timeout for a non-blocking call")

raise ValueError("can't specify a timeout for a non-blocking call")

__acquire = acquire

Expand Down Expand Up @@ -396,8 +396,8 @@ def locked(self) -> int:
validity_time = ttls[len(self.masters) // 2]
validity_time -= round(timer.elapsed() + self.__drift())
return max(validity_time, 0)
else:
return 0

return 0

__locked = locked

Expand Down