import math from Command import * class Program: def __init__(self, commands=None): self.commands = commands or [] self.active=True self.visible=False self.filename='' def parsefile(self, filename): with open(filename) as file: self.parse( file.read()) def parse(self, code): for command in code.strip().split(";")[:-1]: name, args = command[:2], command[2:] args = [float(arg) for arg in args.split(",")] if args else [] # Experimental #if name=='CI': #self.commands.append(Command('PR',args[0])) #self.commands.append(Command('PR',args[0])) #self.commands.append(Command('PR',args[0])) self.commands.append(Command(name, *args)) def __trunc__ (self): return Program([int(command) for command in self.commands]) def __str__(self): return "".join(str(command) for command in self.commands) def __mul__(self, arg): return Program([command * arg for command in self.commands]) def __add__(self, arg): if type(arg)== type(self): return Program( self.commands + arg.commands ) return Program([command + arg for command in self.commands]) def __sub__(self, arg): return Program([command - arg for command in self.commands]) def __len__(self): return len(str(self)) def rotate(self, angl): return Program([command.rotate(angl) for command in self.commands]) def flip(self): return Program([command.flip() for command in self.commands if command]) @property def xmax(self): return max(command.x for command in self.commands if command.x and command.movable) @property def xmin(self): return min(command.x for command in self.commands if command.x and command.movable) @property def ymax(self): return max(command.y for command in self.commands if command.y and command.movable) @property def ymin(self): return min(command.y for command in self.commands if command.y and command.movable) @property def center(self): return ((self.xmin+self.xmax)/2),((self.ymin+self.ymax)/2) @property def winsize(self): return self.xmax-self.xmin , self.ymax - self.ymin