Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add default arguments ability and update tests. #91

Merged
merged 16 commits into from
Sep 18, 2018
Merged
Show file tree
Hide file tree
Changes from 12 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: 4 additions & 2 deletions pytac/__init__.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
"""Pytac: Python Toolkit for Accelerator Controls."""

# PV types
# PV types.
SP = 'setpoint'
RB = 'readback'
# Unit systems
# Unit systems.
ENG = 'engineering'
PHYS = 'physics'
# Data Source types.
SIM = 'simulation'
LIVE = 'live'
# Default argument flag.
DEFAULT = 'default'

from . import data_source, element, epics, exceptions, lattice, load_csv, units, utils # noqa: E402
"""Error 402 is suppressed as we cannot import these modules at the top of the
Expand Down
2 changes: 2 additions & 0 deletions pytac/cothread_cs.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ class CothreadControlSystem(ControlSystem):

It is used to communicate over channel access with the hardware
in the ring.

**Methods:**
"""
def __init__(self):
pass
Expand Down
5 changes: 2 additions & 3 deletions pytac/cs.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@

class ControlSystem(object):
""" Abstract base class representing a control system.
"""
def __init__(self):
raise NotImplementedError()

**Methods:**
"""
def get(self, pv):
""" Get the value of the given PV.

Expand Down
29 changes: 24 additions & 5 deletions pytac/data_source.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,19 @@ class DataSourceManager(object):
_data_sources (dict): A dictionary of the data sources held.
_uc (dict): A dictionary of the unit conversion objects for each
key(field).
_default_units (str): Holds the current default unit type, pytac.PHYS
or pytac.ENG, for an element or lattice.
_default_data_source (str): Holds the current default data source,
pytac.LIVE or pytac.SIM, for an element
or lattice.

**Methods:**
"""
def __init__(self):
self._data_sources = {}
self._uc = {}
self._default_units = pytac.ENG
self._default_data_source = pytac.LIVE

def set_data_source(self, data_source, data_source_type):
"""Add a data source to the manager.
Expand Down Expand Up @@ -139,13 +146,15 @@ def get_unitconv(self, field):
"""
return self._uc[field]

def get_value(self, field, handle, units, data_source):
def get_value(self, field, handle=pytac.RB, units=pytac.DEFAULT,
data_source=pytac.DEFAULT):
"""Get the value for a field.

Returns the value of a field on the manager. This value is uniquely
identified by a field and a handle. The returned value is either
in engineering or physics units. The data_source flag returns either
real or simulated values.
real or simulated values. If handle, units or data_source are not given
then the lattice default values are used.

Args:
field (str): The requested field.
Expand All @@ -160,6 +169,10 @@ def get_value(self, field, handle, units, data_source):
DeviceException: if there is no device on the given field.
FieldException: if the manager does not have the specified field.
"""
if units is pytac.DEFAULT:
units = self._default_units
if data_source is pytac.DEFAULT:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should really use == not is in this case. I think it's unlikely to cause you a problem, but there's a chance that you end up with a string default that won't compare correctly as DEFAULT.

https://stackoverflow.com/questions/1504717/why-does-comparing-strings-in-python-using-either-or-is-sometimes-produce

Copy link
Collaborator Author

@T-Nicholls T-Nicholls Sep 17, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That stack overflow makes sense, interestingly I currently can't cause that problem in our code, but I will change it just in case.

data_source = self._default_data_source
try:
data_source = self._data_sources[data_source]
value = data_source.get_value(field, handle)
Expand All @@ -172,10 +185,12 @@ def get_value(self, field, handle, units, data_source):
raise FieldException('No field {} on manager {}'.format(field,
self))

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

This value can be set on the machine or the simulation.
This value can be set on the machine or the simulation. If handle, units
or data_source are not given then the lattice default values are used.

Args:
field (str): The requested field.
Expand All @@ -188,6 +203,10 @@ def set_value(self, field, value, handle, units, data_source):
DeviceException: if arguments are incorrect.
FieldException: if the manager does not have the specified field.
"""
if units is pytac.DEFAULT:
units = self._default_units
if data_source is pytac.DEFAULT:
data_source = self._default_data_source
if handle != pytac.SP:
raise HandleException('Must write using {}'.format(pytac.SP))
try:
Expand All @@ -207,7 +226,7 @@ def set_value(self, field, value, handle, units, data_source):
self))


