Skip to content

Commit 7ed7a3d

Browse files
committed
chore: add constants + cleanups
1 parent 42b17b1 commit 7ed7a3d

File tree

4 files changed

+43
-3
lines changed

4 files changed

+43
-3
lines changed

.pre-commit-config.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ repos:
77
- id: isort
88
- id: mypy
99
- id: check-yaml
10+
exclude: ^tests/js
1011
- id: end-of-file-fixer
1112
- id: trailing-whitespace
1213
- repo: local

src/py/mat3ra/code/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import Any, Dict, Optional
1+
from typing import Any, Dict
22

33
from mat3ra.utils import object as object_utils
44

@@ -26,7 +26,7 @@ def set_prop(self, name: str, value: Any) -> None:
2626
def unset_prop(self, name: str) -> None:
2727
del self._json[name]
2828

29-
def set_props(self, json: Dict[str, Any] = {}) -> "BaseUnderscoreJsonPropsHandler":
29+
def set_props(self, json: Dict[str, Any] = {}) -> Any:
3030
for key, value in json.items():
3131
self.set_prop(key, value)
3232
return self

src/py/mat3ra/code/constants.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,43 @@
1+
from math import pi
2+
3+
14
class Coefficients:
5+
# Same as used in: JS/TS
26
EV_TO_RY = 0.0734986176
37
BOHR_TO_ANGSTROM = 0.52917721092
48
ANGSTROM_TO_BOHR = 1 / 0.52917721092
59
EV_A_TO_RY_BOHR = 1 / 25.71104309541616
610

11+
# The below is migrated from:
12+
# https://github.com/Exabyte-io/express/blob/22614e549cdc3b0c344718b72ee2000383d77922/express/parsers/settings.py
13+
# and originally taken from https://github.com/hplgit/physical-quantities/blob/master/PhysicalQuantities.py
14+
15+
# Internal, for convenience purposes
16+
_c = 299792458.0 # speed of light, m/s
17+
_mu0 = 4.0e-7 * pi # permeability of vacuum
18+
_eps0 = 1 / _mu0 / _c**2 # permittivity of vacuum
19+
_Grav = 6.67259e-11 # gravitational constant
20+
_hplanck = 6.6260755e-34 # Planck constant, J s
21+
_hbar = _hplanck / (2 * pi) # Planck constant / 2pi, J s
22+
_e = 1.60217733e-19 # elementary charge
23+
_me = 9.1093897e-31 # electron mass
24+
_mp = 1.6726231e-27 # proton mass
25+
_Nav = 6.0221367e23 # Avogadro number
26+
_k = 1.380658e-23 # Boltzmann constant, J/K
27+
_amu = 1.6605402e-27 # atomic mass unit, kg
28+
29+
# External
30+
BOHR = 4e10 * pi * _eps0 * _hbar**2 / _me / _e**2 # Bohr radius in angstrom
31+
eV = 1.0
32+
HARTREE = _me * _e**3 / 16 / pi**2 / _eps0**2 / _hbar**2 # in eV
33+
RYDBERG = 0.5 * HARTREE # in eV
34+
Ry = RYDBERG
35+
Ha = HARTREE
36+
kJ = 1000.0 / _e
37+
kcal = 4.184 * kJ
38+
cm_inv_to_ev = 0.00012398 # cm^-1 to eV
39+
ry_bohr_to_eV_A = 25.71104309541616 # or RYDBERG / BOHR
40+
741

842
class Tolerance:
943
# in crystal coordinates

src/py/mat3ra/code/entity.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,12 @@ def to_json(self, exclude: List[str] = []) -> Dict[str, Any]:
4747
return self.clean(object_utils.clone_deep(object_utils.omit(self._json, exclude)))
4848

4949
def clone(self, extra_context: Dict[str, Any] = {}) -> Any:
50-
return self.__class__.__init__({**self.to_json(), **extra_context})
50+
config = self.to_json()
51+
config.update(extra_context)
52+
# To avoid:
53+
# Argument 1 to "__init__" of "BaseUnderscoreJsonPropsHandler" has incompatible type "Dict[str, Any]";
54+
# expected "BaseUnderscoreJsonPropsHandler"
55+
return self.__class__.__init__(config) # type: ignore[arg-type]
5156

5257
@staticmethod
5358
def validate_data(data: Dict[str, Any], clean: bool = False):

0 commit comments

Comments
 (0)