Skip to content

Commit 77c044e

Browse files
committed
Make import times significantly faster
Thanks @fjetter for identifying `toolz.curried.operator` as the main culprit in dask/distributed#6659
1 parent 32135de commit 77c044e

File tree

3 files changed

+14
-14
lines changed

3 files changed

+14
-14
lines changed

toolz/curried/operator.py

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,21 @@
22

33
import operator
44

5-
from toolz.functoolz import curry, num_required_args, has_keywords
6-
7-
8-
def should_curry(f):
9-
num = num_required_args(f)
10-
return num is None or num > 1 or num == 1 and has_keywords(f) is not False
5+
from toolz.functoolz import curry
116

127

8+
# Tests will catch if/when this needs updated
9+
IGNORE = {
10+
"__abs__", "__index__", "__inv__", "__invert__", "__neg__", "__not__",
11+
"__pos__", "_abs", "abs", "attrgetter", "index", "inv", "invert",
12+
"itemgetter", "neg", "not_", "pos", "truth"
13+
}
1314
locals().update(
14-
{name: curry(f) if should_curry(f) else f
15-
for name, f in vars(operator).items() if callable(f)},
15+
{name: f if name in IGNORE else curry(f)
16+
for name, f in vars(operator).items() if callable(f)}
1617
)
1718

1819
# Clean up the namespace.
20+
del IGNORE
1921
del curry
20-
del num_required_args
21-
del has_keywords
2222
del operator
23-
del should_curry

toolz/functoolz.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@
33
import sys
44
from operator import attrgetter, not_
55
from importlib import import_module
6-
from textwrap import dedent
76
from types import MethodType
8-
import sys
97

108
from .utils import no_default
119

@@ -780,6 +778,8 @@ def __call__(self, *args, **kwargs):
780778

781779
@instanceproperty(classval=__doc__)
782780
def __doc__(self):
781+
from textwrap import dedent
782+
783783
exc = self.exc
784784
try:
785785
if isinstance(exc, tuple):

toolz/itertoolz.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import operator
55
from functools import partial
66
from itertools import filterfalse, zip_longest
7-
from random import Random
87
from collections.abc import Sequence
98
from toolz.utils import no_default
109

@@ -1052,5 +1051,7 @@ def random_sample(prob, seq, random_state=None):
10521051
[7, 9, 19, 25, 30, 32, 34, 48, 59, 60, 81, 98]
10531052
"""
10541053
if not hasattr(random_state, 'random'):
1054+
from random import Random
1055+
10551056
random_state = Random(random_state)
10561057
return filter(lambda _: random_state.random() < prob, seq)

0 commit comments

Comments
 (0)