From a0d0362bd5638d2a0736010caea4869b8a5a8bb8 Mon Sep 17 00:00:00 2001 From: "W. Trevor King" Date: Sun, 18 Mar 2012 12:04:31 -0400 Subject: [PATCH] Make .load_from_config explicit and optional (don't call it from __init__). --- pypiezo/afm.py | 13 +++++-------- pypiezo/base.py | 50 +++++++++++++++++++++++++++---------------------- 2 files changed, 33 insertions(+), 30 deletions(-) diff --git a/pypiezo/afm.py b/pypiezo/afm.py index 5cf5569..a7cb9d5 100644 --- a/pypiezo/afm.py +++ b/pypiezo/afm.py @@ -42,13 +42,11 @@ class AFMPiezo (_base.Piezo): deflection measurements. >>> from pprint import pprint - >>> from pycomedi.device import Device >>> from pycomedi.constant import AREF >>> from . import config >>> from . import surface - >>> d = Device('/dev/comedi0') - >>> d.open() + >>> devices = [] >>> piezo_config = config.PiezoConfig() >>> piezo_config['name'] = 'Molly' @@ -70,10 +68,8 @@ class AFMPiezo (_base.Piezo): >>> piezo_config['axes'][0].update( ... {'gain':20, 'sensitivity':8e-9, 'minimum':-9}) - >>> a = _base.PiezoAxis(config=piezo_config['axes'][0], devices=[d]) - >>> c = _base.InputChannel(config=piezo_config['inputs'][0], devices=[d]) - - >>> p = AFMPiezo(config=piezo_config, axes=[a], inputs=[c]) + >>> p = AFMPiezo(config=piezo_config) + >>> p.load_from_config(devices=devices) >>> p.setup_config() >>> deflection = p.read_deflection() @@ -142,7 +138,8 @@ class AFMPiezo (_base.Piezo): >>> abs(e.left_slope-1) < 0.1 True - >>> d.close() + >>> for device in devices: + ... device.close() """ def _deflection_channel(self): return self.channel_by_name(name='deflection', direction='input') diff --git a/pypiezo/base.py b/pypiezo/base.py index 98b5e06..6f9eefc 100644 --- a/pypiezo/base.py +++ b/pypiezo/base.py @@ -386,7 +386,8 @@ class PiezoAxis (object): Opening from the config alone: - >>> p = PiezoAxis(config=config, devices=[d]) + >>> p = PiezoAxis(config=config) + >>> p.load_from_config(devices=[d]) >>> p.axis_channel # doctest: +ELLIPSIS >>> p.monitor_channel # doctest: +ELLIPSIS @@ -394,12 +395,10 @@ class PiezoAxis (object): >>> d.close() """ - def __init__(self, config, axis_channel=None, monitor_channel=None, - devices=None): + def __init__(self, config, axis_channel=None, monitor_channel=None): self.config = config self.axis_channel = axis_channel self.monitor_channel = monitor_channel - self.load_from_config(devices=devices) def load_from_config(self, devices): c = self.config # reduce verbosity @@ -410,11 +409,13 @@ class PiezoAxis (object): '({} and {})').format( c['channel']['device'], c['monitor']['device'])) if not self.axis_channel: - self.axis_channel = OutputChannel( - config=c['channel'], devices=devices).channel + output = OutputChannel(config=c['channel']) + output.load_from_config(devices=devices) + self.axis_channel = output.channel if c['monitor'] and not self.monitor_channel: - self.monitor_channel = InputChannel( - config=c['monitor'], devices=devices).channel + monitor = InputChannel(config=c['monitor']) + monitor.load_from_config(devices=devices) + self.monitor_channel = monitor.channel self.name = c['channel']['name'] def setup_config(self): @@ -471,16 +472,16 @@ class OutputChannel(object): Opening from the config alone: - >>> c = OutputChannel(config=channel_config, devices=[d]) + >>> c = OutputChannel(config=channel_config) + >>> c.load_from_config(devices=[d]) >>> c.channel # doctest: +ELLIPSIS >>> d.close() """ - def __init__(self, config, channel=None, devices=None): + def __init__(self, config, channel=None): self.config = config self.channel = channel - self.load_from_config(devices=devices) def load_from_config(self, devices): _load_channel_from_config( @@ -529,16 +530,16 @@ class InputChannel(object): Opening from the config alone: - >>> c = InputChannel(config=channel_config, devices=[d]) + >>> c = InputChannel(config=channel_config) + >>> c.load_from_config(devices=[d]) >>> c.channel # doctest: +ELLIPSIS >>> d.close() """ - def __init__(self, config, channel=None, devices=None): + def __init__(self, config, channel=None): self.config = config self.channel = channel - self.load_from_config(devices=devices) def load_from_config(self, devices): _load_channel_from_config( @@ -568,13 +569,15 @@ class Piezo (object): >>> axis_config['channel']['name'] = 'z' >>> axis_config['monitor'] = _config.InputChannelConfig() >>> axis_config['monitor']['analog-reference'] = AREF.diff - >>> a = PiezoAxis(config=axis_config, devices=[d]) + >>> a = PiezoAxis(config=axis_config) + >>> a.load_from_config(devices=[d]) >>> a.setup_config() >>> input_config = _config.InputChannelConfig() >>> input_config['analog-reference'] = AREF.diff >>> input_config['name'] = 'some-input' - >>> c = InputChannel(config=input_config, devices=[d]) + >>> c = InputChannel(config=input_config) + >>> c.load_from_config(devices=[d]) >>> c.setup_config() >>> config = _config.PiezoConfig() @@ -633,7 +636,8 @@ class Piezo (object): Opening from the config alone: - >>> p = Piezo(config=config, devices=[d]) + >>> p = Piezo(config=config) + >>> p.load_from_config(devices=[d]) >>> for axis in p.axes: ... print(axis.axis_channel) ... print(axis.monitor_channel) @@ -647,24 +651,26 @@ class Piezo (object): >>> d.close() """ - def __init__(self, config, axes=None, inputs=None, devices=None): + def __init__(self, config, axes=None, inputs=None): self.config=config self.axes = axes self.inputs = inputs self.last_output = {} - self.load_from_config(devices=devices) def load_from_config(self, devices): if not self.axes: self.axes = [] for config in self.config['axes']: - self.axes.append(PiezoAxis(config=config, devices=devices)) + axis = PiezoAxis(config=config) + axis.load_from_config(devices=devices) + self.axes.append(axis) self.last_output.clear() if not self.inputs: self.inputs = [] for config in self.config['inputs']: - self.inputs.append( - InputChannel(config=config, devices=devices)) + input = InputChannel(config=config) + input.load_from_config(devices=devices) + self.inputs.append(input) self.name = self.config['name'] def setup_config(self): -- 2.26.2