Skip to content

Commit 16be4ff

Browse files
othallaauvipy
authored andcommitted
Small code improvements (celery#5239)
* small cleanup in events.cursemon * small cleanup in celery.__init__ * small cleanup in loaders.base * small cleanup in celery canvas
1 parent ac32997 commit 16be4ff

File tree

4 files changed

+62
-28
lines changed

4 files changed

+62
-28
lines changed

celery/__init__.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -118,17 +118,20 @@ def _patch_gevent():
118118
_signal.signal = gevent_signal
119119

120120

121-
def maybe_patch_concurrency(argv=sys.argv,
122-
short_opts=['-P'], long_opts=['--pool'],
123-
patches={'eventlet': _patch_eventlet,
124-
'gevent': _patch_gevent}):
121+
def maybe_patch_concurrency(argv=None, short_opts=None,
122+
long_opts=None, patches=None):
125123
"""Apply eventlet/gevent monkeypatches.
126124
127125
With short and long opt alternatives that specify the command line
128126
option to set the pool, this makes sure that anything that needs
129127
to be patched is completed as early as possible.
130128
(e.g., eventlet/gevent monkey patches).
131129
"""
130+
argv = argv if argv else sys.argv
131+
short_opts = short_opts if short_opts else ['-P']
132+
long_opts = long_opts if long_opts else ['--pool']
133+
patches = patches if patches else {'eventlet': _patch_eventlet,
134+
'gevent': _patch_gevent}
132135
try:
133136
pool = _find_option_with_arg(argv, short_opts, long_opts)
134137
except KeyError:

celery/canvas.py

Lines changed: 43 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -185,17 +185,19 @@ def delay(self, *partial_args, **partial_kwargs):
185185
"""Shortcut to :meth:`apply_async` using star arguments."""
186186
return self.apply_async(partial_args, partial_kwargs)
187187

188-
def apply(self, args=(), kwargs={}, **options):
188+
def apply(self, args=None, kwargs=None, **options):
189189
"""Call task locally.
190190
191191
Same as :meth:`apply_async` but executed the task inline instead
192192
of sending a task message.
193193
"""
194+
args = args if args else ()
195+
kwargs = kwargs if kwargs else {}
194196
# For callbacks: extra args are prepended to the stored args.
195197
args, kwargs, options = self._merge(args, kwargs, options)
196198
return self.type.apply(args, kwargs, **options)
197199

198-
def apply_async(self, args=(), kwargs={}, route_name=None, **options):
200+
def apply_async(self, args=None, kwargs=None, route_name=None, **options):
199201
"""Apply this task asynchronously.
200202
201203
Arguments:
@@ -210,6 +212,8 @@ def apply_async(self, args=(), kwargs={}, route_name=None, **options):
210212
See also:
211213
:meth:`[email protected]_async` and the :ref:`guide-calling` guide.
212214
"""
215+
args = args if args else ()
216+
kwargs = kwargs if kwargs else {}
213217
try:
214218
_apply = self._apply_async
215219
except IndexError: # pragma: no cover
@@ -224,15 +228,18 @@ def apply_async(self, args=(), kwargs={}, route_name=None, **options):
224228
# Borks on this, as it's a property
225229
return _apply(args, kwargs, **options)
226230

227-
def _merge(self, args=(), kwargs={}, options={}, force=False):
231+
def _merge(self, args=None, kwargs=None, options=None, force=False):
232+
args = args if args else ()
233+
kwargs = kwargs if kwargs else {}
234+
options = options if options else {}
228235
if self.immutable and not force:
229236
return (self.args, self.kwargs,
230237
dict(self.options, **options) if options else self.options)
231238
return (tuple(args) + tuple(self.args) if args else self.args,
232239
dict(self.kwargs, **kwargs) if kwargs else self.kwargs,
233240
dict(self.options, **options) if options else self.options)
234241

235-
def clone(self, args=(), kwargs={}, **opts):
242+
def clone(self, args=None, kwargs=None, **opts):
236243
"""Create a copy of this signature.
237244
238245
Arguments:
@@ -241,6 +248,8 @@ def clone(self, args=(), kwargs={}, **opts):
241248
options (Dict): Partial options to be merged with
242249
existing options.
243250
"""
251+
args = args if args else ()
252+
kwargs = kwargs if kwargs else {}
244253
# need to deepcopy options so origins links etc. is not modified.
245254
if args or kwargs or opts:
246255
args, kwargs, opts = self._merge(args, kwargs, opts)
@@ -554,20 +563,24 @@ def unchain_tasks(self):
554563
task.link_error(sig)
555564
return tasks
556565

557-
def apply_async(self, args=(), kwargs={}, **options):
566+
def apply_async(self, args=None, kwargs=None, **options):
558567
# python is best at unpacking kwargs, so .run is here to do that.
568+
args = args if args else ()
569+
kwargs = kwargs if kwargs else []
559570
app = self.app
560571
if app.conf.task_always_eager:
561572
with allow_join_result():
562573
return self.apply(args, kwargs, **options)
563574
return self.run(args, kwargs, app=app, **(
564575
dict(self.options, **options) if options else self.options))
565576

566-
def run(self, args=(), kwargs={}, group_id=None, chord=None,
577+
def run(self, args=None, kwargs=None, group_id=None, chord=None,
567578
task_id=None, link=None, link_error=None, publisher=None,
568579
producer=None, root_id=None, parent_id=None, app=None, **options):
569580
# pylint: disable=redefined-outer-name
570581
# XXX chord is also a class in outer scope.
582+
args = args if args else ()
583+
kwargs = kwargs if kwargs else []
571584
app = app or self.app
572585
use_link = self._use_link
573586
if use_link is None and app.conf.task_protocol == 1:
@@ -707,7 +720,9 @@ def prepare_steps(self, args, kwargs, tasks,
707720
prev_res = node
708721
return tasks, results
709722

710-
def apply(self, args=(), kwargs={}, **options):
723+
def apply(self, args=None, kwargs=None, **options):
724+
args = args if args else ()
725+
kwargs = kwargs if kwargs else {}
711726
last, (fargs, fkwargs) = None, (args, kwargs)
712727
for task in self.tasks:
713728
res = task.clone(fargs, fkwargs).apply(
@@ -808,8 +823,10 @@ def __init__(self, task, it, **options):
808823
{'task': task, 'it': regen(it)}, immutable=True, **options
809824
)
810825

811-
def apply_async(self, args=(), kwargs={}, **opts):
826+
def apply_async(self, args=None, kwargs=None, **opts):
812827
# need to evaluate generators
828+
args = args if args else ()
829+
kwargs = kwargs if kwargs else {}
813830
task, it = self._unpack_args(self.kwargs)
814831
return self.type.apply_async(
815832
(), {'task': task, 'it': list(it)},
@@ -871,7 +888,9 @@ def __init__(self, task, it, n, **options):
871888
def __call__(self, **options):
872889
return self.apply_async(**options)
873890

874-
def apply_async(self, args=(), kwargs={}, **opts):
891+
def apply_async(self, args=None, kwargs=None, **opts):
892+
args = args if args else ()
893+
kwargs = kwargs if kwargs else {}
875894
return self.group().apply_async(
876895
args, kwargs,
877896
route_name=task_name_from(self.kwargs.get('task')), **opts
@@ -965,8 +984,9 @@ def skew(self, start=1.0, stop=None, step=1.0):
965984
task.set(countdown=next(it))
966985
return self
967986

968-
def apply_async(self, args=(), kwargs=None, add_to_parent=True,
987+
def apply_async(self, args=None, kwargs=None, add_to_parent=True,
969988
producer=None, link=None, link_error=None, **options):
989+
args = args if args else ()
970990
if link is not None:
971991
raise TypeError('Cannot add link to group: use a chord')
972992
if link_error is not None:
@@ -1000,7 +1020,9 @@ def apply_async(self, args=(), kwargs=None, add_to_parent=True,
10001020
parent_task.add_trail(result)
10011021
return result
10021022

1003-
def apply(self, args=(), kwargs={}, **options):
1023+
def apply(self, args=None, kwargs=None, **options):
1024+
args = args if args else ()
1025+
kwargs = kwargs if kwargs else {}
10041026
app = self.app
10051027
if not self.tasks:
10061028
return self.freeze() # empty group returns GroupResult
@@ -1184,7 +1206,9 @@ def _unpack_args(header=None, body=None, **kwargs):
11841206
return (header, body), kwargs
11851207

11861208
def __init__(self, header, body=None, task='celery.chord',
1187-
args=(), kwargs={}, app=None, **options):
1209+
args=None, kwargs=None, app=None, **options):
1210+
args = args if args else ()
1211+
kwargs = kwargs if kwargs else {}
11881212
Signature.__init__(
11891213
self, task, args,
11901214
{'kwargs': kwargs, 'header': _maybe_group(header, app),
@@ -1220,10 +1244,11 @@ def freeze(self, _id=None, group_id=None, chord=None,
12201244
self.id = self.tasks.id
12211245
return bodyres
12221246

1223-
def apply_async(self, args=(), kwargs={}, task_id=None,
1247+
def apply_async(self, args=None, kwargs=None, task_id=None,
12241248
producer=None, publisher=None, connection=None,
12251249
router=None, result_cls=None, **options):
1226-
kwargs = kwargs or {}
1250+
args = args if args else ()
1251+
kwargs = kwargs if kwargs else {}
12271252
args = (tuple(args) + tuple(self.args)
12281253
if args and not self.immutable else self.args)
12291254
body = kwargs.pop('body', None) or self.kwargs['body']
@@ -1239,7 +1264,10 @@ def apply_async(self, args=(), kwargs={}, task_id=None,
12391264
# chord([A, B, ...], C)
12401265
return self.run(tasks, body, args, task_id=task_id, **options)
12411266

1242-
def apply(self, args=(), kwargs={}, propagate=True, body=None, **options):
1267+
def apply(self, args=None, kwargs=None,
1268+
propagate=True, body=None, **options):
1269+
args = args if args else ()
1270+
kwargs = kwargs if kwargs else {}
12431271
body = self.body if body is None else body
12441272
tasks = (self.tasks.clone() if isinstance(self.tasks, group)
12451273
else group(self.tasks, app=self.app))

celery/events/cursesmon.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,7 @@ def draw(self):
349349
self.handle_keypress()
350350
x = LEFT_BORDER_OFFSET
351351
y = blank_line = count(2)
352-
my, mx = win.getmaxyx()
352+
my, _ = win.getmaxyx()
353353
win.erase()
354354
win.bkgd(' ', curses.color_pair(1))
355355
win.border()
@@ -360,7 +360,7 @@ def draw(self):
360360
curses.A_BOLD | curses.A_UNDERLINE)
361361
tasks = self.tasks
362362
if tasks:
363-
for row, (uuid, task) in enumerate(tasks):
363+
for row, (_, task) in enumerate(tasks):
364364
if row > self.display_height:
365365
break
366366

celery/loaders/base.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -161,13 +161,16 @@ def _import_config_module(self, name):
161161
def find_module(self, module):
162162
return find_module(module)
163163

164-
def cmdline_config_parser(
165-
self, args, namespace='celery',
166-
re_type=re.compile(r'\((\w+)\)'),
167-
extra_types={'json': json.loads},
168-
override_types={'tuple': 'json',
169-
'list': 'json',
170-
'dict': 'json'}):
164+
def cmdline_config_parser(self, args, namespace='celery',
165+
re_type=re.compile(r'\((\w+)\)'),
166+
extra_types=None,
167+
override_types=None):
168+
extra_types = extra_types if extra_types else {'json': json.loads}
169+
override_types = override_types if override_types else {
170+
'tuple': 'json',
171+
'list': 'json',
172+
'dict': 'json'
173+
}
171174
from celery.app.defaults import Option, NAMESPACES
172175
namespace = namespace and namespace.lower()
173176
typemap = dict(Option.typemap, **extra_types)

0 commit comments

Comments
 (0)