class DeviceDataSource(object):
class DeviceDataSource(DataSource):
"""Data source containing control system devices.

**Attributes:**
Expand Down
2 changes: 2 additions & 0 deletions pytac/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ class Device(object):

Typically a control system will be used to set and get values on a
device.

**Methods:**
"""
def is_enabled(self):
"""Whether the device is enabled.
Expand Down
8 changes: 4 additions & 4 deletions pytac/element.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,8 @@ def add_to_family(self, family):
"""
self.families.add(family)

def get_value(self, field, handle=pytac.RB, units=pytac.ENG,
data_source=pytac.LIVE):
def get_value(self, field, handle=pytac.RB, units=pytac.DEFAULT,
data_source=pytac.DEFAULT):
"""Get the value for a field.

Returns the value of a field on the element. This value is uniquely
Expand All @@ -163,8 +163,8 @@ def get_value(self, field, handle=pytac.RB, units=pytac.ENG,
return self._data_source_manager.get_value(field, handle, units,
data_source)

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

This value can be set on the machine or the simulation.
Expand Down
73 changes: 66 additions & 7 deletions pytac/lattice.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import numpy
import pytac
from pytac.data_source import DataSourceManager
from pytac.exceptions import LatticeException
from pytac.exceptions import LatticeException, UnitsException, DeviceException


class Lattice(object):
Expand Down Expand Up @@ -105,8 +105,8 @@ def get_unitconv(self, field):
"""
return self._data_source_manager.get_unitconv(field)

def get_value(self, field, handle=pytac.RB, units=pytac.ENG,
data_source=pytac.LIVE):
def get_value(self, field, handle=pytac.RB, units=pytac.DEFAULT,
data_source=pytac.DEFAULT):
"""Get the value for a field on the lattice.

Returns the value of a field on the lattice. This value is uniquely
Expand All @@ -130,8 +130,8 @@ def get_value(self, field, handle=pytac.RB, units=pytac.ENG,
return self._data_source_manager.get_value(field, handle, units,
data_source)

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

This value can be set on the machine or the simulation.
Expand Down Expand Up @@ -349,6 +349,65 @@ def set_element_values(self, family, field, values):
if len(elements) != len(values):
raise LatticeException("Number of elements in given array must be"
" equal to the number of elements in the "
"family")
"family.")
for element, value in zip(elements, values):
element.set_value(field, value)
element.set_value(field, value, handle=pytac.SP)

def set_default_arguments(self, default_units=None,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It now looks a bit strange for this to be one method - I suggest you split it into two.

default_data_source=None):
"""Sets the default handle, units and data_source values for the lattice
and all its elements.

Args:
default_handle (str): The default handle to be set across the entire
lattice, pytac.RB or pytac.SP.
default_units (str): The default unit type to be set across the
entire lattice, pytac.ENG or pytac.PHYS.
default_data_source (str): The default data source to be set across
the entire lattice, pytac.LIVE or
pytac.SIM.

