def test_action(self): obj = aDriver() self.assertEqual(obj.run(), 42) self.assertEqual(obj.run2(2), 42 * 2) self.assertEqual(obj.run3(3), 42 * 3) self.assertEqual(obj.run4(Q_(3, 'ms')), 3) self.assertEqual(obj.run4b(Q_(3, 'ms')), 3) self.assertEqual(obj.run4(Q_(3, 's')), 3000)
def test_instance_specific(self): x = aDriver() y = aDriver() val = Q_(3, 's') self.assertEqual(x.run4(val), y.run4(val)) x.actions.run4.param('value', units='s') self.assertNotEqual(x.run4(val), y.run4(val)) self.assertEqual(x.run4(val), 3)
def assertQuantityEqual(self, q1, q2, msg=None, delta=None): """ Make sure q1 and q2 are the same quantities to within the given precision. """ delta = 1e-5 if delta is None else delta msg = '' if msg is None else ' (%s)' % msg q1 = Q_(q1) q2 = Q_(q2) d1 = getattr(q1, '_dimensionality', None) d2 = getattr(q2, '_dimensionality', None) if (d1 or d2) and not (d1 == d2): raise self.failureException( "Dimensionalities are not equal (%s vs %s)%s" % (d1, d2, msg))
def test_units_tuple(self): hdl = MemHandler() class Spam(Driver): _logger = get_logger('test.feat', False) _logger.addHandler(hdl) _logger.setLevel(logging.DEBUG) _eggs = (8, 1) # Transform each element of the return vector # based on the set signature @Feat(units=('s', 's')) def eggs(self_): return self_._eggs @eggs.setter def eggs(self_, values): self_._eggs = values class Spam2(Driver): _logger = get_logger('test.feat', False) _logger.addHandler(hdl) _logger.setLevel(logging.DEBUG) _eggs = (8, 1) # Transform each element of the return vector # based on the set signature @Feat(units=('s', None)) def eggs(self_): return self_._eggs @eggs.setter def eggs(self_, values): self_._eggs = values obj = Spam() self.assertQuantityEqual(obj.eggs, (Q_(8, 's'), Q_(1, 's'))) self.assertEqual(setattr(obj, "eggs", (Q_(3, 'ms'), Q_(4, 'ms'))), None) self.assertQuantityEqual(obj.eggs, (Q_(3 / 1000, 's'), Q_(4 / 1000, 's'))) self.assertEqual(setattr(obj, "eggs", (3, 1)), None) obj = Spam2() self.assertQuantityEqual(obj.eggs, (Q_(8, 's'), 1)) self.assertEqual(setattr(obj, "eggs", (Q_(3, 'ms'), 4)), None) self.assertQuantityEqual(obj.eggs, (Q_(3 / 1000, 's'), 4))
def test_Self(self): class X(Driver): @Feat(units=Self.a_value_units('s')) def a_value(self): return 1 @Feat() def a_value_units(self): return self._units @a_value_units.setter def a_value_units(self, new_units): self._units = new_units x = X() self.assertEqual(x.feats.a_value.units, 's') self.assertEqual(x.a_value, Q_(1, 's')) x.a_value_units = 'ms' self.assertEqual(x.feats.a_value.units, 'ms') self.assertEqual(x.a_value, Q_(1, 'ms'))
def test_units(self): hdl = MemHandler() class Spam(Driver): _logger = get_logger('test.feat', False) _logger.addHandler(hdl) _logger.setLevel(logging.DEBUG) _eggs = 8 @Feat(units='s') def eggs(self_): return self_._eggs @eggs.setter def eggs(self_, value): self.assertIsInstance(value, float) self.assertEqual(value, float(value)) self_._eggs = value _eggs_str = '8' @Feat(units='s') def eggs_str(self_): return self_._eggs_str @eggs_str.setter def eggs_str(self_, value): self.assertIsInstance(value, float) self.assertEqual(value, float(value)) self_._eggs_str = str(value) _eggs_adim = '8' @Feat(units=float) def eggs_adim(self_): return self_._eggs_adim @eggs_adim.setter def eggs_adim(self_, value): self.assertIsInstance(value, float) self.assertEqual(value, float(value)) self_._eggs_adim = str(value) obj = Spam() self.assertQuantityEqual(obj.eggs, Q_(8, 's')) self.assertEqual(setattr(obj, "eggs", Q_(3, 'ms')), None) self.assertQuantityEqual(obj.eggs, Q_(3 / 1000, 's')) self.assertEqual(setattr(obj, "eggs", 3), None) self.assertQuantityEqual(obj.eggs_str, Q_(8, 's')) self.assertEqual(setattr(obj, "eggs_str", Q_(3, 'ms')), None) self.assertQuantityEqual(obj.eggs_str, Q_(3 / 1000, 's')) self.assertEqual(setattr(obj, "eggs_str", 3), None) self.assertQuantityEqual(obj.eggs_adim, 8) self.assertEqual(setattr(obj, "eggs_adim", 3), None) self.assertQuantityEqual(obj.eggs_adim, 3)
def test_set_units(self): class Spam(Driver): _eggs = 8 @Feat(values={1, 2.2, 10}, units='second') def eggs(self_): return self_._eggs @eggs.setter def eggs(self_, value): self_._eggs = value obj = Spam() for mult, units in ((1., 'second'), (1000., 'millisecond'), (0.001, 'kilosecond')): val = Q_(2.2 * mult, units) obj.eggs = val self.assertEqual(obj.eggs, val) self.assertRaises(ValueError, setattr, obj, "eggs", Q_(11. * mult, units)) self.assertRaises(ValueError, setattr, obj, "eggs", Q_(0.9 * mult, units))
def get_units(units): """Creates and display a UnitInputDialog and return new units. Parameters ---------- units : str current units. Returns ------- str or None output compatible units. Returns None if cancelled. """ if isinstance(units, str): units = Q_(1, units) dialog = UnitInputDialog(Q_(1, units.units)) if dialog.exec_(): return dialog.destination_units.text() return None
def test_units(self): hdl = MemHandler() class Spam(Driver): _logger = get_logger('test.feat') _logger.addHandler(hdl) _logger.setLevel(logging.DEBUG) _eggs = {'answer': 42} @DictFeat(units='s') def eggs(self_, key): return self_._eggs[key] @eggs.setter def eggs(self_, key, value): self_._eggs[key] = value obj = Spam() self.assertQuantityEqual(obj.eggs['answer'], Q_(42, 's')) obj.eggs['answer'] = Q_(46, 'ms') self.assertQuantityEqual(obj.eggs['answer'], Q_(46 / 1000, 's')) obj.eggs['answer'] = 42
def check(self): units = self.destination_units.text().strip() if not units: return try: new_units = Q_(1, units) factor = self.units.to(new_units).magnitude except LookupError or SyntaxError: self.message.setText('Cannot parse units') self.buttonBox.setEnabled(False) except ValueError: self.message.setText('Incompatible units') self.buttonBox.setEnabled(False) except AttributeError: self.message.setText('Unknown units') self.buttonBox.setEnabled(False) else: self.message.setText('factor {:f}'.format(factor)) self.buttonBox.setEnabled(True)
def _relabel(self, axis): """Builds the actual label using the label and units for a given axis. Also builds a quantity to be used to normalized the data. Parameters ---------- axis : x' or 'y' Returns ------- """ label = self._labels[axis] units = self._units[axis] if label and units: label = '%s [%s]' % (label, units) elif units: label = '[%s]' % units self.pw.setLabel(self._axis[axis], label) if units: setattr(self, '_q' + axis, Q_(1, units))
def test_Self_exceptions(self): class X(Driver): @Feat(units=Self.units) def value(self): return 1 @value.setter def value(self, value): pass @Feat() def units(self): return self._units @units.setter def units(self, value): self._units = value x = X() self.assertRaises(Exception, getattr, x, 'value') self.assertRaises(Exception, setattr, x, 'value', 1) x.units = 'ms' self.assertEqual(x.feats.value.units, 'ms') self.assertEqual(x.value, Q_(1, 'ms'))
# -*- coding: utf-8 -*- import unittest import doctest from lantz_core import Q_ import lantz_core.processors as processors mv = Q_(1, 'mV') Hz = Q_(1, 'Hz') V = Q_(1, 'V') class TestProcessors(unittest.TestCase): def test_docs(self): doctest.testmod(processors) def test_invalid_arguments(self): self.assertRaises(ValueError, processors.convert_to, V, on_incompatible='na') self.assertRaises(ValueError, processors.convert_to, V, on_dimensionless='na') self.assertRaises(ValueError, processors.convert_to, list()) def test_return_float(self): self.assertEqual(