Przeglądaj źródła

Remove some itertools/operator usage

Ask Solem 12 lat temu
rodzic
commit
64146c4d8c
3 zmienionych plików z 15 dodań i 15 usunięć
  1. 2 3
      celery/app/builtins.py
  2. 2 3
      celery/utils/__init__.py
  3. 11 9
      celery/utils/functional.py

+ 2 - 3
celery/app/builtins.py

@@ -11,7 +11,6 @@ from __future__ import absolute_import
 from __future__ import with_statement
 
 from collections import deque
-from itertools import starmap
 
 from celery._state import get_current_worker_task
 from celery.utils import uuid
@@ -127,7 +126,7 @@ def add_map_task(app):
     @app.task(name='celery.map', _force_evaluate=True)
     def xmap(task, it):
         task = subtask(task).type
-        return list(map(task, it))
+        return [task(value) for value in it]
     return xmap
 
 
@@ -138,7 +137,7 @@ def add_starmap_task(app):
     @app.task(name='celery.starmap', _force_evaluate=True)
     def xstarmap(task, it):
         task = subtask(task).type
-        return list(starmap(task, it))
+        return [task(*args) for args in it]
     return xstarmap
 
 

+ 2 - 3
celery/utils/__init__.py

@@ -9,7 +9,6 @@
 from __future__ import absolute_import
 from __future__ import with_statement
 
-import operator
 import os
 import sys
 import traceback
@@ -17,7 +16,7 @@ import warnings
 import types
 import datetime
 
-from functools import partial, wraps
+from functools import wraps
 from inspect import getargspec
 from pprint import pprint
 
@@ -133,7 +132,7 @@ def fun_takes_kwargs(fun, kwlist=[]):
     args, _varargs, keywords, _defaults = argspec
     if keywords is not None:
         return kwlist
-    return filter(partial(operator.contains, args), kwlist)
+    return [kw for kw in kwlist if kw in args]
 
 
 def isatty(fh):

+ 11 - 9
celery/utils/functional.py

@@ -9,7 +9,6 @@
 from __future__ import absolute_import
 from __future__ import with_statement
 
-import operator
 import threading
 
 from functools import partial, wraps
@@ -17,12 +16,11 @@ from itertools import islice
 
 from kombu.utils import cached_property
 from kombu.utils.functional import promise, maybe_promise
-from kombu.utils.compat import OrderedDict
+from kombu.utils.compat import OrderedDict, next
 
 from .compat import UserDict, UserList
 
 KEYWORD_MARK = object()
-is_not_None = partial(operator.is_not, None)
 
 
 class LRUCache(UserDict):
@@ -173,13 +171,17 @@ def noop(*args, **kwargs):
     pass
 
 
-def first(predicate, iterable):
+def first(predicate, it):
     """Returns the first element in `iterable` that `predicate` returns a
-    :const:`True` value for."""
-    predicate = predicate or is_not_None
-    for item in iterable:
-        if predicate(item):
-            return item
+    :const:`True` value for.
+
+    If `predicate` is None it will return the first item that is not None.
+
+    """
+    return next(
+        (v for v in it if (predicate(v) if predicate else v is not None)),
+        None,
+    )
 
 
 def firstmethod(method):