From 8e71a932d5a5227e4c6e1f8c4a939caf99604b90 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Ma=C5=9Blanka?= <piotr.maslanka@henrietta.com.pl> Date: Wed, 29 Jul 2020 20:24:27 +0200 Subject: [PATCH] minor refactor --- satella/coding/concurrent/callablegroup.py | 4 ++-- satella/coding/concurrent/id_allocator.py | 2 ++ satella/coding/concurrent/locked_dataset.py | 18 +++++++++--------- satella/coding/deleters.py | 2 ++ satella/coding/sequences/iterators.py | 2 +- satella/coding/structures/mixins.py | 9 ++++----- satella/exception_handling/__init__.py | 7 ++++--- satella/exception_handling/dump_to_file.py | 3 --- .../exception_handling/exception_handlers.py | 2 +- satella/processes.py | 4 ++-- tests/test_coding/test_locked_dataset.py | 4 ++-- 11 files changed, 29 insertions(+), 28 deletions(-) diff --git a/satella/coding/concurrent/callablegroup.py b/satella/coding/concurrent/callablegroup.py index 2ce51ee4..deff87fd 100644 --- a/satella/coding/concurrent/callablegroup.py +++ b/satella/coding/concurrent/callablegroup.py @@ -80,8 +80,8 @@ class CallableGroup(tp.Generic[T]): class CallNoOftenThan: """ - A class that will ensure that calls to given callable are made no sooner - than after some interval. + A class that will ensure that calls to given callable are made no more often + than some interval. :param interval: interval in seconds :param callable_: callable to call diff --git a/satella/coding/concurrent/id_allocator.py b/satella/coding/concurrent/id_allocator.py index c5e3797b..f19e3153 100644 --- a/satella/coding/concurrent/id_allocator.py +++ b/satella/coding/concurrent/id_allocator.py @@ -13,6 +13,8 @@ class IDAllocator(Monitor): :param start_at: the lowest integer that the allocator will return """ + __slots__ = ('start_at', 'ints_allocated', 'free_ints', 'bound') + def __init__(self, start_at: int = 0): super().__init__() self.start_at = start_at diff --git a/satella/coding/concurrent/locked_dataset.py b/satella/coding/concurrent/locked_dataset.py index 45dfdcb7..3b168dbb 100644 --- a/satella/coding/concurrent/locked_dataset.py +++ b/satella/coding/concurrent/locked_dataset.py @@ -67,7 +67,7 @@ class LockedDataset: if inspect.ismethod(super(LockedDataset, self).__getattribute__(name)): return super(LockedDataset, self).__getattribute__(name) - if not get_internal(self).locked: + if not _get_internal(self).locked: raise ResourceNotLocked('No lock held on this object for a read operation') return super(LockedDataset, self).__getattribute__(name) @@ -75,27 +75,27 @@ class LockedDataset: def __setattr__(self, key, value): if key == '_LockedDataset__internal': return super(LockedDataset, self).__setattr__(key, value) - if not get_internal(self).locked: + if not _get_internal(self).locked: raise ResourceNotLocked('No lock held on this object for a write operation') return super(LockedDataset, self).__setattr__(key, value) def __call__(self, blocking=True, timeout=-1): - get_internal(self).args = blocking, timeout + _get_internal(self).args = blocking, timeout return self def __enter__(self): - args = get_internal(self).args + args = _get_internal(self).args - if not get_internal(self).lock.acquire(*args): + if not _get_internal(self).lock.acquire(*args): raise WouldWaitMore('Resource is still locked') - get_internal(self).locked = True + _get_internal(self).locked = True return self def __exit__(self, exc_type, exc_val, exc_tb): - get_internal(self).lock.release() - get_internal(self).locked = False + _get_internal(self).lock.release() + _get_internal(self).locked = False return False -def get_internal(self): +def _get_internal(self): return super(LockedDataset, self).__getattribute__('_LockedDataset__internal') diff --git a/satella/coding/deleters.py b/satella/coding/deleters.py index 5dc8777d..98d6d575 100644 --- a/satella/coding/deleters.py +++ b/satella/coding/deleters.py @@ -79,9 +79,11 @@ class DictDeleter: del self.dict_to_process[key] return False + DIR_FORWARD = 0 DIR_BACKWARD = 1 + class ListDeleter(tp.Generic[T]): """ Having problems deleting entries from your list while iterating on them? No problem. Just swap diff --git a/satella/coding/sequences/iterators.py b/satella/coding/sequences/iterators.py index b9a19ed9..68adeb5c 100644 --- a/satella/coding/sequences/iterators.py +++ b/satella/coding/sequences/iterators.py @@ -11,7 +11,7 @@ T, U = tp.TypeVar('T'), tp.TypeVar('U') IteratorOrIterable = tp.Union[tp.Iterator[T], tp.Iterable[T]] -def walk(obj: T, child_getter: tp.Callable[[T], tp.List[T]] = list, +def walk(obj: T, child_getter: tp.Callable[[T], tp.Optional[tp.List[T]]] = list, deep_first: bool = True, leafs_only: bool = False) -> tp.Iterator[T]: """ diff --git a/satella/coding/structures/mixins.py b/satella/coding/structures/mixins.py index 578aa175..3fd73a50 100644 --- a/satella/coding/structures/mixins.py +++ b/satella/coding/structures/mixins.py @@ -201,11 +201,10 @@ class ReprableMixin: def __repr__(self): fragments = [] - if hasattr(self, '_REPR_FULL_CLASSNAME'): - if self._REPR_FULL_CLASSNAME: - fragments = ['%s%s' % ((self.__class__.__module__ + '.') - if self.__class__.__module__ != 'builtins' else '', - self.__class__.__qualname__)] + if getattr(self, '_REPR_FULL_CLASSNAME', False): + fragments = ['%s%s' % ((self.__class__.__module__ + '.') + if self.__class__.__module__ != 'builtins' else '', + self.__class__.__qualname__)] if not fragments: fragments = [self.__class__.__name__] fragments.append('(') diff --git a/satella/exception_handling/__init__.py b/satella/exception_handling/__init__.py index 39502e18..0f728586 100644 --- a/satella/exception_handling/__init__.py +++ b/satella/exception_handling/__init__.py @@ -1,6 +1,7 @@ -from .dump_to_file import * -from .exception_handlers import * -from .global_eh import * +from .dump_to_file import DumpToFileHandler, AsStream +from .exception_handlers import BaseExceptionHandler, FunctionExceptionHandler, \ + exception_handler, NORMAL_PRIORITY, ALWAYS_FIRST, ALWAYS_LAST +from .global_eh import GlobalExcepthook from .memerrhandler import MemoryErrorExceptionHandler __all__ = [ diff --git a/satella/exception_handling/dump_to_file.py b/satella/exception_handling/dump_to_file.py index 64008a08..439b6820 100644 --- a/satella/exception_handling/dump_to_file.py +++ b/satella/exception_handling/dump_to_file.py @@ -8,9 +8,6 @@ from satella.coding import silence_excs from satella.instrumentation import Traceback from .exception_handlers import BaseExceptionHandler -__all__ = [ - 'DumpToFileHandler' -] AsStreamTypeAccept = tp.Union[str, tp.IO, None] AsStreamTypeAcceptHR = tp.Union[str, tp.TextIO] diff --git a/satella/exception_handling/exception_handlers.py b/satella/exception_handling/exception_handlers.py index 8bc0e5a4..95d1fc79 100644 --- a/satella/exception_handling/exception_handlers.py +++ b/satella/exception_handling/exception_handlers.py @@ -68,7 +68,7 @@ class FunctionExceptionHandler(BaseExceptionHandler): __slots__ = ('fun',) def __init__(self, fun: ExceptionHandlerCallable, priority: int = NORMAL_PRIORITY): - super(FunctionExceptionHandler, self).__init__(priority) + super().__init__(priority) self.fun = fun def handle_exception(self, type_, value, traceback): diff --git a/satella/processes.py b/satella/processes.py index afc5f549..e09d84b0 100644 --- a/satella/processes.py +++ b/satella/processes.py @@ -7,7 +7,7 @@ from .exceptions import ProcessFailed __all__ = ['call_and_return_stdout'] -def read_nowait(process: subprocess.Popen, output_list: tp.List[str]) -> None: +def _read_nowait(process: subprocess.Popen, output_list: tp.List[str]) -> None: """ To be launched as a daemon thread. This reads stdout and appends it's entries to a list. This should finish as soon as the process exits or closes it's stdout. @@ -58,7 +58,7 @@ def call_and_return_stdout(args: tp.Union[str, tp.List[str]], proc = subprocess.Popen(args, **kwargs) reader_thread = threading.Thread(name='stdout reader', - target=read_nowait, + target=_read_nowait, args=(proc, stdout_list), daemon=True) reader_thread.start() diff --git a/tests/test_coding/test_locked_dataset.py b/tests/test_coding/test_locked_dataset.py index d172313e..a7823c0c 100644 --- a/tests/test_coding/test_locked_dataset.py +++ b/tests/test_coding/test_locked_dataset.py @@ -1,7 +1,7 @@ import unittest from satella.coding.concurrent import LockedDataset -from satella.coding.concurrent.locked_dataset import get_internal +from satella.coding.concurrent.locked_dataset import _get_internal from satella.coding.structures import Singleton from satella.exceptions import WouldWaitMore, ResourceNotLocked @@ -30,7 +30,7 @@ class TestLockedDataset(unittest.TestCase): self.assertEqual(a.counter, 1) self.assertRaises(ResourceNotLocked, lambda: a.counter) - get_internal(a) + _get_internal(a) def test_locked_dataset_singleton(self): @Singleton -- GitLab