Skip to content

Instantly share code, notes, and snippets.

@hgrecco
Forked from ColinBrosseau/code
Last active December 22, 2015 14:39
Show Gist options
  • Save hgrecco/6486889 to your computer and use it in GitHub Desktop.
Save hgrecco/6486889 to your computer and use it in GitHub Desktop.
SampleRateUnit = {'Seconds': '0', 'Minutes': '1', 'Hours': '2',
'Days': '3', '%': '4'}
SamplePeriodUnit = {'Seconds': '0', 'Minutes': '1', 'Hours': '2',
'Days': '3', 'Weeks': '4', 'Pulses': '5'}
TotalDurationUnit = {'Period': '0', 'Seconds': '1', 'Minutes': '2',
'Hours': '3', 'Days': '4', 'Weeks': '5', 'Continuous': '6',
'predefinded': '7'}
TimeStampEnabled = {False: 0, True: 1}
@Feat(values=(None, SampleRateUnit, None, SamplePeriodUnit, None,
TotalDurationUnit, TimeStampEnabled,))
def sampling(self):
"""This command is used to read the current data sampling settings.
(rds)
"""
return self.parse_query('*rds', format='Sample Rate: {:f} / {}\t'
'Sample Period: {:f} / {}\t'
'Total Duration: {:f} {}\t'
'Time Stamp: {}')
@sampling.setter
def sampling(self, value):
"""This command provides the data sampling parameters for the logging
and statistics environments. (dsu)
"""
self.query('*dsu {} {} {} {} {} {} {}'.format(*value))
@ColinBrosseau
Copy link

previous code returns:

14:47:04 INFO Getting sampling
14:47:04 DEBUG Sending b'*rds\r\n'
14:47:04 DEBUG Received 'Sample Rate: 1 / Seconds\tSample Period: 30 / Minutes\tTotal Duration: 1 Period\tTime Stamp: ON\r\n' (len=94)
14:47:04 DEBUG (raw) Got [1, 'Seconds', 30, 'Minutes', 1, 'Period', 'ON'] for sampling
14:47:04 ERROR While post-processing [1, 'Seconds', 30, 'Minutes', 1, 'Period', 'ON'] for sampling: 'Seconds' not in ('4', '2', '3', '0', '1')
14:47:04 DEBUG Closing port /dev/ttyUSB0
Traceback (most recent call last):
File "842PE.py", line 404, in
print(inst.sampling)
File "/usr/lib/python3.3/site-packages/Lantz-0.3.dev0-py3.3.egg/lantz/feat.py", line 303, in get
return self.get(instance)
File "/usr/lib/python3.3/site-packages/Lantz-0.3.dev0-py3.3.egg/lantz/feat.py", line 255, in get
raise e
File "/usr/lib/python3.3/site-packages/Lantz-0.3.dev0-py3.3.egg/lantz/feat.py", line 252, in get
value = self.post_get(value, instance, key)
File "/usr/lib/python3.3/site-packages/Lantz-0.3.dev0-py3.3.egg/lantz/feat.py", line 213, in post_get
value = processor(value)
File "/usr/lib/python3.3/site-packages/Lantz-0.3.dev0-py3.3.egg/lantz/processors.py", line 169, in call
for processor, value in zip(self.processors, values))
File "/usr/lib/python3.3/site-packages/Lantz-0.3.dev0-py3.3.egg/lantz/processors.py", line 169, in
for processor, value in zip(self.processors, values))
File "/usr/lib/python3.3/site-packages/Lantz-0.3.dev0-py3.3.egg/lantz/processors.py", line 403, in _inner
raise ValueError("{!r} not in {}".format(key, tuple(container.keys())))
ValueError: 'Seconds' not in ('4', '2', '3', '0', '1')

@ColinBrosseau
Copy link

As one can see, there is no need for conversion in the getter as the return from the device is already in the correct form (except 'ON' -> True). Maybe it's just easyer to not make a getter/setter for this one.

@hgrecco
Copy link
Author

hgrecco commented Sep 8, 2013

I see now. Sorry for the confusion. In this cases where the instrument programming code is not designed in a simmetrical way, you can still use getter and setter but just avoid the automatic conversion using values.

SampleRateUnit = {'Seconds': '0', 'Minutes': '1', 'Hours': '2',
                  'Days': '3', '%': '4'}
SamplePeriodUnit = {'Seconds': '0', 'Minutes': '1', 'Hours': '2',
                    'Days': '3', 'Weeks': '4', 'Pulses': '5'}
TotalDurationUnit = {'Period': '0', 'Seconds': '1', 'Minutes': '2',
                     'Hours': '3', 'Days': '4', 'Weeks': '5', 'Continuous': '6',
                     'predefinded': '7'}
TimeStampEnabled = {False: 0, True: 1}

@Feat(values=(None, SampleRateUnit, None, SamplePeriodUnit, None,
              TotalDurationUnit, TimeStampEnabled,))
def sampling(self):
    """This command is used to read the current data sampling settings.
    (rds)
    """
    out = self.parse_query('*rds', format='Sample Rate: {} / {}\t'
                           'Sample Period: {} / {}\t'
                           'Total Duration: {} {}\t'
                           'Time Stamp: {}')
    out[0] = int(out[0])
    out[2] = int(out[2])
    out[4] = int(out[4])
    out[6] = True if value[6] == 'ON' else False
    return out

@sampling.setter
def sampling(self, value):
    """This command provides the data sampling parameters for the logging
    and statistics environments. (dsu)
    """
    value[1] = self.SamplePeriodUnit[value[1]]
    value[3] = self.SamplePeriodUnit[value[3]]
    value[5] = self.TotalDurationUnit[value[5]]
    value[6] = 1 if value[6] else 0
    self.query('*dsu {} {} {} {} {} {} {}'.format(*value))

By the way, are you sure that the numbers are always integers?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment