From de9266e0822884ad47546bb2502ce7f9fab20841 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Ma=C5=9Blanka?= <piotr.maslanka@henrietta.com.pl> Date: Mon, 29 Mar 2021 17:03:14 +0200 Subject: [PATCH] more `ThreadCollection` methods return self, permitting writing more conciser code --- .gitignore | 1 + CHANGELOG.md | 2 +- satella/__init__.py | 2 +- .../coding/concurrent/thread_collection.py | 27 ++++++++++++++----- 4 files changed, 24 insertions(+), 8 deletions(-) diff --git a/.gitignore b/.gitignore index 79cb29c0..78583dae 100644 --- a/.gitignore +++ b/.gitignore @@ -11,6 +11,7 @@ venv coverage.xml .coverage.* .metadata +test lock bin/ tmp/ diff --git a/CHANGELOG.md b/CHANGELOG.md index ba20c57c..6aebe3d0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,4 @@ # v2.15.2 * changed schema's file to return a file-object - +* more `ThreadCollection` methods return self, permitting writing more conciser code diff --git a/satella/__init__.py b/satella/__init__.py index 658de0a4..919cebbc 100644 --- a/satella/__init__.py +++ b/satella/__init__.py @@ -1 +1 @@ -__version__ = '2.15.2a2' +__version__ = '2.15.2a3' diff --git a/satella/coding/concurrent/thread_collection.py b/satella/coding/concurrent/thread_collection.py index e138022b..41110b98 100644 --- a/satella/coding/concurrent/thread_collection.py +++ b/satella/coding/concurrent/thread_collection.py @@ -73,43 +73,58 @@ class ThreadCollection: def __init__(self, threads: tp.Sequence[Thread]): self.threads = list(threads) - def append(self, thread: Thread) -> None: + def append(self, thread: Thread) -> 'ThreadCollection': """ Alias for :meth:`~satella.coding.concurrent.ThreadCollection.add` :param thread: thread to add + :returns: this thread collection instance """ self.add(thread) + return self - def add(self, thread: Thread) -> None: + def add(self, thread: Thread) -> 'ThreadCollection': """ Add a thread to the collection :param thread: thread to add + :returns: this thread collection instance """ self.threads.append(thread) + return self - def start(self) -> None: + def start(self) -> 'ThreadCollection': """ Start all threads + + :returns: this thread collection instance """ for thread in self.threads: thread.start() + return self - def terminate(self, *args, **kwargs) -> None: + def terminate(self, *args, **kwargs) -> 'ThreadCollection': """ Call terminate() on all threads that have this method + + :returns: this thread collection instance """ for thread in self.threads: try: thread.terminate(*args, **kwargs) except AttributeError: pass + return self - def join(self) -> None: - """Join all threads""" + def join(self) -> 'ThreadCollection': + """ + Join all threads + + :returns: this thread collection instance + """ for thread in self.threads: thread.join() + return self def is_alive(self) -> bool: """ -- GitLab