mcad2py 0.2.0__tar.gz → 0.4.0__tar.gz
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.
- {mcad2py-0.2.0/mcad2py.egg-info → mcad2py-0.4.0}/PKG-INFO +1 -1
- {mcad2py-0.2.0 → mcad2py-0.4.0}/mcad2py/cli.py +16 -1
- mcad2py-0.4.0/mcad2py/const.py +59 -0
- {mcad2py-0.2.0 → mcad2py-0.4.0}/mcad2py/convert.py +10 -3
- {mcad2py-0.2.0 → mcad2py-0.4.0}/mcad2py/emit/codegen.py +423 -151
- {mcad2py-0.2.0 → mcad2py-0.4.0}/mcad2py/emit/notebook_backend.py +56 -13
- {mcad2py-0.2.0 → mcad2py-0.4.0}/mcad2py/emit/py_backend.py +53 -11
- {mcad2py-0.2.0 → mcad2py-0.4.0}/mcad2py/ir.py +228 -7
- {mcad2py-0.2.0 → mcad2py-0.4.0}/mcad2py/loader.py +10 -0
- mcad2py-0.4.0/mcad2py/mapping.py +405 -0
- {mcad2py-0.2.0 → mcad2py-0.4.0}/mcad2py/parser/expressions.py +141 -14
- {mcad2py-0.2.0 → mcad2py-0.4.0}/mcad2py/parser/namespaces.py +5 -0
- {mcad2py-0.2.0 → mcad2py-0.4.0}/mcad2py/parser/regions.py +551 -7
- mcad2py-0.4.0/mcad2py/runtime.py +3907 -0
- {mcad2py-0.2.0 → mcad2py-0.4.0}/mcad2py/shapes.py +58 -1
- mcad2py-0.4.0/mcad2py/units.py +15 -0
- {mcad2py-0.2.0 → mcad2py-0.4.0/mcad2py.egg-info}/PKG-INFO +1 -1
- {mcad2py-0.2.0 → mcad2py-0.4.0}/mcad2py.egg-info/SOURCES.txt +2 -0
- {mcad2py-0.2.0 → mcad2py-0.4.0}/pyproject.toml +1 -1
- mcad2py-0.2.0/mcad2py/mapping.py +0 -204
- mcad2py-0.2.0/mcad2py/runtime.py +0 -1490
- {mcad2py-0.2.0 → mcad2py-0.4.0}/LICENSE +0 -0
- {mcad2py-0.2.0 → mcad2py-0.4.0}/MANIFEST.in +0 -0
- {mcad2py-0.2.0 → mcad2py-0.4.0}/README.md +0 -0
- {mcad2py-0.2.0 → mcad2py-0.4.0}/mcad2py/__init__.py +0 -0
- {mcad2py-0.2.0 → mcad2py-0.4.0}/mcad2py/emit/__init__.py +0 -0
- {mcad2py-0.2.0 → mcad2py-0.4.0}/mcad2py/parser/__init__.py +0 -0
- {mcad2py-0.2.0 → mcad2py-0.4.0}/mcad2py/text.py +0 -0
- {mcad2py-0.2.0 → mcad2py-0.4.0}/mcad2py.egg-info/dependency_links.txt +0 -0
- {mcad2py-0.2.0 → mcad2py-0.4.0}/mcad2py.egg-info/entry_points.txt +0 -0
- {mcad2py-0.2.0 → mcad2py-0.4.0}/mcad2py.egg-info/requires.txt +0 -0
- {mcad2py-0.2.0 → mcad2py-0.4.0}/mcad2py.egg-info/top_level.txt +0 -0
- {mcad2py-0.2.0 → mcad2py-0.4.0}/setup.cfg +0 -0
|
@@ -4,16 +4,25 @@ from __future__ import annotations
|
|
|
4
4
|
|
|
5
5
|
import argparse
|
|
6
6
|
import sys
|
|
7
|
+
from importlib.metadata import version
|
|
7
8
|
from pathlib import Path
|
|
8
9
|
|
|
9
10
|
from .convert import convert_file
|
|
10
11
|
|
|
12
|
+
__version__ = version("mcad2py")
|
|
13
|
+
|
|
11
14
|
|
|
12
15
|
def main(argv: list[str] | None = None) -> int:
|
|
13
16
|
parser = argparse.ArgumentParser(
|
|
14
17
|
prog="mcad2py",
|
|
15
18
|
description="Convert PTC Mathcad Prime (.mcdx) worksheets to Python / Jupyter.",
|
|
16
19
|
)
|
|
20
|
+
parser.add_argument(
|
|
21
|
+
"-v",
|
|
22
|
+
"--version",
|
|
23
|
+
action="version",
|
|
24
|
+
version=f"%(prog)s {__version__}",
|
|
25
|
+
)
|
|
17
26
|
sub = parser.add_subparsers(dest="command", required=True)
|
|
18
27
|
|
|
19
28
|
conv = sub.add_parser("convert", help="convert a .mcdx file")
|
|
@@ -30,6 +39,12 @@ def main(argv: list[str] | None = None) -> int:
|
|
|
30
39
|
choices=["notebook", "py"],
|
|
31
40
|
help="output format (default: inferred from -o suffix, else notebook).",
|
|
32
41
|
)
|
|
42
|
+
conv.add_argument(
|
|
43
|
+
"--trace-source",
|
|
44
|
+
action="store_true",
|
|
45
|
+
help="annotate each statement with its Mathcad worksheet region id "
|
|
46
|
+
"(and original name, if renamed).",
|
|
47
|
+
)
|
|
33
48
|
|
|
34
49
|
args = parser.parse_args(argv)
|
|
35
50
|
if args.command == "convert":
|
|
@@ -41,7 +56,7 @@ def main(argv: list[str] | None = None) -> int:
|
|
|
41
56
|
def _run_convert(args: argparse.Namespace) -> int:
|
|
42
57
|
fmt = args.format or _infer_format(args.output)
|
|
43
58
|
try:
|
|
44
|
-
result = convert_file(args.input, fmt=fmt)
|
|
59
|
+
result = convert_file(args.input, fmt=fmt, trace_source=args.trace_source)
|
|
45
60
|
except (OSError, ValueError) as exc:
|
|
46
61
|
# The loader raises these with user-facing messages (missing file, not a
|
|
47
62
|
# zip, no worksheet.xml), so print the message rather than a traceback.
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
"""Mathcad's built-in physical constants, as importable Pint quantities.
|
|
2
|
+
|
|
3
|
+
Generated code names these rather than inlining their values, so a formula reads
|
|
4
|
+
the way the worksheet does::
|
|
5
|
+
|
|
6
|
+
E = m * c**2 # not m * (299792458 * ureg.m / ureg.s)**2
|
|
7
|
+
|
|
8
|
+
They behave like Mathcad's constants do: in scope from the top of the sheet, and
|
|
9
|
+
shadowed the moment the worksheet defines a variable of the same name -- which
|
|
10
|
+
is simply Python assignment, since the import sits in the module header. (Prime
|
|
11
|
+
keeps the two apart by *label* even then; a sheet that defines its own ``c`` and
|
|
12
|
+
still refers to the constant ``c`` below it is the one case where that
|
|
13
|
+
divergence shows. No fixture does.)
|
|
14
|
+
|
|
15
|
+
Values are the CODATA/SI numbers Prime itself caches, in **base SI units** --
|
|
16
|
+
which is how ``result.xml`` states them, and what makes a scaled display
|
|
17
|
+
override (``10⁻³⁴ kg·m²/s``) divide down to exactly Mathcad's number.
|
|
18
|
+
|
|
19
|
+
The Mathcad display name for each is in :data:`mcad2py.mapping.CONSTANTS`, which
|
|
20
|
+
is what maps ``ℏ`` to ``hbar`` and ``R_∞`` to ``R_inf``.
|
|
21
|
+
"""
|
|
22
|
+
|
|
23
|
+
from __future__ import annotations
|
|
24
|
+
|
|
25
|
+
from .units import ureg
|
|
26
|
+
|
|
27
|
+
#: Speed of light in vacuum.
|
|
28
|
+
c = 299792458 * ureg.m / ureg.s
|
|
29
|
+
#: Standard acceleration of gravity.
|
|
30
|
+
g = 9.80665 * ureg.m / ureg.s**2
|
|
31
|
+
#: Elementary charge (Mathcad spells it ``e_c``, to leave ``e`` to Euler).
|
|
32
|
+
e_c = 1.602176634e-19 * ureg.coulomb
|
|
33
|
+
#: Planck constant.
|
|
34
|
+
h = 6.62607015e-34 * ureg.kg * ureg.m**2 / ureg.s
|
|
35
|
+
#: Reduced Planck constant, Mathcad's ``ℏ``. Prime carries the rounded
|
|
36
|
+
#: 1.054571817e-34 rather than h/(2π), and the cache is held to it.
|
|
37
|
+
hbar = 1.054571817e-34 * ureg.kg * ureg.m**2 / ureg.s
|
|
38
|
+
#: Boltzmann constant.
|
|
39
|
+
k = 1.380649e-23 * ureg.kg * ureg.m**2 / (ureg.s**2 * ureg.K)
|
|
40
|
+
#: Atomic mass unit.
|
|
41
|
+
m_u = 1.66053906892e-27 * ureg.kg
|
|
42
|
+
#: Avogadro constant.
|
|
43
|
+
N_A = 6.02214076e23 / ureg.mol
|
|
44
|
+
#: Molar gas constant.
|
|
45
|
+
R = 8.314462618 * ureg.kg * ureg.m**2 / (ureg.s**2 * ureg.K * ureg.mol)
|
|
46
|
+
#: Rydberg constant, Mathcad's ``R_∞``.
|
|
47
|
+
R_inf = 10973731.56816 / ureg.m
|
|
48
|
+
#: Fine-structure constant (dimensionless).
|
|
49
|
+
alpha = 0.0072973525643
|
|
50
|
+
#: Euler-Mascheroni constant, Mathcad's ``γ`` (dimensionless).
|
|
51
|
+
gamma = 0.5772156649015329
|
|
52
|
+
#: Vacuum electric permittivity.
|
|
53
|
+
epsilon_0 = 8.8541878188e-12 * ureg.A**2 * ureg.s**4 / (ureg.kg * ureg.m**3)
|
|
54
|
+
#: Vacuum magnetic permeability.
|
|
55
|
+
mu_0 = 1.25663706127e-6 * ureg.kg * ureg.m / (ureg.s**2 * ureg.A**2)
|
|
56
|
+
#: Stefan-Boltzmann constant.
|
|
57
|
+
sigma = 5.670374419e-8 * ureg.kg / (ureg.s**3 * ureg.K**4)
|
|
58
|
+
#: Magnetic flux quantum.
|
|
59
|
+
Phi_0 = 2.067833848e-15 * ureg.weber
|
|
@@ -23,6 +23,8 @@ def convert_worksheet(pkg: McdxPackage) -> ir.Worksheet:
|
|
|
23
23
|
pkg.worksheet_xml,
|
|
24
24
|
text_resolver=resolve_text,
|
|
25
25
|
image_resolver=pkg.image,
|
|
26
|
+
integration_xml=pkg.integration_xml,
|
|
27
|
+
result_xml=pkg.result_xml,
|
|
26
28
|
)
|
|
27
29
|
|
|
28
30
|
|
|
@@ -30,12 +32,17 @@ def convert_file(
|
|
|
30
32
|
path: str | Path,
|
|
31
33
|
*,
|
|
32
34
|
fmt: str = "notebook",
|
|
35
|
+
trace_source: bool = False,
|
|
33
36
|
) -> str:
|
|
34
|
-
"""Convert a ``.mcdx`` file to source. ``fmt`` is ``"notebook"`` or ``"py"``.
|
|
37
|
+
"""Convert a ``.mcdx`` file to source. ``fmt`` is ``"notebook"`` or ``"py"``.
|
|
38
|
+
|
|
39
|
+
``trace_source`` annotates each generated statement with a back-reference
|
|
40
|
+
to its originating Mathcad worksheet region (see ``--trace-source``).
|
|
41
|
+
"""
|
|
35
42
|
pkg = load_mcdx(path)
|
|
36
43
|
ws = convert_worksheet(pkg)
|
|
37
44
|
if fmt == "py":
|
|
38
|
-
return to_python(ws)
|
|
45
|
+
return to_python(ws, trace_source=trace_source)
|
|
39
46
|
if fmt == "notebook":
|
|
40
|
-
return to_ipynb_string(ws)
|
|
47
|
+
return to_ipynb_string(ws, trace_source=trace_source)
|
|
41
48
|
raise ValueError(f"unknown format: {fmt!r} (expected 'notebook' or 'py')")
|