Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 1 addition & 5 deletions pytac/data_source.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""Module containing pytac data source classes."""
import pytac
from pytac.exceptions import DataSourceException, FieldException, HandleException
from pytac.exceptions import DataSourceException, FieldException


class DataSource(object):
Expand Down Expand Up @@ -233,7 +233,6 @@ def set_value(
self,
field: str,
value: float,
handle: str = pytac.SP,
units: str = pytac.DEFAULT,
data_source_type: str = pytac.DEFAULT,
throw: bool = True,
Expand All @@ -246,7 +245,6 @@ def set_value(
Args:
field: The requested field.
value: The value to set.
handle: pytac.SP or pytac.RB.
units: pytac.ENG or pytac.PHYS.
data_source_type: pytac.LIVE or pytac.SIM.
throw: On failure: if True, raise ControlSystemException: if
Expand All @@ -261,8 +259,6 @@ def set_value(
units = self.default_units
if data_source_type == pytac.DEFAULT:
data_source_type = self.default_data_source
if handle != pytac.SP:
raise HandleException(f"Must write using {pytac.SP}.")
data_source = self.get_data_source(data_source_type)
value = self.get_unitconv(field).convert(
value, origin=units, target=data_source.units
Expand Down
13 changes: 2 additions & 11 deletions pytac/element.py
Original file line number Diff line number Diff line change
Expand Up @@ -267,13 +267,7 @@ def get_value(
raise FieldException(f"{self}: {e}")

def set_value(
self,
field,
value,
handle=pytac.SP,
units=pytac.DEFAULT,
data_source=pytac.DEFAULT,
throw=True,
self, field, value, units=pytac.DEFAULT, data_source=pytac.DEFAULT, throw=True,
):
"""Set the value for a field.

