From 55f833ab6f0e5f9404cb64f4a824d1453929b7d6 Mon Sep 17 00:00:00 2001 From: lowercase00 <21188280+lowercase00@users.noreply.github.com> Date: Sat, 28 Jan 2023 21:47:55 -0300 Subject: [PATCH] Moves the `compact` helper to `utils` (#1769) * Moves the `compact` helper to `utils` There is a helper funcion that excludes `None` values from a list. This was being declared both in the Queue and in the worker. This centralizes this helper in the `utils` Importing * Fix Type Annotation --- rq/queue.py | 5 +---- rq/utils.py | 12 ++++++++++++ rq/worker.py | 5 +---- 3 files changed, 14 insertions(+), 8 deletions(-) diff --git a/rq/queue.py b/rq/queue.py index 3cdfcb68..08da3b1e 100644 --- a/rq/queue.py +++ b/rq/queue.py @@ -19,7 +19,7 @@ from .defaults import DEFAULT_RESULT_TTL from .exceptions import DequeueTimeout, NoSuchJobError from .job import Job, JobStatus from .serializers import resolve_serializer -from .utils import backend_class, get_version, import_attribute, make_colorizer, parse_timeout, utcnow +from .utils import backend_class, get_version, import_attribute, make_colorizer, parse_timeout, utcnow, compact green = make_colorizer('darkgreen') @@ -29,9 +29,6 @@ blue = make_colorizer('darkblue') logger = logging.getLogger("rq.queue") -def compact(lst): - return [item for item in lst if item is not None] - class EnqueueData(namedtuple('EnqueueData', ["func", "args", "kwargs", "timeout", "result_ttl", "ttl", "failure_ttl", diff --git a/rq/utils.py b/rq/utils.py index e347ec3b..a9fbb1d7 100644 --- a/rq/utils.py +++ b/rq/utils.py @@ -125,6 +125,18 @@ class ColorizingStreamHandler(logging.StreamHandler): return message +def compact(lst: t.List[t.Any]) -> t.List[t.Any]: + """Excludes `None` values from a list-like object. + + Args: + lst (list): A list (or list-like) oject + + Returns: + object (list): The list without None values + """ + return [item for item in lst if item is not None] + + def as_text(v): if v is None: return None diff --git a/rq/worker.py b/rq/worker.py index c5ecdf02..278b3eec 100644 --- a/rq/worker.py +++ b/rq/worker.py @@ -45,7 +45,7 @@ from .scheduler import RQScheduler from .suspension import is_suspended from .timeouts import JobTimeoutException, HorseMonitorTimeoutException, UnixSignalDeathPenalty from .utils import (backend_class, ensure_list, get_version, - make_colorizer, utcformat, utcnow, utcparse) + make_colorizer, utcformat, utcnow, utcparse, compact) from .version import VERSION from .worker_registration import clean_worker_registry, get_keys from .serializers import resolve_serializer @@ -67,9 +67,6 @@ class StopRequested(Exception): pass -def compact(a_list): - return [x for x in a_list if x is not None] - _signames = dict((getattr(signal, signame), signame) for signame in dir(signal)