def __init__(self, data, unit=1, error=None, mask=None, dimensions=None, longname=u"Sampled Field", shortname=u"\\Psi", attributes=None, rescale=False): DataContainer.__init__(self, longname, shortname, attributes) self.data = data self.mask = mask try: if isinstance(unit, (str, unicode)): unit = unit.replace('^', '**') if isinstance(unit, unicode): unit = unit.encode('utf-8') self.unit = Quantity(unit) except: try: self.unit = Quantity("1" + unit) except: self.unit = unit self.error = error if dimensions != None: self.dimensions = dimensions else: N = len(data.shape) - 1 self.dimensions = [ generateIndex(N - i, n) for i, n in enumerate(data.shape) ] if rescale: self.rescale() for dim in self._dimensions: dim.rescale() assert self.isValid()
class DataContainerTestCase(unittest.TestCase): def setUp(self): self.dc = DataContainer("variable", "v") self.dc0 = DataContainer("variable", "v_0") def testNoSubscriptPersistent(self): result = self.dc.appendSubscript("i") self.assertEqual(result, "v_{i}") self.assertEqual(self.dc.shortname, "v_{i}") def testSubscriptPersistent(self): result = self.dc0.appendSubscript("i") self.assertEqual(result, "v_{0,i}") self.assertEqual(self.dc0.shortname, "v_{0,i}") def testNoSubscriptNotPersistent(self): result = self.dc.appendSubscript("i", persistent=False) self.assertEqual(result, "v_{i}") self.assertEqual(self.dc.shortname, "v") def testSubscriptNotPersistent(self): result = self.dc0.appendSubscript("i", persistent=False) self.assertEqual(result, "v_{0,i}") self.assertEqual(self.dc0.shortname, "v_0") def testDefaultAttribute(self): """ Checking if the default attribute has correctly been set to an empty dictionary. """ self.assertEqual( self.dc.attributes, {}, "Expected an empty dictionary as attribute, \ but found %s." % self.dc.attributes, )
def setUp(self): self.dc = DataContainer("variable", "v") self.dc0 = DataContainer("variable", "v_0")