Expand All @@ -282,7 +276,6 @@ def set_value(
Args:
field (str): The requested field.
value (float): The value to set.
handle (str): pytac.SP or pytac.RB.
units (str): pytac.ENG or pytac.PHYS.
data_source (str): pytac.LIVE or pytac.SIM.
throw (bool): On failure: if True, raise ControlSystemException: if
Expand All @@ -293,9 +286,7 @@ def set_value(
FieldException: if the element does not have the specified field.
"""
try:
self._data_source_manager.set_value(
field, value, handle, units, data_source, throw
)
self._data_source_manager.set_value(field, value, units, data_source, throw)
except DataSourceException as e:
raise DataSourceException(f"{self}: {e}")
except FieldException as e:
Expand Down
31 changes: 4 additions & 27 deletions pytac/lattice.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
from pytac.exceptions import (
DataSourceException,
UnitsException,
HandleException,
)


Expand Down Expand Up @@ -220,13 +219,7 @@ def get_value(
)

def set_value(
self,
field,
value,
handle=pytac.SP,
units=pytac.DEFAULT,
data_source=pytac.DEFAULT,
throw=True,
self, field, value, units=pytac.DEFAULT, data_source=pytac.DEFAULT, throw=True,
):
"""Set the value for a field.

Expand All @@ -235,7 +228,6 @@ def set_value(
Args:
field (str): The requested field.
value (float): The value to set.
handle (str): pytac.SP or pytac.RB.
units (str): pytac.ENG or pytac.PHYS.
data_source (str): pytac.LIVE or pytac.SIM.
throw (bool): On failure: if True, raise ControlSystemException: if
Expand All @@ -245,9 +237,7 @@ def set_value(
DataSourceException: if arguments are incorrect.
FieldException: if the lattice does not have the specified field.
"""
self._data_source_manager.set_value(
field, value, handle, units, data_source, throw
)
self._data_source_manager.set_value(field, value, units, data_source, throw)

def get_length(self):
"""Returns the length of the lattice, in meters.
Expand Down Expand Up @@ -410,7 +400,6 @@ def set_element_values(
family,
field,
values,
handle=pytac.SP,
units=pytac.DEFAULT,
data_source=pytac.DEFAULT,
throw=True,
Expand All @@ -422,7 +411,6 @@ def set_element_values(
family (str): family of elements on which to set values.
field (str): field to set values for.
values (sequence): A list of values to assign.
handle (str): pytac.SP or pytac.RB.
units (str): pytac.ENG or pytac.PHYS.
data_source (str): pytac.LIVE or pytac.SIM.
throw (bool): On failure, if True raise ControlSystemException, if
Expand All @@ -434,8 +422,6 @@ def set_element_values(
IndexError: if the given list of values doesn't match the number of
elements in the family.
"""
if handle != pytac.SP:
raise HandleException(f"Must write using {pytac.SP}.")
elements = self.get_elements(family)
if len(elements) != len(values):
raise IndexError(
Expand All @@ -444,12 +430,7 @@ def set_element_values(
)
for element, value in zip(elements, values):
status = element.set_value(
field,
value,
handle=pytac.SP,
units=units,
data_source=data_source,
throw=throw,
field, value, units=units, data_source=data_source, throw=throw,
)
if status is not None:
return status
Expand Down Expand Up @@ -667,7 +648,6 @@ def set_element_values(
family,
field,
values,
handle=pytac.SP,
units=pytac.DEFAULT,
data_source=pytac.DEFAULT,
throw=True,
Expand All @@ -679,7 +659,6 @@ def set_element_values(
family (str): family of elements on which to set values.
field (str): field to set values for.
values (sequence): A list of values to assign.
handle (str): pytac.SP or pytac.RB.
units (str): pytac.ENG or pytac.PHYS.
data_source (str): pytac.LIVE or pytac.SIM.
throw (bool): On failure: if True, raise ControlSystemException: if
Expand All @@ -693,8 +672,6 @@ def set_element_values(
data_source = self.get_default_data_source()
if units == pytac.DEFAULT:
units = self.get_default_units()
if handle != pytac.SP:
raise HandleException(f"Must write using {pytac.SP}.")
if data_source == pytac.LIVE:
if units == pytac.PHYS:
values = self.convert_family_values(
Expand All @@ -710,5 +687,5 @@ def set_element_values(
self._cs.set_multiple(pv_names, values, throw)
else:
super(EpicsLattice, self).set_element_values(
family, field, values, pytac.SP, units, data_source, throw
family, field, values, units, data_source, throw
)
4 changes: 2 additions & 2 deletions test/test_data_source.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def test_get_fields(simple_object):
],
)
def test_set_value(simple_object):
simple_object.set_value("x", DUMMY_VALUE_2, pytac.SP, pytac.ENG, pytac.LIVE)
simple_object.set_value("x", DUMMY_VALUE_2, pytac.ENG, pytac.LIVE)
simple_object.get_device("x").set_value.assert_called_with(DUMMY_VALUE_2, True)


Expand All @@ -78,5 +78,5 @@ def test_get_value_sim(simple_object):
],
)
def test_unit_conversion(simple_object, double_uc):
simple_object.set_value("y", DUMMY_VALUE_2, pytac.SP, pytac.PHYS, pytac.LIVE)
simple_object.set_value("y", DUMMY_VALUE_2, pytac.PHYS, pytac.LIVE)
simple_object.get_device("y").set_value.assert_called_with(DUMMY_VALUE_2 / 2, True)
14 changes: 5 additions & 9 deletions test/test_element.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,30 +107,26 @@ def test_get_value_uses_uc_if_necessary_for_sim_call(simple_element, double_uc):


def test_set_value_eng(simple_element):
simple_element.set_value("x", DUMMY_VALUE_2, handle=pytac.SP)
simple_element.set_value("x", DUMMY_VALUE_2)
# No conversion needed
simple_element.get_device("x").set_value.assert_called_with(DUMMY_VALUE_2, True)


def test_set_value_phys(simple_element, double_uc):
simple_element._data_source_manager._uc["x"] = double_uc
simple_element.set_value("x", DUMMY_VALUE_2, handle=pytac.SP, units=pytac.PHYS)
simple_element.set_value("x", DUMMY_VALUE_2, units=pytac.PHYS)
# Conversion fron physics to engineering units
simple_element.get_device("x").set_value.assert_called_with(DUMMY_VALUE_2 / 2, True)


def test_set_exceptions(simple_element, unit_uc):
with pytest.raises(pytac.exceptions.FieldException):
simple_element.set_value("unknown_field", 40.0, "setpoint")
with pytest.raises(pytac.exceptions.HandleException):
simple_element.set_value("y", 40.0, "unknown_handle")
simple_element.set_value("unknown_field", 40.0)
with pytest.raises(pytac.exceptions.DataSourceException):
simple_element.set_value(
"y", 40.0, "setpoint", data_source="unknown_data_source"
)
simple_element.set_value("y", 40.0, data_source="unknown_data_source")
simple_element._data_source_manager._uc["uc_but_no_data_source"] = unit_uc
with pytest.raises(pytac.exceptions.FieldException):
simple_element.set_value("uc_but_no_data_source", 40.0, "setpoint")
simple_element.set_value("uc_but_no_data_source", 40.0)


def test_get_exceptions(simple_element):
Expand Down
4 changes: 1 addition & 3 deletions test/test_epics.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,13 @@ def test_set_element_values_sim(simple_epics_lattice):
simple_epics_lattice[0].set_data_source(mock_ds, pytac.SIM)
simple_epics_lattice[0].set_unitconv("a_field", mock_uc)
simple_epics_lattice.set_element_values(
"family", "a_field", [1], pytac.SP, pytac.ENG, pytac.SIM
"family", "a_field", [1], pytac.ENG, pytac.SIM
)
mock_ds.set_value.assert_called_with("a_field", 1, True)
mock_uc.convert.assert_called_once_with(1, origin=pytac.ENG, target=pytac.PHYS)


def test_set_element_values_raises_correctly(simple_epics_lattice):
with pytest.raises(pytac.exceptions.HandleException):
simple_epics_lattice.set_element_values("family", "x", [1], pytac.RB)
with pytest.raises(IndexError):
simple_epics_lattice.set_element_values("family", "x", [1, 2])
with pytest.raises(IndexError):
Expand Down
2 changes: 0 additions & 2 deletions test/test_lattice.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,8 +177,6 @@ def test_set_element_values(simple_lattice):


def test_set_element_values_raises_Exceptions_correctly(simple_lattice):
with pytest.raises(pytac.exceptions.HandleException):
simple_lattice.set_element_values("family", "x", [1], handle=pytac.RB)
with pytest.raises(IndexError):
simple_lattice.set_element_values("family", "x", [1, 2])
with pytest.raises(IndexError):
Expand Down