Skip to content

Commit

Permalink
Merge pull request #47 from willrogers/tweaks
Browse files Browse the repository at this point in the history
Tweaks
  • Loading branch information
willrogers committed Nov 16, 2017
2 parents 7ab5088 + 095cf11 commit be811b5
Show file tree
Hide file tree
Showing 9 changed files with 97 additions and 79 deletions.
3 changes: 3 additions & 0 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@
'sphinx.ext.viewcode',
]

# Include both class and __init__() docstrings.
autoclass_content = 'both'

# Add any paths that contain templates here, relative to this directory.
templates_path = ['_templates']

Expand Down
45 changes: 21 additions & 24 deletions pytac/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,20 @@ class Device(object):
"""

def __init__(self, name, cs, enabled=True, rb_suffix=None, sp_suffix=None):
"""Device constructor.
"""
Args:
name: prefix of EPICS PVs for this device
cs (ControlSystem): Control system object used to get and set
the value of a pv.
enabled (bool-like): Whether the device is enabled. May be
a PvEnabler object.
rb_suffix (string): suffix of EPICS readback pv
sp_suffix (string): suffix of EPICS setpoint pv
rb_suffix (str): suffix of EPICS readback pv
sp_suffix (str): suffix of EPICS setpoint pv
"""
self.name = name
self._cs = cs
if rb_suffix is not None:
self.rb_pv = name + rb_suffix
if sp_suffix is not None:
self.sp_pv = name + sp_suffix
self.rb_pv = name + rb_suffix if rb_suffix is not None else None
self.sp_pv = name + sp_suffix if sp_suffix is not None else None
self._enabled = True

def is_enabled(self):
Expand All @@ -50,28 +47,27 @@ def put_value(self, value):
"""Set the device value.
Args:
value(Number): The value to set on the pv.
value (float): The value to set on the pv.
Raises:
PvException: An exception occured when no setpoint pv exists.
"""
try:
self._cs.put(self.sp_pv, value)
except AttributeError:
if self.sp_pv is None:
raise PvException("""This device {0} has no setpoint pv."""
.format(self.name))
self._cs.put(self.sp_pv, value)

def get_value(self, handle):
"""Read the value of a readback or setpoint pv.
If neither readback or setpoint pvs exist then a PvException is raised.
Args:
handle(string): Handle used to get the value off a readback or setpoint
handle (str): Handle used to get the value off a readback or setpoint
pv.
Returns:
Number: The value of the pv.
float: The value of the pv.
Raises:
PvException: In case the requested pv doesn't exist.
Expand All @@ -88,18 +84,19 @@ def get_pv_name(self, handle):
"""Get a pv name on a specified handle.
Args:
handle(string): The readback or setpoint handle to be returned.
handle (str): The readback or setpoint handle to be returned.
Returns:
string: A readback or setpoint pv.
str: A readback or setpoint pv.
"""
if handle == '*':
return [self.rb_pv, self.sp_pv]
elif handle == pytac.RB:
if handle == pytac.RB and self.rb_pv:
return self.rb_pv
elif handle == pytac.SP:
elif handle == pytac.SP and self.sp_pv:
return self.sp_pv

raise PvException("""This device {0} has no {1} pv."""
.format(self.name, handle))

def get_cs(self):
return self._cs

Expand All @@ -112,8 +109,8 @@ def __init__(self, pv, enabled_value, cs):
and False otherwise.
Args:
pv(string): pv name
enabled_value(string): value for pv for which the device should
pv (str): pv name
enabled_value (str): value for pv for which the device should
be considered enabled
cs: Control system object
"""
Expand All @@ -127,7 +124,7 @@ def __nonzero__(self):
Support for Python 2.7.
Returns:
boolean: True if the device should be considered enabled
bool: True if the device should be considered enabled
"""
pv_value = self._cs.get(self._pv)
return self._enabled_value == str(int(float(pv_value)))
Expand All @@ -138,6 +135,6 @@ def __bool__(self):
Support for Python 3.x.
Returns:
boolean: True if the device should be considered enabled
bool: True if the device should be considered enabled
"""
return self.__nonzero__()
51 changes: 29 additions & 22 deletions pytac/element.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,25 @@ class Element(object):
name (str): name identifying the element
type_ (str): type of the element
length (number): length of the element in metres
s (float): the element's start position within the lattice in metres
cell (int): the element's cell within the lattice
families (set): the families this element is a member of
"""
def __init__(self, name, length, element_type, cell=None):
"""An element of the ring.
def __init__(self, name, length, element_type, s=None, cell=None):
"""
Args:
name (int): Unique identifier for the element in the ring.
length (float): The length of the element.
element_type (string): Type of the element.
element_type (str): Type of the element.
s (float): Position of the start of the element in the ring
cell (int): lattice cell this element is wihin
"""
self.name = name
self.type_ = element_type
self.length = length
self.s = s
self.cell = cell
self.families = set()
self._uc = dict()
Expand All @@ -42,9 +45,14 @@ def __str__(self):
Return a representation of an element, as a string.
Returns:
string: A representation of an element.
str: A representation of an element.
"""
return 'Element: {0}, length: {1}, families: {2}'.format(self.name, self.length, self.families)
repn = '<Element {0}, length {1} m, families {2}>'
return repn.format(self.name,
self.length,
', '.join(f for f in self.families))

__repr__ = __str__

def set_model(self, model):
self._model = model
Expand All @@ -61,10 +69,10 @@ def add_device(self, field, device, uc):
"""Add device and unit conversion objects to a given field.
Args:
field (string): The key to store the unit conversion and device
field (str): The key to store the unit conversion and device
objects.
device (Device): Represents a device stored on an element.
uc (PolyUnitConv/PchipUnitConv): Represents a unit conversion object stored for a
uc (UnitConv): Represents a unit conversion object used for this
device.
"""
self._devices[field] = device
Expand All @@ -74,7 +82,7 @@ def get_device(self, field):
"""Get the device for the given field.
Args:
field (string): The lookup key to find the device on an element.
field (str): The lookup key to find the device on an element.
Returns:
Device: The device on the given field.
Expand All @@ -88,7 +96,7 @@ def add_to_family(self, family):
"""Add the element to the specified family.
Args:
family (string): Represents the name of the family
family (str): Represents the name of the family
"""
self.families.add(family)

