From ca24e1acdb1ccd01ea17bb1345bd9b7c0fefa2a6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Ma=C5=9Blanka?= <piotr.maslanka@henrietta.com.pl> Date: Tue, 5 Mar 2024 08:37:31 +0100 Subject: [PATCH] fix pylint issues --- satella/coding/concurrent/queue.py | 52 +++++++++++-------- satella/coding/metaclasses.py | 16 +++--- .../coding/structures/syncable_droppable.py | 26 +++++----- satella/os/misc.py | 26 +++++----- 4 files changed, 64 insertions(+), 56 deletions(-) diff --git a/satella/coding/concurrent/queue.py b/satella/coding/concurrent/queue.py index 4c34955b..8df1a49d 100644 --- a/satella/coding/concurrent/queue.py +++ b/satella/coding/concurrent/queue.py @@ -44,6 +44,33 @@ class PeekableQueue(tp.Generic[T]): self.queue.append(item) self.inserted_condition.notify(items_count) + def __get_timeout_none(self, item_getter): + while True: + self.lock.release() + self.inserted_condition.wait() + self.lock.acquire() + if len(self.queue): + try: + return item_getter(self.queue) + finally: + self.lock.release() + + def __get_timeout(self, item_getter, timeout): + with measure(timeout=timeout) as measurement: + while not measurement.timeouted: + self.lock.release() + # raises WouldWaitMore + self.inserted_condition.wait(timeout=measurement.time_remaining) + self.lock.acquire() + if len(self.queue): + try: + return item_getter(self.queue) + finally: + self.lock.release() + else: + self.lock.release() + raise Empty('queue is empty') + @rethrow_as(WouldWaitMore, Empty) def __get(self, timeout, item_getter) -> T: self.lock.acquire() @@ -55,30 +82,9 @@ class PeekableQueue(tp.Generic[T]): self.lock.release() else: if timeout is None: - while True: - self.lock.release() - self.inserted_condition.wait() - self.lock.acquire() - if len(self.queue): - try: - return item_getter(self.queue) - finally: - self.lock.release() + return self.__get_timeout_none(item_getter) else: - with measure(timeout=timeout) as measurement: - while not measurement.timeouted: - self.lock.release() - # raises WouldWaitMore - self.inserted_condition.wait(timeout=measurement.time_remaining) - self.lock.acquire() - if len(self.queue): - try: - return item_getter(self.queue) - finally: - self.lock.release() - else: - self.lock.release() - raise Empty('queue is empty') + return self.__get_timeout(item_getter, timeout) def get(self, timeout: tp.Optional[float] = None) -> T: """ diff --git a/satella/coding/metaclasses.py b/satella/coding/metaclasses.py index 43dc1501..9e35cf5b 100644 --- a/satella/coding/metaclasses.py +++ b/satella/coding/metaclasses.py @@ -70,19 +70,17 @@ def DocsFromParent(name: str, bases: tp.Tuple[type], dictionary: dict) -> tp.Typ """ if '__doc__' not in dictionary: for base in walk(bases, _extract_bases, deep_first=False): - if hasattr(base, '__doc__'): - if base.__doc__: - dictionary['__doc__'] = base.__doc__ - break + if hasattr(base, '__doc__') and base.__doc__: + dictionary['__doc__'] = base.__doc__ + break for key, value in dictionary.items(): if not value.__doc__ and callable(value): for base in walk(bases, _extract_bases, deep_first=False): - if hasattr(base, key): - if getattr(base, key).__doc__: - value.__doc__ = getattr(base, key).__doc__ - dictionary[key] = value - break + if hasattr(base, key) and getattr(base, key).__doc__: + value.__doc__ = getattr(base, key).__doc__ + dictionary[key] = value + break return type(name, bases, dictionary) diff --git a/satella/coding/structures/syncable_droppable.py b/satella/coding/structures/syncable_droppable.py index a1f4c0a4..4bfd2975 100644 --- a/satella/coding/structures/syncable_droppable.py +++ b/satella/coding/structures/syncable_droppable.py @@ -332,6 +332,19 @@ class SyncableDroppable(RMonitor, tp.Generic[K, V]): finally: try_close(iterator) + def __append_item(self, data, iterator, maximum_entries): + while len(data) < maximum_entries: + try: + data.append(next(iterator)) + except StopIteration: + for index, tpl in enumerate(self.data_in_memory): + if len(data) >= maximum_entries: + break + if self.synced_up_to is not None: + if tpl[0] > self.synced_up_to: + break + return itertools.chain(data, self.data_in_memory[:index]) + def _on_sync_request_synced_up_is_not_none(self, maximum_entries): if self.first_key_in_memory <= self.synced_up_to: # Means we have to sync from memory @@ -349,18 +362,7 @@ class SyncableDroppable(RMonitor, tp.Generic[K, V]): data = [] iterator = self.db_storage.iterate(self.start_entry) try: - while len(data) < maximum_entries: - try: - data.append(next(iterator)) - except StopIteration: - for index, tpl in enumerate(self.data_in_memory): - if len(data) >= maximum_entries: - break - if self.synced_up_to is not None: - if tpl[0] > self.synced_up_to: - break - v = itertools.chain(data, self.data_in_memory[:index]) - break + v = self.__append_item(data, iterator, maximum_entries) finally: try_close(iterator) return v diff --git a/satella/os/misc.py b/satella/os/misc.py index 5794078c..60663870 100644 --- a/satella/os/misc.py +++ b/satella/os/misc.py @@ -27,18 +27,20 @@ def whereis(name: str) -> tp.Iterator[str]: available_extensions = '', for directory in paths_to_look_in: - with silence_excs(FileNotFoundError): - for file in os.listdir(directory): - path = os.path.join(directory, file) - if 'x' not in stat.filemode(os.stat(path).st_mode): - continue - - if sys.platform.startswith('win'): # a POSIX-specific check - file = file.upper() # paths are not case-sensitive on Windows - - for extension in available_extensions: - if file == '%s%s' % (name, extension): - yield path + yield from _whereis(directory, name, available_extensions) + + +@silence_excs(FileNotFoundError) +def _whereis(directory: str, name, available_extensions): + for file in os.listdir(directory): + path = os.path.join(directory, file) + if 'x' in stat.filemode(os.stat(path).st_mode): + if sys.platform.startswith('win'): # a POSIX-specific check + file = file.upper() # paths are not case-sensitive on Windows + + for extension in available_extensions: + if file == '%s%s' % (name, extension): + yield path def is_running_as_root() -> bool: -- GitLab