er2 0.7.0__py3-none-any.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.
- er2/__init__.py +33 -0
- er2/__main__.py +86 -0
- er2/_lazy.py +88 -0
- er2/backends/__init__.py +4 -0
- er2/backends/pari_backend.py +1146 -0
- er2/backends/pari_closures.py +111 -0
- er2/backends/sympy_backend.py +220 -0
- er2/data/pari_functions.csv +1169 -0
- er2/dispatch.py +741 -0
- er2/importer.py +71 -0
- er2/kernel.py +113 -0
- er2/oeis.py +285 -0
- er2/prelude.py +118 -0
- er2/preparser.py +342 -0
- er2/printing.py +276 -0
- er2/runtime/__init__.py +1 -0
- er2/runtime/dirichlet.py +229 -0
- er2/runtime/factorization.py +97 -0
- er2/runtime/finite_field.py +440 -0
- er2/runtime/modular.py +246 -0
- er2/runtime/number_field.py +293 -0
- er2/runtime/numbers.py +214 -0
- er2/runtime/qfb.py +104 -0
- er2/session.py +227 -0
- er2-0.7.0.dist-info/METADATA +292 -0
- er2-0.7.0.dist-info/RECORD +29 -0
- er2-0.7.0.dist-info/WHEEL +4 -0
- er2-0.7.0.dist-info/entry_points.txt +3 -0
- er2-0.7.0.dist-info/licenses/LICENSE +21 -0
er2/__init__.py
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
"""ER2 — mathematical Python.
|
|
2
|
+
|
|
3
|
+
Importing ``er2`` from Python has no side effects; ER2 printing and
|
|
4
|
+
``.er2`` imports are enabled only by the ER2 entry points (the ``er2``
|
|
5
|
+
command, the ER2 kernel, ``%load_ext er2``) or explicitly with
|
|
6
|
+
``er2.session.start()``.
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
from importlib.metadata import PackageNotFoundError, version
|
|
10
|
+
|
|
11
|
+
from er2.preparser import preparse
|
|
12
|
+
from er2.printing import latex, show
|
|
13
|
+
from er2.runtime.numbers import Integer, Rational
|
|
14
|
+
|
|
15
|
+
try:
|
|
16
|
+
__version__ = version("er2") # single source: pyproject.toml
|
|
17
|
+
except PackageNotFoundError: # running from a source tree, not installed
|
|
18
|
+
__version__ = "0+unknown"
|
|
19
|
+
__all__ = ["Integer", "Rational", "latex", "preparse", "show"]
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
def load_ipython_extension(shell):
|
|
23
|
+
"""Enable ER2 in IPython: ``%load_ext er2``."""
|
|
24
|
+
from er2.session import load_ipython_extension
|
|
25
|
+
|
|
26
|
+
load_ipython_extension(shell)
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
def unload_ipython_extension(shell):
|
|
30
|
+
"""Disable ER2 in IPython: ``%unload_ext er2``."""
|
|
31
|
+
from er2.session import unload_ipython_extension
|
|
32
|
+
|
|
33
|
+
unload_ipython_extension(shell)
|
er2/__main__.py
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
"""The ``er2`` command.
|
|
2
|
+
|
|
3
|
+
Usage::
|
|
4
|
+
|
|
5
|
+
er2 start the interactive console
|
|
6
|
+
er2 FILE.er2 [ARGS...] run an ER2 program
|
|
7
|
+
er2 --show-python FILE.er2 print the Python generated for FILE
|
|
8
|
+
er2 kernel install [--user | --sys-prefix | --prefix DIR]
|
|
9
|
+
install the ER2 Jupyter kernel
|
|
10
|
+
er2 --version
|
|
11
|
+
"""
|
|
12
|
+
|
|
13
|
+
import sys
|
|
14
|
+
|
|
15
|
+
import er2
|
|
16
|
+
|
|
17
|
+
USAGE = __doc__.split("Usage::", 1)[1].rstrip()
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
def main(argv=None):
|
|
21
|
+
"""Run the ``er2`` command; return the exit code."""
|
|
22
|
+
args = list(sys.argv[1:] if argv is None else argv)
|
|
23
|
+
if not args:
|
|
24
|
+
from er2.session import repl
|
|
25
|
+
|
|
26
|
+
repl()
|
|
27
|
+
return 0
|
|
28
|
+
first = args[0]
|
|
29
|
+
if first in ("-h", "--help"):
|
|
30
|
+
print(f"usage:{USAGE}")
|
|
31
|
+
return 0
|
|
32
|
+
if first == "--version":
|
|
33
|
+
print(f"er2 {er2.__version__}")
|
|
34
|
+
return 0
|
|
35
|
+
if first == "--show-python":
|
|
36
|
+
return _show_python(args[1:])
|
|
37
|
+
if first == "kernel":
|
|
38
|
+
return _kernel(args[1:])
|
|
39
|
+
if first.startswith("-"):
|
|
40
|
+
print(f"er2: unknown option {first}\nusage:{USAGE}", file=sys.stderr)
|
|
41
|
+
return 2
|
|
42
|
+
from er2.session import run_file
|
|
43
|
+
|
|
44
|
+
return run_file(first, args[1:])
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
def _show_python(args):
|
|
48
|
+
"""Print the Python translation of an ER2 file."""
|
|
49
|
+
if len(args) != 1:
|
|
50
|
+
print("usage: er2 --show-python FILE.er2", file=sys.stderr)
|
|
51
|
+
return 2
|
|
52
|
+
with open(args[0], encoding="utf-8") as fh:
|
|
53
|
+
print(er2.preparse(fh.read(), args[0]), end="")
|
|
54
|
+
return 0
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
def _kernel(args):
|
|
58
|
+
"""Handle ``er2 kernel install [...]``."""
|
|
59
|
+
if not args or args[0] != "install":
|
|
60
|
+
print(
|
|
61
|
+
"usage: er2 kernel install [--user | --sys-prefix | --prefix DIR]",
|
|
62
|
+
file=sys.stderr,
|
|
63
|
+
)
|
|
64
|
+
return 2
|
|
65
|
+
options = args[1:]
|
|
66
|
+
user, prefix = True, None
|
|
67
|
+
if "--sys-prefix" in options:
|
|
68
|
+
user, prefix = False, sys.prefix
|
|
69
|
+
elif "--prefix" in options:
|
|
70
|
+
index = options.index("--prefix")
|
|
71
|
+
if index + 1 >= len(options):
|
|
72
|
+
print("er2: --prefix needs a directory", file=sys.stderr)
|
|
73
|
+
return 2
|
|
74
|
+
user, prefix = False, options[index + 1]
|
|
75
|
+
try:
|
|
76
|
+
from er2.kernel import install_kernel
|
|
77
|
+
except ImportError as exc:
|
|
78
|
+
print(f"er2: {exc}", file=sys.stderr)
|
|
79
|
+
return 1
|
|
80
|
+
path = install_kernel(user=user, prefix=prefix)
|
|
81
|
+
print(f"Installed the ER2 kernel in {path}")
|
|
82
|
+
return 0
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
if __name__ == "__main__":
|
|
86
|
+
sys.exit(main())
|
er2/_lazy.py
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
"""Load SymPy only when it is needed.
|
|
2
|
+
|
|
3
|
+
Importing SymPy takes about 0.3 s, most of an ER2 program's startup. A
|
|
4
|
+
program that only uses numbers and PARI never needs it, so ER2 modules
|
|
5
|
+
never import it at module level; they call ``sympy()`` when they need it.
|
|
6
|
+
|
|
7
|
+
``sympy_loaded()`` is True once SymPy has been imported by anyone, ER2 or
|
|
8
|
+
user code. Until then no object can be a SymPy object, so type checks
|
|
9
|
+
can skip it (``is_sympy``, ``is_symbolic``). ``when_sympy_loaded(hook)``
|
|
10
|
+
runs ``hook`` as soon as SymPy is imported (ER2's printer is installed
|
|
11
|
+
this way, D11).
|
|
12
|
+
"""
|
|
13
|
+
|
|
14
|
+
import importlib.abc
|
|
15
|
+
import importlib.machinery
|
|
16
|
+
import sys
|
|
17
|
+
|
|
18
|
+
_hooks = []
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
def sympy_loaded():
|
|
22
|
+
"""Whether SymPy has been imported (by ER2 or by user code)."""
|
|
23
|
+
module = sys.modules.get("sympy")
|
|
24
|
+
return module is not None and hasattr(module, "Basic")
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
def is_symbolic(obj):
|
|
28
|
+
"""Whether ``obj`` is a SymPy object other than a plain number."""
|
|
29
|
+
if not sympy_loaded():
|
|
30
|
+
return False # no SymPy object can exist yet
|
|
31
|
+
return isinstance(obj, sys.modules["sympy"].Basic) and not obj.is_Number
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
def is_sympy(obj):
|
|
35
|
+
"""Whether ``obj`` is a SymPy object (always False before SymPy loads)."""
|
|
36
|
+
return sympy_loaded() and isinstance(obj, sys.modules["sympy"].Basic)
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
def sympy():
|
|
40
|
+
"""Import SymPy (if needed) and return the module."""
|
|
41
|
+
import sympy as module
|
|
42
|
+
|
|
43
|
+
_run_hooks()
|
|
44
|
+
return module
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
def when_sympy_loaded(hook):
|
|
48
|
+
"""Run ``hook()`` now if SymPy is loaded, else right after it loads."""
|
|
49
|
+
if sympy_loaded():
|
|
50
|
+
hook()
|
|
51
|
+
return
|
|
52
|
+
if hook not in _hooks:
|
|
53
|
+
_hooks.append(hook)
|
|
54
|
+
if not any(isinstance(f, _SympyFinder) for f in sys.meta_path):
|
|
55
|
+
sys.meta_path.insert(0, _SympyFinder())
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
def cancel(hook):
|
|
59
|
+
"""Forget a hook registered with ``when_sympy_loaded``."""
|
|
60
|
+
if hook in _hooks:
|
|
61
|
+
_hooks.remove(hook)
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
def _run_hooks():
|
|
65
|
+
while _hooks:
|
|
66
|
+
_hooks.pop(0)()
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
class _SympyFinder(importlib.abc.MetaPathFinder):
|
|
70
|
+
"""Run the hooks after ``import sympy``, whoever imports it."""
|
|
71
|
+
|
|
72
|
+
def find_spec(self, name, path=None, target=None):
|
|
73
|
+
"""Wrap the loader of the top-level ``sympy`` package."""
|
|
74
|
+
if name != "sympy":
|
|
75
|
+
return None
|
|
76
|
+
sys.meta_path.remove(self)
|
|
77
|
+
spec = importlib.machinery.PathFinder.find_spec(name, path)
|
|
78
|
+
if spec is None or spec.loader is None:
|
|
79
|
+
return spec
|
|
80
|
+
loader = spec.loader
|
|
81
|
+
original = loader.exec_module
|
|
82
|
+
|
|
83
|
+
def exec_module(module):
|
|
84
|
+
original(module)
|
|
85
|
+
_run_hooks()
|
|
86
|
+
|
|
87
|
+
loader.exec_module = exec_module
|
|
88
|
+
return spec
|
er2/backends/__init__.py
ADDED