pyflyby 1.10.4__cp312-cp312-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- pyflyby/__init__.py +61 -0
- pyflyby/__main__.py +9 -0
- pyflyby/_autoimp.py +2228 -0
- pyflyby/_cmdline.py +591 -0
- pyflyby/_comms.py +221 -0
- pyflyby/_dbg.py +1383 -0
- pyflyby/_dynimp.py +154 -0
- pyflyby/_fast_iter_modules.cpython-312-x86_64-linux-gnu.so +0 -0
- pyflyby/_file.py +771 -0
- pyflyby/_flags.py +230 -0
- pyflyby/_format.py +186 -0
- pyflyby/_idents.py +227 -0
- pyflyby/_import_sorting.py +165 -0
- pyflyby/_importclns.py +658 -0
- pyflyby/_importdb.py +535 -0
- pyflyby/_imports2s.py +643 -0
- pyflyby/_importstmt.py +723 -0
- pyflyby/_interactive.py +2113 -0
- pyflyby/_livepatch.py +793 -0
- pyflyby/_log.py +107 -0
- pyflyby/_modules.py +646 -0
- pyflyby/_parse.py +1396 -0
- pyflyby/_py.py +2165 -0
- pyflyby/_saveframe.py +1145 -0
- pyflyby/_saveframe_reader.py +471 -0
- pyflyby/_util.py +458 -0
- pyflyby/_version.py +8 -0
- pyflyby/autoimport.py +20 -0
- pyflyby/etc/pyflyby/canonical.py +10 -0
- pyflyby/etc/pyflyby/common.py +27 -0
- pyflyby/etc/pyflyby/forget.py +10 -0
- pyflyby/etc/pyflyby/mandatory.py +10 -0
- pyflyby/etc/pyflyby/numpy.py +156 -0
- pyflyby/etc/pyflyby/std.py +335 -0
- pyflyby/importdb.py +19 -0
- pyflyby/libexec/pyflyby/colordiff +34 -0
- pyflyby/libexec/pyflyby/diff-colorize +148 -0
- pyflyby/share/emacs/site-lisp/pyflyby.el +112 -0
- pyflyby-1.10.4.data/scripts/collect-exports +76 -0
- pyflyby-1.10.4.data/scripts/collect-imports +58 -0
- pyflyby-1.10.4.data/scripts/find-import +38 -0
- pyflyby-1.10.4.data/scripts/prune-broken-imports +34 -0
- pyflyby-1.10.4.data/scripts/pyflyby-diff +34 -0
- pyflyby-1.10.4.data/scripts/reformat-imports +27 -0
- pyflyby-1.10.4.data/scripts/replace-star-imports +37 -0
- pyflyby-1.10.4.data/scripts/saveframe +299 -0
- pyflyby-1.10.4.data/scripts/tidy-imports +170 -0
- pyflyby-1.10.4.data/scripts/transform-imports +47 -0
- pyflyby-1.10.4.dist-info/METADATA +605 -0
- pyflyby-1.10.4.dist-info/RECORD +53 -0
- pyflyby-1.10.4.dist-info/WHEEL +6 -0
- pyflyby-1.10.4.dist-info/entry_points.txt +4 -0
- pyflyby-1.10.4.dist-info/licenses/LICENSE.txt +19 -0
pyflyby/_dynimp.py
ADDED
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Virtual module to create dynamic import at runtime.
|
|
3
|
+
|
|
4
|
+
It is sometime desirable to have auto import which are define only during
|
|
5
|
+
a session and never exist on a on-disk file.
|
|
6
|
+
|
|
7
|
+
This is injects a Dict module loader as well as a dictionary registry of in
|
|
8
|
+
memory module.
|
|
9
|
+
|
|
10
|
+
This is mostly use in IPython for lazy variable initialisation without having
|
|
11
|
+
to use proxy objects.
|
|
12
|
+
|
|
13
|
+
To use, put the following in your IPython startup files
|
|
14
|
+
(``~/.ipython/profile_default/startup/autoimp.py`), or in your IPython
|
|
15
|
+
configuration file:
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
.. code:: python
|
|
19
|
+
|
|
20
|
+
from pyflyby._dynimp import add_import
|
|
21
|
+
|
|
22
|
+
add_import("foo", "foo = 1")
|
|
23
|
+
|
|
24
|
+
add_import(
|
|
25
|
+
"df, data",
|
|
26
|
+
'''
|
|
27
|
+
import pandas as pd
|
|
28
|
+
data = [1,2,3]
|
|
29
|
+
df = pd.DataFrame(data)
|
|
30
|
+
''',
|
|
31
|
+
)
|
|
32
|
+
|
|
33
|
+
Now at the IPython prompt, if the pyflyby extension is loaded (either because
|
|
34
|
+
you started using the ``py`` cli, or some configuration options like ``ipython
|
|
35
|
+
--TerminalIPythonApp.extra_extensions=pyflyby``. When trying to use an undefined
|
|
36
|
+
variable like ``foo``, ``df`` or ``data``, the corresponding module will be
|
|
37
|
+
executed and the relevant variable imported.
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
"""
|
|
41
|
+
import importlib.abc
|
|
42
|
+
import importlib.util
|
|
43
|
+
import sys
|
|
44
|
+
|
|
45
|
+
from textwrap import dedent
|
|
46
|
+
from typing import FrozenSet
|
|
47
|
+
|
|
48
|
+
from pyflyby._importclns import ImportSet, Import
|
|
49
|
+
|
|
50
|
+
module_dict = {}
|
|
51
|
+
|
|
52
|
+
PYFLYBY_LAZY_LOAD_PREFIX = "from pyflyby_autoimport_"
|
|
53
|
+
|
|
54
|
+
def add_import(names: str, code: str, *, strict: bool = True):
|
|
55
|
+
"""
|
|
56
|
+
Add a runtime generated import module
|
|
57
|
+
|
|
58
|
+
Parameters
|
|
59
|
+
----------
|
|
60
|
+
names: str
|
|
61
|
+
name, or comma separated list variable names that should be created by
|
|
62
|
+
executing and importing `code`.
|
|
63
|
+
code: str
|
|
64
|
+
potentially multiline string that will be turned into a module,
|
|
65
|
+
executed and from which variables listed in names can be imported.
|
|
66
|
+
strict: bool
|
|
67
|
+
Raise in case of problem loading IPython of if pyflyby extension not installed.
|
|
68
|
+
otherwise just ignore error
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
Examples
|
|
73
|
+
--------
|
|
74
|
+
|
|
75
|
+
>>> add_import('pd, df', '''
|
|
76
|
+
... import pandas a pd
|
|
77
|
+
...
|
|
78
|
+
... df = pd.DataFrame([[1,2], [3,4]])
|
|
79
|
+
... ''', strict=False) # don't fail doctest
|
|
80
|
+
|
|
81
|
+
"""
|
|
82
|
+
try:
|
|
83
|
+
ip = _raise_if_problem()
|
|
84
|
+
except Exception:
|
|
85
|
+
if strict:
|
|
86
|
+
raise
|
|
87
|
+
else:
|
|
88
|
+
return
|
|
89
|
+
return _add_import(ip, names, code)
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
def _raise_if_problem():
|
|
93
|
+
try:
|
|
94
|
+
import IPython
|
|
95
|
+
except ModuleNotFoundError as e:
|
|
96
|
+
raise ImportError("Dynamic autoimport requires IPython to be installed") from e
|
|
97
|
+
|
|
98
|
+
ip = IPython.get_ipython()
|
|
99
|
+
if ip is None:
|
|
100
|
+
raise ImportError("Dynamic autoimport only work from within IPython")
|
|
101
|
+
|
|
102
|
+
if not hasattr(ip, "_auto_importer"):
|
|
103
|
+
raise ValueError(
|
|
104
|
+
"IPython needs to be loaded with pyflyby extension for lazy variable to work"
|
|
105
|
+
)
|
|
106
|
+
return ip
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
def _add_import(ip, names: str, code: str) -> None:
|
|
110
|
+
"""
|
|
111
|
+
private version of add_import
|
|
112
|
+
"""
|
|
113
|
+
assert ip is not None
|
|
114
|
+
module = PYFLYBY_LAZY_LOAD_PREFIX.split()[1]
|
|
115
|
+
mang = module + names.replace(",", "_").replace(" ", "_")
|
|
116
|
+
a: FrozenSet[Import] = ImportSet(f"from {mang} import {names}")._importset
|
|
117
|
+
b: FrozenSet[Import] = ip._auto_importer.db.known_imports._importset
|
|
118
|
+
s_import: FrozenSet[Import] = a | b
|
|
119
|
+
|
|
120
|
+
ip._auto_importer.db.known_imports = ImportSet._from_imports(list(s_import))
|
|
121
|
+
module_dict[mang] = dedent(code)
|
|
122
|
+
|
|
123
|
+
class DictLoader(importlib.abc.Loader):
|
|
124
|
+
"""
|
|
125
|
+
A dict based loader for in-memory module definition.
|
|
126
|
+
"""
|
|
127
|
+
def __init__(self, module_name, module_code):
|
|
128
|
+
self.module_name = module_name
|
|
129
|
+
self.module_code = module_code
|
|
130
|
+
|
|
131
|
+
def create_module(self, spec):
|
|
132
|
+
return None # Use default module creation semantics
|
|
133
|
+
|
|
134
|
+
def exec_module(self, module):
|
|
135
|
+
"""
|
|
136
|
+
we exec module code directly in memory
|
|
137
|
+
"""
|
|
138
|
+
exec(self.module_code, module.__dict__)
|
|
139
|
+
|
|
140
|
+
|
|
141
|
+
class DictFinder(importlib.abc.MetaPathFinder):
|
|
142
|
+
"""
|
|
143
|
+
A meta path finder for abode DictLoader
|
|
144
|
+
"""
|
|
145
|
+
def find_spec(self, fullname, path, target=None):
|
|
146
|
+
if fullname in module_dict:
|
|
147
|
+
module_code = module_dict[fullname]
|
|
148
|
+
loader = DictLoader(fullname, module_code)
|
|
149
|
+
return importlib.util.spec_from_loader(fullname, loader)
|
|
150
|
+
return None
|
|
151
|
+
|
|
152
|
+
|
|
153
|
+
def inject():
|
|
154
|
+
sys.meta_path.insert(0, DictFinder())
|
|
Binary file
|