Skip to content

Commit 61af677

Browse files
committed
REFACT: don't import CONFIG UpFront ...
+ dropped from 17ms --> 3m `import composition`
1 parent ab4f886 commit 61af677

File tree

6 files changed

+31
-22
lines changed

6 files changed

+31
-22
lines changed

graphtik/base.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,13 @@ def __repr__(self):
9999
UNSET = Token("UNSET")
100100

101101

102+
def first_solid(*tristates, default=None):
103+
"""Utility combining multiple tri-state booleans."""
104+
from boltons.iterutils import first
105+
106+
return first(tristates, default=default, key=lambda i: i is not None)
107+
108+
102109
def aslist(i, argname, allowed_types=list):
103110
"""Utility to accept singular strings as lists, and None --> []."""
104111
if not i:

graphtik/composition.py

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import abc
1212
import itertools as itt
1313
import logging
14+
import re
1415
import textwrap
1516
from collections import abc as cabc
1617
from functools import wraps
@@ -27,18 +28,10 @@
2728
Token,
2829
aslist,
2930
astuple,
31+
first_solid,
3032
func_name,
3133
jetsam,
3234
)
33-
from .config import (
34-
first_solid,
35-
is_debug,
36-
is_endure_operations,
37-
is_marshal_tasks,
38-
is_parallel_tasks,
39-
is_reschedule_operations,
40-
reset_abort,
41-
)
4235
from .modifiers import (
4336
dep_singularized,
4437
dep_stripped,
@@ -352,6 +345,15 @@ def __repr__(self):
352345
"""
353346
Display more informative names for the Operation class
354347
"""
348+
from .config import (
349+
is_debug,
350+
is_endure_operations,
351+
is_marshal_tasks,
352+
is_parallel_tasks,
353+
is_reschedule_operations,
354+
reset_abort,
355+
)
356+
355357
needs = aslist(self.needs, "needs")
356358
provides = aslist(self.provides, "provides")
357359
aliases = aslist(self.aliases, "aliases")
@@ -386,6 +388,7 @@ def deps(self) -> Mapping[str, Collection]:
386388
387389
if not DEBUG, all deps are converted into lists, ready to be printed.
388390
"""
391+
from .config import is_debug
389392

390393
return {
391394
k: v if is_debug() else list(v)
@@ -423,6 +426,8 @@ def _prepare_match_inputs_error(
423426
varargs_bad: List,
424427
named_inputs: Mapping,
425428
) -> ValueError:
429+
from .config import is_debug
430+
426431
errors = [
427432
f"Need({n}) failed due to: {type(nex).__name__}({nex})"
428433
for n, nex in enumerate(exceptions, 1)
@@ -498,6 +503,8 @@ def _match_inputs_with_fn_needs(self, named_inputs) -> Tuple[list, list, dict]:
498503

499504
def _zip_results_with_provides(self, results) -> dict:
500505
"""Zip results with expected "real" (without sideffects) `provides`."""
506+
from .config import is_reschedule_operations
507+
501508
fn_expected: iset = self._fn_provides
502509
rescheduled = first_solid(is_reschedule_operations(), self.rescheduled)
503510
if self.returns_dict:
@@ -936,6 +943,7 @@ def __repr__(self):
936943
"""
937944
Display more informative names for the Operation class
938945
"""
946+
from .config import is_debug
939947
from .network import yield_ops
940948

941949
clsname = type(self).__name__
@@ -1142,6 +1150,8 @@ def compute(
11421150
11431151
See also :meth:`.Operation.compute()`.
11441152
"""
1153+
from .config import reset_abort
1154+
11451155
try:
11461156
if named_inputs is UNSET:
11471157
named_inputs = {}

graphtik/config.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -260,8 +260,3 @@ def get_execution_pool() -> "Optional[Pool]":
260260
a "reset" token (see :meth:`.ContextVar.set`)
261261
262262
."""
263-
264-
265-
def first_solid(*tristates, default=None):
266-
"""Utility combining multiple tri-state booleans."""
267-
return first(tristates, default=default, key=lambda i: i is not None)

graphtik/execution.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,11 @@
2121
Plottable,
2222
aslist,
2323
astuple,
24+
first_solid,
2425
jetsam,
2526
)
27+
from .composition import Operation
2628
from .config import (
27-
first_solid,
2829
get_execution_pool,
2930
is_abort,
3031
is_debug,
@@ -41,7 +42,6 @@
4142
yield_node_names,
4243
yield_ops,
4344
)
44-
from .composition import Operation
4545

4646
#: If this logger is *eventually* DEBUG-enabled,
4747
#: the string-representation of network-objects (network, plan, solution)

graphtik/plot.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,17 +42,16 @@
4242
import pydot
4343
from boltons.iterutils import default_enter, default_exit, get_path, remap
4444

45-
from .base import PlotArgs, func_name, func_source
45+
from .base import PlotArgs, first_solid, func_name, func_source
46+
from .composition import Operation
4647
from .config import (
47-
first_solid,
4848
is_debug,
4949
is_endure_operations,
5050
is_marshal_tasks,
5151
is_parallel_tasks,
5252
is_reschedule_operations,
5353
)
5454
from .modifiers import is_mapped, is_sfx, is_sfxed
55-
from .composition import Operation
5655

5756
log = logging.getLogger(__name__)
5857

test/conftest.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
import pytest
22

3-
from graphtik import compose, operation
4-
53
# Enable pytest-sphinx fixtures
64
# See https://www.sphinx-doc.org/en/master/devguide.html#unit-testing
75
pytest_plugins = "sphinx.testing.fixtures"
@@ -13,7 +11,7 @@
1311

1412
@pytest.fixture
1513
def debug_mode():
16-
from graphtik import debug_enabled
14+
from graphtik.config import debug_enabled
1715

1816
with debug_enabled(True):
1917
yield

0 commit comments

Comments
 (0)