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