Skip to content

Commit

Permalink
Merge pull request #78 from T-Nicholls/errorChanges
Browse files Browse the repository at this point in the history
Add field exception, raised when the entered field does not exist on …
  • Loading branch information
willrogers committed Aug 22, 2018
2 parents 106930c + 934e26f commit 0234ec8
Show file tree
Hide file tree
Showing 11 changed files with 93 additions and 45 deletions.
19 changes: 7 additions & 12 deletions pytac/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,7 @@
magnets and a skew quadrupole.
"""
import pytac


class DeviceException(Exception):
"""Exception associated with Device misconfiguration or invalid requests.
"""
pass
from pytac.exceptions import HandleException


class Device(object):
Expand Down Expand Up @@ -70,10 +65,10 @@ def set_value(self, value):
value (float): The value to set on the PV.
Raises:
DeviceException: if no setpoint PV exists.
HandleException: if no setpoint PV exists.
"""
if self.sp_pv is None:
raise DeviceException(
raise HandleException(
"Device {0} has no setpoint PV.".format(self.name)
)
self._cs.put(self.sp_pv, value)
Expand All @@ -89,15 +84,15 @@ def get_value(self, handle):
float: The value of the PV.
Raises:
DeviceException: if the requested PV doesn't exist.
HandleException: if the requested PV doesn't exist.
"""
print('getting {}'.format('handle'))
if handle == pytac.RB and self.rb_pv:
return self._cs.get(self.rb_pv)
elif handle == pytac.SP and self.sp_pv:
return self._cs.get(self.sp_pv)

raise DeviceException(
raise HandleException(
"Device {0} has no {1} PV.".format(self.name, handle)
)

Expand All @@ -111,14 +106,14 @@ def get_pv_name(self, handle):
str: A readback or setpoint PV.
Raises:
DeviceException: if the PV doesn't exist.
HandleException: if the PV doesn't exist.
"""
if handle == pytac.RB and self.rb_pv:
return self.rb_pv
elif handle == pytac.SP and self.sp_pv:
return self.sp_pv

raise DeviceException(
raise HandleException(
"Device {0} has no {1} PV.".format(self.name, handle)
)

Expand Down
20 changes: 16 additions & 4 deletions pytac/element.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""Module containing the element class."""
import pytac
from pytac.device import DeviceException
from pytac.exceptions import FieldException, HandleException, DeviceException


class Element(object):
Expand Down Expand Up @@ -161,6 +161,7 @@ def get_value(self, field, handle=pytac.RB, units=pytac.ENG,
Raises:
DeviceException: if there is no device on the given field.
FieldException: if the element does not have the specified field.
"""
try:
model = self._models[model]
Expand All @@ -170,6 +171,8 @@ def get_value(self, field, handle=pytac.RB, units=pytac.ENG,
except KeyError:
raise DeviceException('No model type {} on element {}'.format(model,
self))
except FieldException:
raise FieldException('No field {} on element {}'.format(field, self))

def set_value(self, field, value, handle=pytac.SP, units=pytac.ENG,
model=pytac.LIVE):
Expand All @@ -187,16 +190,22 @@ def set_value(self, field, value, handle=pytac.SP, units=pytac.ENG,
Raises:
DeviceException: if arguments are incorrect.
FieldException: if the element does not have the specified field.
"""
if handle != pytac.SP:
raise DeviceException('Must write using {}'.format(pytac.SP))
raise HandleException('Must write using {}'.format(pytac.SP))
try:
model = self._models[model]
value = self._uc[field].convert(value, origin=units, target=model.units)
model.set_value(field, value)
except KeyError:
raise DeviceException('No model type {} on element {}'.format(model,
self))
try:
value = self._uc[field].convert(value, origin=units, target=model.units)
model.set_value(field, value)
except KeyError:
raise FieldException('No field {} on element {}'.format(model, self))
except FieldException:
raise FieldException('No field {} on element {}'.format(field, self))

def get_pv_name(self, field, handle):
"""Get a PV name on a device.
Expand All @@ -210,9 +219,12 @@ def get_pv_name(self, field, handle):
Raises:
DeviceException: if there is no device for this field.
FieldException: if the element does not have the specified field.
"""
try:
return self._models[pytac.LIVE].get_pv_name(field, handle)
except KeyError:
raise DeviceException('{} has no device for field {}'.format(self,
field))
except FieldException:
raise FieldException('No field {} on element {}'.format(field, self))
29 changes: 29 additions & 0 deletions pytac/exceptions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
"""Module containing all the exceptions used in pytac."""


class FieldException(Exception):
"""Exception associated with invalid field requests.
"""
pass


class HandleException(Exception):
"""Exception associated with requests with invalid handles.
"""
pass


class DeviceException(Exception):
"""Exception associated with Device misconfiguration or invalid requests.
"""
pass


class UnitsException(Exception):
"""Conversion not understood
"""
pass


class LatticeException(Exception):
pass
5 changes: 1 addition & 4 deletions pytac/lattice.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@
machine.
"""
import numpy


class LatticeException(Exception):
pass
from pytac.exceptions import LatticeException


class Lattice(object):
Expand Down
17 changes: 15 additions & 2 deletions pytac/model.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Module containing pytac model classes."""
import pytac
from pytac.exceptions import FieldException


class Model(object):
Expand Down Expand Up @@ -116,8 +117,14 @@ def get_value(self, field, handle):
Returns:
float: The value of the PV.
Raises:
FieldException: if the device does not have the specified field.
"""
return self._devices[field].get_value(handle)
try:
return self._devices[field].get_value(handle)
except KeyError:
raise FieldException('No field {} on device {}'.format(field, self))

def set_value(self, field, value):
"""Set the value of a readback or setpoint PV for a field from the
Expand All @@ -126,5 +133,11 @@ def set_value(self, field, value):
Args:
field (str): field for the requested value.
value (float): The value to set on the PV.
Raises:
FieldException: if the device does not have the specified field.
"""
self._devices[field].set_value(value)
try:
self._devices[field].set_value(value)
except KeyError:
raise FieldException('No field {} on device {}'.format(field, self))
7 changes: 1 addition & 6 deletions pytac/units.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
"""Classes for use in unit conversion."""
import pytac
import numpy
from pytac.exceptions import UnitsException
from scipy.interpolate import PchipInterpolator


class UnitsException(Exception):
"""Conversion not understood
"""
pass


def unit_function(value):
"""Default value for the pre and post functions used in unit conversion.
Expand Down
4 changes: 2 additions & 2 deletions test/test_device.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ def test_set_device_value():

def test_device_invalid_sp_raise_exception():
device2 = create_device(PREFIX, RB_PV, None)
with pytest.raises(pytac.device.DeviceException):
with pytest.raises(pytac.exceptions.HandleException):
device2.set_value(40)


def test_get_device_value():
device = create_device()
with pytest.raises(pytac.device.DeviceException):
with pytest.raises(pytac.exceptions.HandleException):
device.get_value('non_existent')


Expand Down
26 changes: 16 additions & 10 deletions test/test_element.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,16 +106,22 @@ def test_set_value_phys(test_element):
(DUMMY_VALUE_2 / 2))


def test_set_value_incorrect_field(test_element):
with pytest.raises(pytac.device.DeviceException):
test_element.set_value('non_existent', 40.0)


def test_get_pv_exceptions(test_element):
with pytest.raises(pytac.device.DeviceException):
test_element.get_value('setpoint', 'unknown_field')
with pytest.raises(pytac.device.DeviceException):
test_element.get_value('unknown_handle', 'y')
def test_set_exceptions(test_element):
with pytest.raises(pytac.exceptions.FieldException):
test_element.set_value('unknown_field', 40.0, 'setpoint')
with pytest.raises(pytac.exceptions.HandleException):
test_element.set_value('y', 40.0, 'unknown_handle')
with pytest.raises(pytac.exceptions.DeviceException):
test_element.set_value('y', 40.0, 'setpoint', model='unknown_model')


def test_get_exceptions(test_element):
with pytest.raises(pytac.exceptions.FieldException):
test_element.get_value('unknown_field', 'setpoint')
with pytest.raises(pytac.exceptions.HandleException):
test_element.get_value('y', 'unknown_handle')
with pytest.raises(pytac.exceptions.DeviceException):
test_element.get_value('y', 'setpoint', model='unknown_model')


def test_identity_conversion():
Expand Down
4 changes: 2 additions & 2 deletions test/test_lattice.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ def test_set_values(simple_element_and_lattice):

def test_set_values_raise_exception_if_number_of_values_does_not_match(simple_element_and_lattice):
element, lattice = simple_element_and_lattice
with pytest.raises(pytac.lattice.LatticeException):
with pytest.raises(pytac.exceptions.LatticeException):
lattice.set_values('family', 'x', [1, 2])


Expand All @@ -149,7 +149,7 @@ def test_s_position(simple_element_and_lattice):
def test_get_s_throws_exception_if_element_not_in_lattice():
lat = pytac.lattice.Lattice(LATTICE, mock.MagicMock(), 1)
element = pytac.element.Element(1, 1.0, 'Quad')
with pytest.raises(pytac.lattice.LatticeException):
with pytest.raises(pytac.exceptions.LatticeException):
lat.get_s(element)


Expand Down
4 changes: 2 additions & 2 deletions test/test_machine.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def test_load_bpms(ring_mode, n_bpms):
for bpm in bpms:
assert set(bpm.get_fields()) == bpm_fields
assert re.match('SR.*BPM.*X', bpm.get_pv_name('x', pytac.RB))
with pytest.raises(pytac.device.DeviceException):
with pytest.raises(pytac.exceptions.HandleException):
bpm.get_pv_name('x', pytac.SP)
assert len(bpms) == n_bpms
assert bpms[0].cell == 1
Expand Down Expand Up @@ -184,7 +184,7 @@ def test_quad_unitconv():

def test_quad_unitconv_raise_exception():
uc = pytac.units.PchipUnitConv([50.0, 100.0, 180.0], [-4.95, -9.85, -17.56])
with pytest.raises(pytac.units.UnitsException):
with pytest.raises(pytac.exceptions.UnitsException):
uc.phys_to_eng(-0.7)


Expand Down
3 changes: 2 additions & 1 deletion test/test_units.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import pytest
import pytac
from pytac.units import UnitConv, PolyUnitConv, PchipUnitConv, UnitsException
from pytac.units import UnitConv, PolyUnitConv, PchipUnitConv
from pytac.exceptions import UnitsException
import numpy


Expand Down

0 comments on commit 0234ec8

Please sign in to comment.