diff --git a/.pylintrc b/.pylintrc index b5260bf1af4e4e89e5d5db108a43922c9ee6beae..bff0404057560cd7e968dc1832f9951d30079fee 100644 --- a/.pylintrc +++ b/.pylintrc @@ -1,4 +1,7 @@ [MASTER] disable= C0114, # missing-module-docstring - C0116 # missing-function-docstring \ No newline at end of file + C0116, # missing-function-docstring + W0603, # global-statement + C0103 # invalid-name + diff --git a/.readthedocs.yaml b/.readthedocs.yaml index 9f0cbc4db9b01fdfdd3c55c75f5357dee2def636..1c60d8693072fa622e70c340a2002f8202aa5940 100644 --- a/.readthedocs.yaml +++ b/.readthedocs.yaml @@ -15,4 +15,4 @@ sphinx: python: install: - - requirements: requirements.txt \ No newline at end of file + - requirements: requirements.txt diff --git a/CHANGELOG.md b/CHANGELOG.md index 2d1967ce134b3fba8128bd1b9c6f181495b3bf9e..fe08aa8523e840f04dbd2f7ce22c3f195eb4d081 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,8 @@ The software * fixed behavior for AutoflushFile(..., 'wb') * added CPUTimeManager.set_refresh_each() * added satella.instrumentation.cpu_time.get_own_cpu_time() +* better documentation for satella.time.sleep() +* fixed behaviour of PIDFileLock Build process ------------- diff --git a/README.md b/README.md index 0a4226a7e4131094085658a9beabb861b099e8da..5b507eae82d3d22f1c2bbbca0027c97f63d9a7f2 100644 --- a/README.md +++ b/README.md @@ -9,6 +9,8 @@ satella [](https://pypi.python.org/pypi/satella) [](http://satella.readthedocs.io/en/latest/?badge=latest) [](https://github.com/piotrmaslanka/satella) +[](https://github.com/pylint-dev/pylint) + Satella is an almost-zero-requirements Python 3.5+ library for writing server applications. It has arisen out of my requirements to have some classes or design patterns handy, and kinda diff --git a/satella/debug/tainting/environment.py b/satella/debug/tainting/environment.py index f9352bd2d0a3d42399a34f750e09d559cb8e1f9c..57d2cdde81d58352b31df3e71055fe69426951cd 100644 --- a/satella/debug/tainting/environment.py +++ b/satella/debug/tainting/environment.py @@ -10,7 +10,7 @@ import warnings from satella.coding.typing import T -from satella.debug.tainting.tainteds import TaintedObject, access_tainted, taint +from .tainteds import TaintedObject, access_tainted, taint local = threading.local() diff --git a/satella/instrumentation/cpu_time/collector.py b/satella/instrumentation/cpu_time/collector.py index 47a4348d64f37975b128c5a4c08dd4e15de9cf8c..06648f93b58a97bc75f59efc01add765c04ffd91 100644 --- a/satella/instrumentation/cpu_time/collector.py +++ b/satella/instrumentation/cpu_time/collector.py @@ -22,7 +22,7 @@ pCPUtimes = collections.namedtuple('pcputimes', 'iowait']) -class CPUProfileBuilderThread(threading.Thread): +class _CPUProfileBuilderThread(threading.Thread): """ A CPU profile builder thread and a core singleton object to use. @@ -51,16 +51,16 @@ class CPUProfileBuilderThread(threading.Thread): @staticmethod def get_instance(): """Access instances of this thread in this way ONLY!""" - if CPUProfileBuilderThread.thread is None: - CPUProfileBuilderThread.thread = CPUProfileBuilderThread() - CPUProfileBuilderThread.thread.start() - return CPUProfileBuilderThread.thread + if _CPUProfileBuilderThread.thread is None: + _CPUProfileBuilderThread.thread = _CPUProfileBuilderThread() + _CPUProfileBuilderThread.thread.start() + return _CPUProfileBuilderThread.thread def start(self) -> None: if self.started: return - super().start() self.started = True + super().start() def save_load(self, times: pCPUtimes) -> None: while len(self.own_load_average) > 3 and \ @@ -131,7 +131,7 @@ class CPUTimeManager: :param percent: float between 0 and 1 :return: the value of the percentile """ - return CPUProfileBuilderThread.get_instance().percentile(percent) + return _CPUProfileBuilderThread.get_instance().percentile(percent) @staticmethod def set_window_size(window_size: float) -> None: @@ -140,13 +140,13 @@ class CPUTimeManager: :param window_size: time, in seconds """ - CPUProfileBuilderThread.get_instance().window_size = window_size + _CPUProfileBuilderThread.get_instance().window_size = window_size @staticmethod def set_refresh_each(refresh: tp.Union[str, float, int]) -> None: """Change the refresh interval for the CPU usage collection thread""" - CPUProfileBuilderThread.get_instance().refresh_each = parse_time_string(refresh) + _CPUProfileBuilderThread.get_instance().refresh_each = parse_time_string(refresh) @for_argument(parse_time_string) @@ -234,7 +234,7 @@ def calculate_occupancy_factor() -> float: :return: a float between 0 and 1 telling you how occupied CPU-wise is your system. """ - CPUProfileBuilderThread.get_instance() + _CPUProfileBuilderThread.get_instance() c = _calculate_occupancy_factor() while c is None: time.sleep(0.1) @@ -250,4 +250,4 @@ def get_own_cpu_usage() -> tp.Optional[pCPUtimes]: passed since the last measure. or None if data not yet ready """ - return CPUProfileBuilderThread.get_instance().get_own_cpu_usage() + return _CPUProfileBuilderThread.get_instance().get_own_cpu_usage() diff --git a/satella/instrumentation/cpu_time/concurrency.py b/satella/instrumentation/cpu_time/concurrency.py index aefc676e90b9f2a10d09998399d43574b507d9a5..a4247b36e2e4fd589624e3e0545d60b9912a0eaf 100644 --- a/satella/instrumentation/cpu_time/concurrency.py +++ b/satella/instrumentation/cpu_time/concurrency.py @@ -2,7 +2,7 @@ import typing as tp from abc import abstractmethod, ABCMeta from satella.coding.concurrent import IntervalTerminableThread -from .collector import sleep_cpu_aware, CPUProfileBuilderThread +from .collector import sleep_cpu_aware, _CPUProfileBuilderThread from satella.time import measure, parse_time_string @@ -31,7 +31,7 @@ class CPUTimeAwareIntervalTerminableThread(IntervalTerminableThread, metaclass=A self.seconds = parse_time_string(seconds) self.wakeup_interval = parse_time_string(wakeup_interval) self.max_sooner = max_sooner or seconds * 0.1 - cp_bt = CPUProfileBuilderThread.get_instance() + cp_bt = _CPUProfileBuilderThread.get_instance() cp_bt.request_percentile(percentile) self.percentile = percentile super().__init__(seconds, *args, **kwargs) @@ -48,7 +48,7 @@ class CPUTimeAwareIntervalTerminableThread(IntervalTerminableThread, metaclass=A return measurement() def __sleep_waiting_for_cpu(self, how_long: float) -> None: - cp_bt = CPUProfileBuilderThread.get_instance() + cp_bt = _CPUProfileBuilderThread.get_instance() per_val = cp_bt.percentile(self.percentile) while how_long > 0 and not self._terminating: diff --git a/satella/instrumentation/metrics/metric_types/measurable_mixin.py b/satella/instrumentation/metrics/metric_types/measurable_mixin.py index a7c0c88740d79bbda93167a676d2c66a33958cc4..5b3c519e736076044867175eed35c2aeb5957c9b 100644 --- a/satella/instrumentation/metrics/metric_types/measurable_mixin.py +++ b/satella/instrumentation/metrics/metric_types/measurable_mixin.py @@ -2,7 +2,7 @@ import inspect import time from concurrent.futures import Future -from satella.coding import wraps +from satella.coding.decorators.decorators import wraps from satella.coding.typing import NoArgCallable from .base import MetricLevel diff --git a/satella/os/pidlock.py b/satella/os/pidlock.py index 176895fffb68f28998a43ec555f534d7b20e8196..177895cf7ced3a96e20d3ef5388432b94bc1f240 100644 --- a/satella/os/pidlock.py +++ b/satella/os/pidlock.py @@ -76,6 +76,7 @@ class PIDFileLock: fd = os.fdopen(self.file_no, 'w') fd.write(str(os.getpid()) + '\n') fd.close() + return True def __enter__(self) -> None: self.acquire() diff --git a/satella/time/measure.py b/satella/time/measure.py index 06131b8ab07e6cb4aa32cf54e404233521485ad5..c5a91f199be7a90dbe9de4a650b1a215b62443be 100644 --- a/satella/time/measure.py +++ b/satella/time/measure.py @@ -1,5 +1,4 @@ from __future__ import annotations -from satella.exceptions import WouldWaitMore import typing as tp import time import copy @@ -7,11 +6,12 @@ import inspect import warnings from concurrent.futures import Future from functools import wraps # import from functools to prevent circular import exception +from satella.exceptions import WouldWaitMore TimeSignal = tp.Callable[[], float] -class measure: +class measure: # pylint: disable=invalid-name """ A class used to measure time elapsed. Use for example like this: @@ -92,7 +92,7 @@ class measure: def __init__(self, future_to_measure: tp.Optional[Future] = None, stop_on_stop: bool = True, adjust: float = 0.0, time_getter_callable: TimeSignal = time.monotonic, create_stopped: bool = False, - timeout: tp.Optional[float] = None): + timeout: tp.Optional[float] = None): # pylint: disable=too-many-arguments self.time_getter_callable = time_getter_callable self.timeout = timeout self.started_on = time_getter_callable() + adjust diff --git a/satella/time/misc.py b/satella/time/misc.py index b194c17e7d8e03e5e7dd200b498069f894649085..a7a16c1f4ebc33cb73d491e1824307a3f4bfca81 100644 --- a/satella/time/misc.py +++ b/satella/time/misc.py @@ -3,8 +3,6 @@ import typing as tp __all__ = ['time_as_int', 'time_ms', 'sleep', 'time_us'] -from satella.coding.concurrent.thread import Condition -from satella.exceptions import WouldWaitMore from satella.time.parse import parse_time_string from satella.time.measure import measure @@ -19,11 +17,12 @@ def sleep(y: tp.Union[str, float], abort_on_interrupt: bool = False) -> bool: :param y: the interval to wait in seconds, can be also a time string :param abort_on_interrupt: whether to abort at once when KeyboardInterrupt is seen :returns: whether the function has completed its sleep naturally. False is seen on - aborts thanks to KeyboardInterrupt only if abort_on_interrupt is True + aborts thanks to KeyboardInterrupt only if abort_on_interrupt is True. False will be also + seen on negative y. """ y = parse_time_string(y) if y < 0: - return + return False with measure() as measurement: while measurement() < y: