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
5 changes: 5 additions & 0 deletions pytac/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
# PV types
SP = 'setpoint'
RB = 'readback'
# Unit systems
ENG = 'engineering'
PHYS = 'physics'
# Model types.
SIM = 'simulation'
LIVE = 'live'
24 changes: 12 additions & 12 deletions pytac/element.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,29 +91,29 @@ def add_to_family(self, family):
"""
self.families.add(family)

def get_value(self, field, handle, unit=pytac.ENG, sim=False):
"""Get the value of a pv.
def get_value(self, field, handle, unit=pytac.ENG, model=pytac.LIVE):
"""Get the value for a field.

Returns the value of a pv on the element. This value is uniquely
Returns the value for a field on the element. This value is uniquely
identified by a field and a handle. The returned value is either
in engineering or physics units. The sim flag returns either real
in engineering or physics units. The model flag returns either real
or simulated values.

Args:
field (string): Choose which device to use.
handle (string): Can take as value either 'setpoint' or 'readback'.
unit (string): Specify either engineering or physics units to be
returned.
sim (boolean): Set whether real or simulated values to be returned.
model (string): Set whether real or simulated values to be returned.

Returns:
Number: A number that corresponds to the pv value of the identified
device.
Number: A number that corresponds to the value of the identified
field.

Raises:
PvException: When there is no associated device on the given field.
"""
if not sim:
if model == pytac.LIVE:
if field in self._devices:
value = self._devices[field].get_value(handle)
if unit == pytac.PHYS:
Expand All @@ -127,8 +127,8 @@ def get_value(self, field, handle, unit=pytac.ENG, sim=False):
value = self._uc[field].phys_to_eng(value)
return value

def set_value(self, field, value, unit=pytac.ENG, sim=False):
"""Set the pv value on a uniquely identified device.
def set_value(self, field, value, unit=pytac.ENG, model=pytac.LIVE):
"""Set the value on a uniquely identified device.

This value can be set on the machine or the simulation.
A field is required to identify a device. Returned value
Expand All @@ -138,13 +138,13 @@ def set_value(self, field, value, unit=pytac.ENG, sim=False):
field (string): The key used to identify a device.
value (float): The value set on the device.
unit (string): Can be engineering or physics units.
sim (boolean): To set whether the simulation is on or off.
model (string): The type of model: simulation or live

Raises:
PvException: An exception occured accessing a field with
no associated device.
"""
if not sim:
if model == pytac.LIVE:
if field in self._devices:
if unit == pytac.PHYS:
value = self._uc[field].phys_to_eng(value)
Expand Down
12 changes: 6 additions & 6 deletions test/test_element.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,21 +54,21 @@ def test_add_element_to_family():
assert 'fam' in e.families


def test_get_value_uses_cs_if_sim_False(test_element):
test_element.get_value('x', handle=pytac.SP, sim=False)
def test_get_value_uses_cs_if_model_live(test_element):
test_element.get_value('x', handle=pytac.SP, model=pytac.LIVE)
test_element.get_device('x')._cs.get.assert_called_with(SP_PV)
test_element.get_value('x', handle=pytac.RB, sim=False)
test_element.get_value('x', handle=pytac.RB, model=pytac.LIVE)
test_element.get_device('x')._cs.get.assert_called_with(RB_PV)


def test_get_value_uses_uc_if_necessary_for_cs_call(test_element):
test_element.get_value('x', handle=pytac.SP, unit=pytac.PHYS, sim=False)
test_element.get_value('x', handle=pytac.SP, unit=pytac.PHYS, model=pytac.LIVE)
test_element._uc['x'].eng_to_phys.assert_called_with(DUMMY_VALUE_1)
test_element.get_device('x')._cs.get.assert_called_with(SP_PV)


def test_get_value_uses_uc_if_necessary_for_sim_call(test_element):
test_element.get_value('x', handle=pytac.SP, unit=pytac.ENG, sim=True)
def test_get_value_uses_uc_if_necessary_for_model_call(test_element):
test_element.get_value('x', handle=pytac.SP, unit=pytac.ENG, model=pytac.SIM)
test_element._uc['x'].phys_to_eng.assert_called_with(DUMMY_VALUE_2)
test_element._model.get_value.assert_called_with('x')

Expand Down
9 changes: 7 additions & 2 deletions test/test_machine.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
""" This file tests the entire loading of the DLS machine from the CSV
files in the data directory. These are more like integration tests,
and allows us to check that the pytac setup is working correctly.
"""
import pytac
import pytest
import os
Expand All @@ -12,6 +16,7 @@

@pytest.fixture
def lattice():
""" Load the entire lattice from the data directory. """
basepath = os.getcwd()
filename = os.path.join(basepath, 'data/')
lattice = pytac.load_csv.load('VMX', mock.MagicMock(), filename)
Expand All @@ -34,7 +39,7 @@ def test_get_family_pvs(lattice):
def test_load_bpms(lattice):
bpms = lattice.get_elements('BPM')
for bpm in bpms:
assert set(bpm._devices.keys()) == set(('x', 'y'))
assert set(bpm.get_fields()) == set(('x', 'y'))
assert len(bpms) == 173


Expand All @@ -47,7 +52,7 @@ def test_load_quadrupoles(lattice):
quads = lattice.get_elements('QUAD')
assert len(quads) == 248
for quad in quads:
assert set(quad._devices.keys()) == set(('b1',))
assert set(quad.get_fields()) == set(('b1',))
device = quad.get_device('b1')
assert re.match('SR.*Q.*:I', device.rb_pv)
assert re.match('SR.*Q.*:SETI', device.sp_pv)
Expand Down
2 changes: 1 addition & 1 deletion utils/load_mml.m
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ function insertpvs(index, pvs)
field = 'b2';
elseif strcmp(type, 'VSTR')
field = 'a0';
else
elseif strcmp(type, 'HSTR') || strcmp(type, 'BEND')
field = 'b0';
end
% MML is inconsistent about whether the family for the bends
Expand Down