Skip to content

Commit 22dc024

Browse files
authored
Merge pull request #534 from eriknw/faster_import
Make import times significantly faster
2 parents 32135de + 7880c57 commit 22dc024

File tree

4 files changed

+26
-22
lines changed

4 files changed

+26
-22
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)

toolz/tests/test_curried.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,18 @@ def test_module_name():
4141
assert toolz.curried.__name__ == 'toolz.curried'
4242

4343

44+
def should_curry(func):
45+
if not callable(func) or isinstance(func, toolz.curry):
46+
return False
47+
nargs = toolz.functoolz.num_required_args(func)
48+
if nargs is None or nargs > 1:
49+
return True
50+
return nargs == 1 and toolz.functoolz.has_keywords(func)
51+
52+
4453
def test_curried_operator():
54+
import operator
55+
4556
for k, v in vars(cop).items():
4657
if not callable(v):
4758
continue
@@ -60,6 +71,7 @@ def test_curried_operator():
6071
raise AssertionError(
6172
'toolz.curried.operator.%s is not curried!' % k,
6273
)
74+
assert should_curry(getattr(operator, k)) == isinstance(v, toolz.curry), k
6375

6476
# Make sure this isn't totally empty.
6577
assert len(set(vars(cop)) & {'add', 'sub', 'mul'}) == 3
@@ -69,14 +81,6 @@ def test_curried_namespace():
6981
exceptions = import_module('toolz.curried.exceptions')
7082
namespace = {}
7183

72-
def should_curry(func):
73-
if not callable(func) or isinstance(func, toolz.curry):
74-
return False
75-
nargs = toolz.functoolz.num_required_args(func)
76-
if nargs is None or nargs > 1:
77-
return True
78-
return nargs == 1 and toolz.functoolz.has_keywords(func)
79-
8084

8185
def curry_namespace(ns):
8286
return {

0 commit comments

Comments
 (0)