Skip to content

Commit ae6c319

Browse files
committed
Rework arc atom lazy import to be simpler by using a constructor function
instead of a dict of imported classes.
1 parent d09dbad commit ae6c319

2 files changed

Lines changed: 38 additions & 52 deletions

File tree

src/rydiqule/atom_utils.py

Lines changed: 36 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -4,55 +4,44 @@
44

55
from scipy.constants import epsilon_0, hbar, c
66
import numpy as np
7-
import re
7+
import re
88
from .sensor_utils import expand_statespec
99

10-
from typing import Union, Optional, Literal, Tuple, List, Dict, Callable, NamedTuple, Any
11-
12-
from .exceptions import RydiquleError
13-
14-
__all__ = [
15-
'QSpec',
16-
'A_QState',
17-
'QState',
18-
'D1_excited',
19-
'D1_states',
20-
'D2_excited',
21-
'D2_states',
22-
'ground_state',
23-
'expand_qnums',
24-
'calc_eta',
25-
'calc_kappa',
26-
'validate_qnums',
27-
]
28-
__lazy_loads = [
29-
'ATOMS'
30-
]
31-
32-
def __dir__() -> List[str]:
33-
34-
return sorted(__all__ + __lazy_loads)
35-
36-
37-
def __getattr__(name: str) -> Any:
38-
39-
if name == 'ATOMS':
40-
import arc.alkali_atom_data as arc_atoms
41-
42-
ATOMS = {
43-
'H': arc_atoms.Hydrogen,
44-
'Li6': arc_atoms.Lithium6, 'Li7': arc_atoms.Lithium7,
45-
'Na': arc_atoms.Sodium,
46-
'K39': arc_atoms.Potassium39, 'K40': arc_atoms.Potassium40, 'K41': arc_atoms.Potassium41,
47-
'Rb85': arc_atoms.Rubidium85, 'Rb87': arc_atoms.Rubidium87,
48-
'Cs': arc_atoms.Caesium
49-
}
50-
"""
51-
Alkali atoms defined by ARC that can be used with :class:`~.Cell`.
52-
"""
53-
return ATOMS
54-
else:
55-
raise AttributeError(f'Module {__name__} does not have attribute {name}')
10+
from typing import Union, Optional, Literal, Tuple, List, Dict, Callable, NamedTuple, TYPE_CHECKING
11+
12+
from .exceptions import RydiquleError, AtomError
13+
14+
if TYPE_CHECKING:
15+
import arc.alkali_atom_data
16+
17+
ATOMS = {
18+
'H': 'Hydrogen',
19+
'Li6': 'Lithium6', 'Li7': 'Lithium7',
20+
'Na': 'Sodium',
21+
'K39': 'Potassium39', 'K40': 'Potassium40', 'K41': 'Potassium41',
22+
'Rb85': 'Rubidium85', 'Rb87': 'Rubidium87',
23+
'Cs': 'Caesium'
24+
}
25+
"""
26+
Alkali atoms defined by ARC that can be used with :class:`~.Cell`.
27+
"""
28+
29+
def _load_arc_atom(atom_flag: str) -> 'arc.alkali_atom_data.Alkali_Atom':
30+
"""
31+
Function that lazy loads ARC atoms from :external+arc:module:`~arc.alkali_atom_data`
32+
33+
Returns
34+
-------
35+
arc.alkali_atom_data.Alkali_Atom
36+
ARC alkali atom class associated with the provided atom_flag.
37+
"""
38+
39+
import arc.alkali_atom_data as arc_atoms
40+
41+
if atom_flag not in ATOMS.keys():
42+
raise AtomError(f"Atom flag must be one of {ATOMS.keys()}")
43+
44+
return getattr(arc_atoms, ATOMS[atom_flag])() # instantiate the class here
5645

5746

5847
ground_n = {

src/rydiqule/cell.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
from .sensor import Sensor
1515
from .sensor_utils import scale_dipole
1616
from .sensor_utils import ScannableParameter, TimeFunc
17-
from .atom_utils import calc_kappa, calc_eta, expand_qnums, validate_qnums, A_QState, ground_state
17+
from .atom_utils import calc_kappa, calc_eta, expand_qnums, validate_qnums, A_QState, ground_state, _load_arc_atom
1818
from .arc_utils import RQ_AlkaliAtom
1919
from .exceptions import RydiquleError, AtomError, CouplingNotAllowedError
2020
from .exceptions import RydiquleWarning, debug_state
@@ -165,12 +165,9 @@ def __init__(self, atom_flag: AtomFlags, atomic_states: List[A_QState],
165165
(5, 0, 0.5, f=2.0, m_f=2.0)
166166
167167
"""
168-
from .atom_utils import ATOMS
169-
if atom_flag not in ATOMS.keys():
170-
raise AtomError(f"Atom flag must be one of {ATOMS.keys()}")
171168

172169
self.atom_flag = atom_flag
173-
self.atom = RQ_AlkaliAtom(ATOMS[atom_flag]())
170+
self.atom = RQ_AlkaliAtom(_load_arc_atom(atom_flag))
174171
self.I = self.atom.arc_atom.I
175172

176173
#prepare states by expanding statespec into list and intialize graph

0 commit comments

Comments
 (0)