Raises:
LatticeException: if no default arguments are given.
UnitsException: if specified default unit type is not a valid unit
type.
DeviceException: if specified default data source is not a valid
data source.
"""
if not any([default_units, default_data_source]):
raise LatticeException('Please set at least one default argument '
'for units or data_source.')
if default_units is pytac.ENG or default_units is pytac.PHYS:
self._data_source_manager._default_units = default_units
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're using a private attribute _default_units from outside the DataSourceManager class. Should it be a public attribute?

elems = self.get_elements()
for elem in elems:
elem._data_source_manager._default_units = default_units
elif default_units is not None:
raise UnitsException('{0} is not a unit type. Please enter {1} or '
'{2}'.format(default_units, pytac.ENG,
pytac.PHYS))
if default_data_source is pytac.LIVE or default_data_source is pytac.SIM:
self._data_source_manager._default_data_source = default_data_source
elems = self.get_elements()
for elem in elems:
elem._data_source_manager._default_data_source = default_data_source
elif default_data_source is not None:
raise DeviceException('{0} is not a data source. Please enter {1} '
'or {2}'.format(default_data_source,
pytac.LIVE, pytac.SIM))

def get_default_units(self):
"""Get the default unit type, pytac.RB or pytac.SP.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment wrong.


Returns:
str: the default unit type for the entire lattice.
"""
return self._data_source_manager._default_units

def get_default_data_source(self):
"""Get the default data source, pytac.RB or pytac.SP.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment wrong.


Returns:
str: the default data source for the entire lattice.
"""
return self._data_source_manager._default_data_source
23 changes: 0 additions & 23 deletions test/test_cs.py

This file was deleted.

5 changes: 3 additions & 2 deletions test/test_element.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,15 @@ 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)
simple_element.set_value('x', DUMMY_VALUE_2, handle=pytac.SP)
# No conversion needed
simple_element.get_device('x').set_value.assert_called_with(DUMMY_VALUE_2)


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, units=pytac.PHYS)
simple_element.set_value('x', DUMMY_VALUE_2, handle=pytac.SP,
units=pytac.PHYS)
# Conversion fron physics to engineering units
simple_element.get_device('x').set_value.assert_called_with((DUMMY_VALUE_2 / 2))

Expand Down
30 changes: 30 additions & 0 deletions test/test_invalid_classes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import pytest
from pytac import cs, data_source, device


def test_ControlSystem_throws_NotImplementedError():
test_cs = cs.ControlSystem()
with pytest.raises(NotImplementedError):
test_cs.get('dummy')
with pytest.raises(NotImplementedError):
test_cs.put('dummy', 1)


def test_DataSource_throws_NotImplementedError():
test_ds = data_source.DataSource()
with pytest.raises(NotImplementedError):
test_ds.get_fields()
with pytest.raises(NotImplementedError):
test_ds.get_value('field', 'handle')
with pytest.raises(NotImplementedError):
test_ds.set_value('field', 0.0)


def test_Device_throws_NotImplementedError():
test_d = device.Device()
with pytest.raises(NotImplementedError):
test_d.is_enabled()
with pytest.raises(NotImplementedError):
test_d.set_value(0.0)
with pytest.raises(NotImplementedError):
test_d.get_value()
20 changes: 20 additions & 0 deletions test/test_lattice.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,3 +125,23 @@ def test_get_family_s(simple_lattice):
element4.add_to_family('family')
simple_lattice.add_element(element4)
assert simple_lattice.get_family_s('family') == [0, 0, 1.0, 2.5]


def test_get_default_arguments(simple_lattice):
assert simple_lattice.get_default_units() == pytac.ENG
assert simple_lattice.get_default_data_source() == pytac.LIVE


def test_set_default_arguments(simple_lattice):
simple_lattice.set_default_arguments(pytac.PHYS, pytac.SIM)
assert simple_lattice._data_source_manager._default_units == pytac.PHYS
assert simple_lattice._data_source_manager._default_data_source == pytac.SIM


def test_set_default_arguments_exceptions(simple_lattice):
with pytest.raises(pytac.exceptions.LatticeException):
simple_lattice.set_default_arguments()
with pytest.raises(pytac.exceptions.UnitsException):
simple_lattice.set_default_arguments(default_units='invalid_units')
with pytest.raises(pytac.exceptions.DeviceException):
simple_lattice.set_default_arguments(default_data_source='invalid_data_source')