def test_toCartesian(self): """MagePoint toCartesian() should transform coordinates correctly""" m = MagePoint([.1,.2,.3]) self.assertEqual(m.toCartesian().Coordinates,[.6,.7,.5]) m = MagePoint() self.assertEqual(m.toCartesian().Coordinates,[1,1,1]) m = MagePoint([.25,.25,.25],Color='red',Label='label',State='L') self.assertEqual(m.toCartesian().Coordinates,[.5,.5,.5]) self.assertEqual(m.toCartesian().Color,m.Color) self.assertEqual(m.toCartesian().Label,m.Label) self.assertEqual(m.toCartesian().State,m.State) m = MagePoint([1/3.0,1/3.0,0]) self.assertFloatEqual(m.toCartesian().Coordinates, [2/3.0,1/3.0,2/3.0]) m = MagePoint([3,4,5]) self.assertRaises(ValueError,m.toCartesian)
def test_fromCartesian(self): """MagePoint fromCartesian should transform coordinates correctly""" mp = MagePoint([2/3.0,1/3.0,2/3.0]) self.assertFloatEqual(mp.fromCartesian().Coordinates,[1/3.0,1/3.0,0]) points = [MagePoint([.1,.2,.3]),MagePoint([.25,.25,.25],Color='red', Label='label',State='L'),MagePoint([1/3,1/3,0]), MagePoint([0,0,0]),MagePoint([1/7,2/7,3/7])] for m in points: b = m.toCartesian().fromCartesian() self.assertFloatEqual(m.Coordinates,b.Coordinates) self.assertEqual(m.Color,b.Color) self.assertEqual(m.Label,b.Label) self.assertEqual(m.State,b.State) #even after multiple iterations? mutant = deepcopy(m) for x in range(10): mutant = mutant.toCartesian().fromCartesian() self.assertFloatEqual(m.Coordinates,mutant.Coordinates)
def test_set_coord(self): """MagePoint _get_coord should return coordinate that is asked for""" m = MagePoint([0,1,2]) m.X, m.Y, m.Z = 2,3,4 self.assertEqual(m.Coordinates,[2,3,4]) m = MagePoint() m.X, m.Y, m.Z = 5,4,3 self.assertEqual(m.Coordinates,[5,4,3]) m = MagePoint() m.X = 5 self.assertEqual(m.Coordinates,[5,0,0])
def MagePointFromString(line): """Constructs a new MagePoint from a one-line string.""" #handle the label if there is one: note that it might contain spaces line = line.strip() result = MagePoint() label = extract_delimited(line, '{', '}') if label: pieces = line.replace('{'+label+'}', '').split() else: pieces = line.split() fields = [] #also have to take into account the possibility of comma-delimited parts for p in pieces: fields.extend(filter(None, p.split(','))) pieces = fields result.Label = label #get the coordinates and remove them from the list of items result.Coordinates = map(float, pieces[-3:]) pieces = pieces[:-3] #parse the remaining attributes in more detail result.State = None result.Width = None result.Radius = None result.Color = None for attr in pieces: #handle radius if attr.startswith('r='): #radius: note case sensitivity result.Radius = float(attr[2:]) #handle single-character attributes elif len(attr) == 1: result.State = attr #handle line width elif attr.startswith('width'): result.Width = int(attr[5:]) else: #otherwise assume it's a color label result.Color = attr return result