Skip to content

Commit a15345a

Browse files
committed
Move dictionaries to class level
The dictionaries are constant so can be shared by any and all instances of this class. Moving them into the class definition, but outside of the __init__ allows that to happen.
1 parent 6524f56 commit a15345a

1 file changed

Lines changed: 54 additions & 51 deletions

File tree

src/nuclearmasses/utils/converter.py

Lines changed: 54 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -9,30 +9,6 @@
99
# Typing hint Union for the different ways a file or data can be represented
1010
DataInput = Traversable | os.PathLike[str] | str | typing.TextIO
1111

12-
UNIT_TO_SECONDS: dict[str, float] = {
13-
# Base SI and common
14-
"s": 1.0,
15-
"ms": 1e-3,
16-
"us": 1e-6,
17-
"ns": 1e-9,
18-
"ps": 1e-12,
19-
"as": 1e-18,
20-
"zs": 1e-21,
21-
"ys": 1e-24,
22-
"min": 60.0,
23-
"h": 3600.0,
24-
"d": 86400.0,
25-
"yr": 31_557_600.0, # 365.25 days
26-
"kyr": 3.15576e10,
27-
"myr": 3.15576e13,
28-
"gyr": 3.15576e16,
29-
"zyr": 3.15576e21,
30-
"eyr": 3.15576e18,
31-
"pyr": 3.15576e15,
32-
"tyr": 3.15576e12,
33-
"yyr": 3.15576e24,
34-
}
35-
3612

3713
class Converter:
3814
"""A utility class for converting between symbol and Z value
@@ -41,46 +17,72 @@ class Converter:
4117
and the other symbol to Z.
4218
"""
4319