Expand All @@ -101,11 +109,11 @@ def get_value(self, field, handle, unit=pytac.ENG, model=pytac.LIVE):
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
field (str): Choose which device to use.
handle (str): Can take as value either 'setpoint' or 'readback'.
unit (str): Specify either engineering or physics units to be
returned.
model (string): Set whether real or simulated values to be returned.
model (str): Set whether real or simulated values to be returned.
Returns:
Number: A number that corresponds to the value of the identified
Expand Down Expand Up @@ -136,10 +144,10 @@ def set_value(self, field, value, unit=pytac.ENG, model=pytac.LIVE):
can be engineering or physics.
Args:
field (string): The key used to identify a device.
field (str): The key used to identify a device.
value (float): The value set on the device.
unit (string): Can be engineering or physics units.
model (string): The type of model: simulation or live
unit (str): Can be engineering or physics units.
model (str): The type of model: simulation or live
Raises:
PvException: An exception occured accessing a field with
Expand All @@ -158,18 +166,17 @@ def set_value(self, field, value, unit=pytac.ENG, model=pytac.LIVE):
value = self._uc[field].eng_to_phys(value)
self._model.set_value(field, value)

def get_pv_name(self, field, handle='*'):
def get_pv_name(self, field, handle):
""" Get a pv name on a device.
Can return the readback and setpoint pvs if no handle is specified.
Args:
field (string): Uniquely identifies a device.
handle(string): Can be 'readback' or 'setpoint' to return each pv.
If neither is specified then both pvs are returned.
field (str): Uniquely identifies a device.
handle (str): pytac.RB or pytac.SP
Returns:
string: A readback or setpoint pv associated with the identified device.
str: A readback or setpoint pv associated with the identified device.
Raises:
PvException: An exception occured accessing a field with
Expand Down
20 changes: 11 additions & 9 deletions pytac/load_csv.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ def load_unitconv(directory, mode, lattice):
"""Load the unit conversion objects from a file.
Args:
directory(string): The directory where the data is stored.
mode(string): The name of the mode that is used.
directory (str): The directory where the data is stored.
mode (str): The name of the mode that is used.
lattice(Lattice): The lattice object that will be used.
"""
data = collections.defaultdict(list)
Expand Down Expand Up @@ -65,10 +65,10 @@ def load(mode, control_system=None, directory=None):
"""Load the elements of a lattice from a directory.
Args:
mode(string): The name of the mode to be loaded.
control_system(ControlSystem): The control system to be used. If none is provided
mode (str): The name of the mode to be loaded.
control_system (ControlSystem): The control system to be used. If none is provided
an EpicsControlSystem will be created.
directory(string): Directory where to load the files from. If no directory is given
directory (str): Directory where to load the files from. If no directory is given
the data directory at the root of the repository is used.
Returns:
Expand All @@ -89,23 +89,25 @@ def load(mode, control_system=None, directory=None):
directory = os.path.join(os.path.dirname(os.path.abspath(__file__)),
'data')
lat = lattice.Lattice(mode, control_system, 3000)
s = 0
with open(os.path.join(directory, mode, 'elements.csv')) as elements:
csv_reader = csv.DictReader(elements)
for item in csv_reader:
length = float(item['length'])
cell = int(item['cell']) if item['cell'] else None
e = element.Element(item['name'], float(item['length']),
item['type'], cell)
e = element.Element(item['name'], length, item['type'], s, cell)
e.add_to_family(item['type'])
lat.add_element(e)
s += length

with open(os.path.join(directory, mode, 'devices.csv')) as devices:
csv_reader = csv.DictReader(devices)
for item in csv_reader:
name = item['name']
enable_pv = item['enable_pv']
enable_value = item['enable_value']
get_pv = item['get_pv']
set_pv = item['set_pv']
get_pv = item['get_pv'] if item['get_pv'] else None
set_pv = item['set_pv'] if item['set_pv'] else None
pve = True
if enable_pv and enable_value:
pve = device.PvEnabler(enable_pv, enable_value, control_system)
Expand Down
Loading

0 comments on commit be811b5

Please sign in to comment.