Skip to content

Commit 454c58e

Browse files
authored
units: exposing "si" global var in pdoc-generated HTML; streamlining namedtuple definition with two loops; adding work, energy and force; closes #113 (#340)
1 parent e9ba9d8 commit 454c58e

File tree

2 files changed

+62
-20
lines changed

2 files changed

+62
-20
lines changed

PyPartMC/__init__.py

Lines changed: 44 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -24,27 +24,51 @@ def __build_extension_env():
2424
cookie.close()
2525

2626

27-
# TODO #113: 2 x loop over prefixes and units
28-
si = namedtuple("SI", ("m", "cm", "um", "nm", "kg", "g", "s", "K", "Pa", "hPa", "mol"))(
29-
m=1.0,
30-
cm=0.01,
31-
um=1e-6,
32-
nm=1e-9,
33-
kg=1.0,
34-
g=1e-3,
35-
s=1.0,
36-
K=1.0,
37-
Pa=1.0,
38-
hPa=100.0,
39-
mol=1.0,
40-
)
41-
""" TODO #113 """
27+
def __generate_si():
28+
prefixes = {
29+
"T": 1e12,
30+
"G": 1e9,
31+
"M": 1e6,
32+
"k": 1e3,
33+
"h": 1e2,
34+
"da": 1e1,
35+
"": 1e0,
36+
"d": 1e-1,
37+
"c": 1e-2,
38+
"m": 1e-3,
39+
"u": 1e-6,
40+
"n": 1e-9,
41+
"p": 1e-12,
42+
}
43+
units = {
44+
"m": 1e0,
45+
"g": 1e-3,
46+
"s": 1e0,
47+
"K": 1e0,
48+
"Pa": 1e0,
49+
"mol": 1e0,
50+
"W": 1e0,
51+
"J": 1e0,
52+
"N": 1e0,
53+
}
54+
return namedtuple("SI", [prefix + unit for prefix in prefixes for unit in units])(
55+
**{
56+
prefix_k + unit_k: prefix_v * unit_v
57+
for prefix_k, prefix_v in prefixes.items()
58+
for unit_k, unit_v in units.items()
59+
}
60+
)
61+
62+
63+
si = __generate_si()
64+
""" a utility namedtuple aimed at clrifying physics-related code by providing
65+
SI-prefix-aware unit multipliers, resulting in e.g.: `p = 1000 * si.hPa`
66+
notation. Note: no dimensional analysis is done! """
4267

4368
with __build_extension_env():
4469
import _PyPartMC
4570
from _PyPartMC import *
46-
from _PyPartMC import ( # pylint: disable=no-name-in-module
47-
__all__,
48-
__version__,
49-
__versions_of_build_time_dependencies__,
50-
)
71+
from _PyPartMC import __all__ as _PyPartMC_all # pylint: disable=no-name-in-module
72+
from _PyPartMC import __version__, __versions_of_build_time_dependencies__
73+
74+
__all__ = tuple([*_PyPartMC_all, "si"])

tests/test_units.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,12 @@ def test_length():
1313
@staticmethod
1414
def test_temperatur():
1515
assert ppmc.si.K == 1
16+
assert ppmc.si.pK == 1e-12
1617

1718
@staticmethod
1819
def test_time():
1920
assert ppmc.si.s == 1
21+
assert ppmc.si.us == 1e-6
2022

2123
@staticmethod
2224
def test_pressure():
@@ -26,8 +28,24 @@ def test_pressure():
2628
@staticmethod
2729
def test_amount():
2830
assert ppmc.si.mol == 1
31+
assert ppmc.si.Tmol == 1e12
2932

3033
@staticmethod
3134
def test_mass():
3235
assert ppmc.si.kg == 1
3336
assert ppmc.si.g == 1e-3
37+
38+
@staticmethod
39+
def test_energy():
40+
assert ppmc.si.J == 1
41+
assert ppmc.si.MJ == 1e6
42+
43+
@staticmethod
44+
def test_force():
45+
assert ppmc.si.N == 1
46+
assert ppmc.si.GN == 1e9
47+
48+
@staticmethod
49+
def test_work():
50+
assert ppmc.si.W == 1
51+
assert ppmc.si.uW == 1e-6

0 commit comments

Comments
 (0)