20+
UNIT_TO_SECONDS: dict[str, float] = {
21+
"s": 1.0,
22+
"ms": 1e-3,
23+
"us": 1e-6,
24+
"ns": 1e-9,
25+
"ps": 1e-12,
26+
"as": 1e-18,
27+
"zs": 1e-21,
28+
"ys": 1e-24,
29+
"min": 60.0,
30+
"h": 3600.0,
31+
"d": 86400.0,
32+
"yr": 31_557_600.0, # 365.25 days
33+
"kyr": 3.15576e10,
34+
"myr": 3.15576e13,
35+
"gyr": 3.15576e16,
36+
"zyr": 3.15576e21,
37+
"eyr": 3.15576e18,
38+
"pyr": 3.15576e15,
39+
"tyr": 3.15576e12,
40+
"yyr": 3.15576e24,
41+
}
42+
43+
# fmt: off
44+
# Formatter wants to put each item on it's own line, I don't
45+
Z_TO_SYMBOL: dict[int, str] = {
46+
0: "n", 1: "H", 2: "He", 3: "Li", 4: "Be", 5: "B", 6: "C", 7: "N", 8: "O", 9: "F",
47+
10: "Ne", 11: "Na", 12: "Mg", 13: "Al", 14: "Si", 15: "P", 16: "S", 17: "Cl", 18: "Ar", 19: "K",
48+
20: "Ca", 21: "Sc", 22: "Ti", 23: "V", 24: "Cr", 25: "Mn", 26: "Fe", 27: "Co", 28: "Ni", 29: "Cu",
49+
30: "Zn", 31: "Ga", 32: "Ge", 33: "As", 34: "Se", 35: "Br", 36: "Kr", 37: "Rb", 38: "Sr", 39: "Y",
50+
40: "Zr", 41: "Nb", 42: "Mo", 43: "Tc", 44: "Ru", 45: "Rh", 46: "Pd", 47: "Ag", 48: "Cd", 49: "In",
51+
50: "Sn", 51: "Sb", 52: "Te", 53: "I", 54: "Xe", 55: "Cs", 56: "Ba", 57: "La", 58: "Ce", 59: "Pr",
52+
60: "Nd", 61: "Pm", 62: "Sm", 63: "Eu", 64: "Gd", 65: "Tb", 66: "Dy", 67: "Ho", 68: "Er", 69: "Tm",
53+
70: "Yb", 71: "Lu", 72: "Hf", 73: "Ta", 74: "W", 75: "Re", 76: "Os", 77: "Ir", 78: "Pt", 79: "Au",
54+
80: "Hg", 81: "Tl", 82: "Pb", 83: "Bi", 84: "Po", 85: "At", 86: "Rn", 87: "Fr", 88: "Ra", 89: "Ac",
55+
90: "Th", 91: "Pa", 92: "U", 93: "Np", 94: "Pu", 95: "Am", 96: "Cm", 97: "Bk", 98: "Cf", 99: "Es",
56+
100: "Fm", 101: "Md", 102: "No", 103: "Lr", 104: "Rf", 105: "Db", 106: "Sg", 107: "Bh", 108: "Hs", 109: "Mt",
57+
110: "Ds", 111: "Rg", 112: "Cn", 113: "Ed", 114: "Fl", 115: "Ef", 116: "Lv", 117: "Ts", 118: "Og"
58+
}
59+
# fmt: on
60+
61+
# Switch the keys and values of the z_to_symbol dictionary so a user can access the Z value using the symbol
62+
SYMBOL_TO_Z: dict[str, int] = {val: key for key, val in Z_TO_SYMBOL.items()}
63+
4464
def __init__(self, **kwargs) -> None:
4565
"""Construct the symbol -> Z and Z -> symbol dictionaries."""
4666
# We are using multiple inheritance, so need this for MRO
4767
super().__init__(**kwargs)
48-
# fmt: off
49-
# Formatter wants to put each item on it's own line, I don't
50-
self.z_to_symbol: dict[int, str] = {
51-
0: "n", 1: "H", 2: "He", 3: "Li", 4: "Be", 5: "B", 6: "C", 7: "N", 8: "O", 9: "F",
52-
10: "Ne", 11: "Na", 12: "Mg", 13: "Al", 14: "Si", 15: "P", 16: "S", 17: "Cl", 18: "Ar", 19: "K",
53-
20: "Ca", 21: "Sc", 22: "Ti", 23: "V", 24: "Cr", 25: "Mn", 26: "Fe", 27: "Co", 28: "Ni", 29: "Cu",
54-
30: "Zn", 31: "Ga", 32: "Ge", 33: "As", 34: "Se", 35: "Br", 36: "Kr", 37: "Rb", 38: "Sr", 39: "Y",
55-
40: "Zr", 41: "Nb", 42: "Mo", 43: "Tc", 44: "Ru", 45: "Rh", 46: "Pd", 47: "Ag", 48: "Cd", 49: "In",
56-
50: "Sn", 51: "Sb", 52: "Te", 53: "I", 54: "Xe", 55: "Cs", 56: "Ba", 57: "La", 58: "Ce", 59: "Pr",
57-
60: "Nd", 61: "Pm", 62: "Sm", 63: "Eu", 64: "Gd", 65: "Tb", 66: "Dy", 67: "Ho", 68: "Er", 69: "Tm",
58-
70: "Yb", 71: "Lu", 72: "Hf", 73: "Ta", 74: "W", 75: "Re", 76: "Os", 77: "Ir", 78: "Pt", 79: "Au",
59-
80: "Hg", 81: "Tl", 82: "Pb", 83: "Bi", 84: "Po", 85: "At", 86: "Rn", 87: "Fr", 88: "Ra", 89: "Ac",
60-
90: "Th", 91: "Pa", 92: "U", 93: "Np", 94: "Pu", 95: "Am", 96: "Cm", 97: "Bk", 98: "Cf", 99: "Es",
61-
100: "Fm", 101: "Md", 102: "No", 103: "Lr", 104: "Rf", 105: "Db", 106: "Sg", 107: "Bh", 108: "Hs", 109: "Mt", # noqa: E501
62-
110: "Ds", 111: "Rg", 112: "Cn", 113: "Ed", 114: "Fl", 115: "Ef", 116: "Lv", 117: "Ts", 118: "Og"
63-
}
64-
# fmt: on
65-
66-
# Switch the keys and values of the z_to_symbol dictionary so a user can access the Z value using the symbol
67-
self.symbol_to_z: dict[str, int] = {val: key for key, val in self.z_to_symbol.items()}
68-
69-
def get_symbol(self, z: int) -> str | None:
68+
69+
@staticmethod
70+
def get_symbol(z: int) -> str | None:
7071
"""Get the symbol representing <z>
7172
7273
This is a nicely named, very thin wrapper around the inbuilt dictionary.
7374
I'm sure I was going to do something else in this function beyond basic accessing, but don't recall.
7475
Leave as is and hopefully I'll remember
7576
"""
76-
return self.z_to_symbol.get(z, None)
77+
return Converter.Z_TO_SYMBOL.get(z, None)
7778

78-
def get_z(self, symbol: str) -> int | None:
79+
@staticmethod
80+
def get_z(symbol: str) -> int | None:
7981
"""Get the z (proton number) representing <symbol>
8082
8183
This is a nicely named, very thin wrapper around the inbuilt dictionary.
8284
"""
83-
return self.symbol_to_z.get(symbol, None)
85+
return Converter.SYMBOL_TO_Z.get(symbol, None)
8486

8587
@staticmethod
8688
def normalise_symbol(symbol: str) -> str:
@@ -94,7 +96,8 @@ def normalise_symbol(symbol: str) -> str:
9496
"""
9597
return symbol.strip().title()
9698

97-
def unit_to_seconds(self, unit_str: str) -> float:
99+
@staticmethod
100+
def unit_to_seconds(unit_str: str) -> float:
98101
"""Convert a time unit to a scale factor in seconds.
99102
100103
Returns np.nan for non-valid time units.
@@ -113,7 +116,7 @@ def unit_to_seconds(self, unit_str: str) -> float:
113116
if not cleaned_unit:
114117
return np.nan
115118

116-
return UNIT_TO_SECONDS.get(cleaned_unit, np.nan)
119+
return Converter.UNIT_TO_SECONDS.get(cleaned_unit, np.nan)
117120

118121
@staticmethod
119122
def read_fwf(base: DataInput, **kwargs):

0 commit comments

Comments
 (0)