From bc12d86ce19ba84aa37d8d2a69ca0f8232049a05 Mon Sep 17 00:00:00 2001 From: "W. Trevor King" Date: Sun, 18 Mar 2012 12:12:46 -0400 Subject: [PATCH] Make .load_from_config explicit and optional (don't call it from __init__). --- pyafm/afm.py | 17 ++++++++++------- pyafm/digital_port.py | 6 +++--- pyafm/stepper.py | 9 +++++---- pyafm/storage.py | 4 ++-- pyafm/temperature.py | 2 +- 5 files changed, 21 insertions(+), 17 deletions(-) diff --git a/pyafm/afm.py b/pyafm/afm.py index dd6d239..61a036f 100644 --- a/pyafm/afm.py +++ b/pyafm/afm.py @@ -81,7 +81,8 @@ class AFM (object): >>> config['temperature'] = pyafm.config.TemperatureConfig() >>> config['temperature']['name'] = 'test temperature' - >>> afm = AFM(config=config, devices=devices) + >>> afm = AFM(config=config) + >>> afm.load_from_config(devices=devices) >>> afm.setup_config() >>> afm.get_temperature() # doctest: +SKIP @@ -260,7 +261,8 @@ class AFM (object): test temperature Celsius - >>> afm2 = pyafm.storage.load_afm(filename=filename, devices=devices) + >>> afm2 = pyafm.storage.load_afm(filename=filename) + >>> afm2.load_from_config(devices=devices) >>> afm2.get_temperature() # doctest: +SKIP 297.37 @@ -274,22 +276,23 @@ class AFM (object): >>> os.remove(filename) """ - def __init__(self, config, piezo=None, stepper=None, temperature=None, - devices=None): + def __init__(self, config, piezo=None, stepper=None, temperature=None): self.config = config self.piezo = piezo self.stepper = stepper self.temperature = temperature - self.load_from_config(devices=devices) def load_from_config(self, devices): c = self.config # reduce verbosity if self.piezo is None and c['piezo']: - self.piezo = _AFMPiezo(config=c['piezo'], devices=devices) + self.piezo = _AFMPiezo(config=c['piezo']) + self.piezo.load_from_config(devices=devices) if self.stepper is None and c['stepper']: - self.stepper = _Stepper(config=c['stepper'], devices=devices) + self.stepper = _Stepper(config=c['stepper']) + self.stepper.load_from_config(devices=devices) if self.temperature is None and c['temperature']: self.temperature = _Temperature(config=c['temperature']) + self.temperature.load_from_config() def setup_config(self): if self.piezo: diff --git a/pyafm/digital_port.py b/pyafm/digital_port.py index 4c8a401..508b4e8 100644 --- a/pyafm/digital_port.py +++ b/pyafm/digital_port.py @@ -15,7 +15,8 @@ class DigitalPort (object): >>> config['channels'] = [1, 2, 3, 4] >>> config['name'] = 'test port' - >>> port = DigitalPort(config=config, devices=devices) + >>> port = DigitalPort(config=config) + >>> port.load_from_config(devices=devices) >>> port.write_bitfield(13) >>> port.write([1, 0, 1, 0]) >>> port.write_bitfield(0) @@ -23,10 +24,9 @@ class DigitalPort (object): >>> for device in devices: ... device.close() """ - def __init__(self, config, devices=None): + def __init__(self, config): self.config = config self.subdevice = None - self.load_from_config(devices=devices) def load_from_config(self, devices): c = self.config # reduce verbosity diff --git a/pyafm/stepper.py b/pyafm/stepper.py index d82acce..3c04556 100644 --- a/pyafm/stepper.py +++ b/pyafm/stepper.py @@ -23,7 +23,8 @@ class Stepper(_stepper.Stepper): >>> config['port']['name'] = 'stepper port' >>> config['name'] = 'test stepper' - >>> s = Stepper(config=config, devices=devices) + >>> s = Stepper(config=config) + >>> s.load_from_config(devices=devices) >>> s.position 0 >>> s.single_step(1) @@ -33,10 +34,9 @@ class Stepper(_stepper.Stepper): >>> for device in devices: ... device.close() """ - def __init__(self, config, devices=None): + def __init__(self, config): self.config = config self.port = None - self.load_from_config(devices=devices) c = self.config # reduce verbosity super(Stepper, self).__init__( write=self.port.write_bitfield, full_step=c['full-step'], @@ -46,7 +46,8 @@ class Stepper(_stepper.Stepper): def load_from_config(self, devices): c = self.config # reduce verbosity if self.port is None: - self.port = _DigitalPort(config=c['port'], devices=devices) + self.port = _DigitalPort(config=c['port']) + self.port.load_from_config(devices=devices) self._write = self.port.write_bitfield self.full_step = c['full-step'] self.logic = c['logic'] diff --git a/pyafm/storage.py b/pyafm/storage.py index b764730..2bce2bb 100644 --- a/pyafm/storage.py +++ b/pyafm/storage.py @@ -24,7 +24,7 @@ def save_afm(afm, filename=None, group=None): storage = _HDF5_Storage(filename=filename, group=group) storage.save(config=afm.config) -def load_afm(filename=None, group=None, devices=None): +def load_afm(filename=None, group=None): if filename is None: filename = DEFAULT_FILENAME if group is None: @@ -35,4 +35,4 @@ def load_afm(filename=None, group=None, devices=None): config.load() _LOG.debug( 'constructing AFM from configuration:\n{}'.format(config.dump())) - return AFM(config=config, devices=devices) + return = AFM(config=config) diff --git a/pyafm/temperature.py b/pyafm/temperature.py index 5798a4e..a50b448 100644 --- a/pyafm/temperature.py +++ b/pyafm/temperature.py @@ -30,6 +30,7 @@ class Temperature (object): >>> config = TemperatureConfig() >>> t = Temperature(config=config) + >>> t.load_from_config() >>> t.get_temperature() # doctest: +SKIP 297.37 >>> t.cleanup() @@ -38,7 +39,6 @@ class Temperature (object): _LOG.debug('setup temperature monitor') self.config = config self.backend = backend - self.load_from_config() def load_from_config(self): c = self.config # reduce verbosity -- 2.26.2