Skip to content

Commit

Permalink
Terminals can now be added to global namespace upon creation of the d…
Browse files Browse the repository at this point in the history
…evice object.
  • Loading branch information
THuckemann committed May 3, 2024
1 parent 3098b1d commit 3708551
Showing 1 changed file with 23 additions and 7 deletions.
30 changes: 23 additions & 7 deletions src/qumada/measurement/device_object.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,22 +30,36 @@ def is_measurement_script(o):


class QumadaDevice:
def __init__(self):
def __init__(self,

Check warning on line 33 in src/qumada/measurement/device_object.py

View check run for this annotation

Codecov / codecov/patch

src/qumada/measurement/device_object.py#L32-L33

Added lines #L32 - L33 were not covered by tests
make_terminals_global=True,
namespace=None,):
self.namespace = namespace or globals()
self.terminals = {}
self.instrument_parameters = {}
self.make_terminals_global = make_terminals_global

Check warning on line 39 in src/qumada/measurement/device_object.py

View check run for this annotation

Codecov / codecov/patch

src/qumada/measurement/device_object.py#L36-L39

Added lines #L36 - L39 were not covered by tests

def add_terminal(self, terminal_name: str, type: str | None = None):
def add_terminal(self, terminal_name: str, type: str | None = None, terminal_data: dict|None={}):
if terminal_name not in self.terminals.keys():
self.__dict__[terminal_name.replace(" ", "_")] = self.terminals[terminal_name] = Terminal(

Check warning on line 43 in src/qumada/measurement/device_object.py

View check run for this annotation

Codecov / codecov/patch

src/qumada/measurement/device_object.py#L41-L43

Added lines #L41 - L43 were not covered by tests
terminal_name, self, type
)
else:
raise Exception(f"Terminal {terminal_name} already exists. Please remove it first!")
if self.make_terminals_global:
if terminal_name not in self.namespace.keys():

Check warning on line 49 in src/qumada/measurement/device_object.py

View check run for this annotation

Codecov / codecov/patch

src/qumada/measurement/device_object.py#L47-L49

Added lines #L47 - L49 were not covered by tests
# Adding to the global namespace
self.namespace[terminal_name] = self.terminals[terminal_name]
logger.warning(f"Added {terminal_name} to global namespace!")

Check warning on line 52 in src/qumada/measurement/device_object.py

View check run for this annotation

Codecov / codecov/patch

src/qumada/measurement/device_object.py#L51-L52

Added lines #L51 - L52 were not covered by tests
else:
raise Exception(f"Terminal {terminal_name} already exists in global namespace. \

Check warning on line 54 in src/qumada/measurement/device_object.py

View check run for this annotation

Codecov / codecov/patch

src/qumada/measurement/device_object.py#L54

Added line #L54 was not covered by tests
Please remove it first!")

def remove_terminal(self, terminal_name: str):
if terminal_name in self.terminals.keys():
del self.__dict__[terminal_name]
del self.terminals[terminal_name]
if terminal_name in self.namespace:
del self.namespace[terminal_name]

Check warning on line 62 in src/qumada/measurement/device_object.py

View check run for this annotation

Codecov / codecov/patch

src/qumada/measurement/device_object.py#L57-L62

Added lines #L57 - L62 were not covered by tests
else:
logger.warning(f"{terminal_name} does not exist and could not be deleted")

Check warning on line 64 in src/qumada/measurement/device_object.py

View check run for this annotation

Codecov / codecov/patch

src/qumada/measurement/device_object.py#L64

Added line #L64 was not covered by tests

Expand All @@ -71,18 +85,18 @@ def set_defaults(self):
param.set_default()

Check warning on line 85 in src/qumada/measurement/device_object.py

View check run for this annotation

Codecov / codecov/patch

src/qumada/measurement/device_object.py#L83-L85

Added lines #L83 - L85 were not covered by tests

@staticmethod
def create_from_dict(data: dict):
device = QumadaDevice()
def create_from_dict(data: dict, make_terminals_global=True, namespace=None):
device = QumadaDevice(make_terminals_global=make_terminals_global, namespace=namespace)
for terminal_name, terminal_data in data.items():
device.add_terminal(terminal_name, terminal_data)
device.add_terminal(terminal_name, terminal_data=terminal_data)
for parameter_name, properties in terminal_data.items():
device.terminals[terminal_name].add_terminal_parameter(parameter_name, properties=properties)
return device

Check warning on line 94 in src/qumada/measurement/device_object.py

View check run for this annotation

Codecov / codecov/patch

src/qumada/measurement/device_object.py#L87-L94

Added lines #L87 - L94 were not covered by tests

def load_from_dict(self, data: dict):
device = self
for terminal_name, terminal_data in data.items():
device.add_terminal(terminal_name, terminal_data)
device.add_terminal(terminal_name, terminal_data=terminal_data)
for parameter_name, properties in terminal_data.items():
device.terminals[terminal_name].add_terminal_parameter(parameter_name, properties=properties)
return device

Check warning on line 102 in src/qumada/measurement/device_object.py

View check run for this annotation

Codecov / codecov/patch

src/qumada/measurement/device_object.py#L96-L102

Added lines #L96 - L102 were not covered by tests
Expand Down Expand Up @@ -212,13 +226,15 @@ def __init__(self, name: str, Terminal: Terminal, properties: dict = {}) -> None
self._parent = Terminal
self.properties: Dict[Any, Any] = properties
self.type = self.properties.get("type", None)
self._stored_value = self.properties.get("value", None)
self._stored_value = self.properties.get("value", None) # For storing values for measurements
self.setpoints = self.properties.get("setpoints", None)
self.delay = self.properties.get("delay", 0)
self._value = None
self.name = name
self.limits = None
self.rampable = False
self.ramp_rate = self.properties.get("ramp_rate", 0.1)
self.group = self.properties.get("group", None)
self.default_value = None
self.scaling = 1
self._instrument_parameter = None
Expand Down

0 comments on commit 3708551

Please sign in to comment.