From dcd7408d2f4c18e9342119829aa039693c276b31 Mon Sep 17 00:00:00 2001 From: Walter Bender Date: Sun, 10 Oct 2010 22:22:37 +0000 Subject: pep8 cleanup --- (limited to 'TurtleArt/talogo.py') diff --git a/TurtleArt/talogo.py b/TurtleArt/talogo.py index 0801d75..0d467ac 100644 --- a/TurtleArt/talogo.py +++ b/TurtleArt/talogo.py @@ -47,9 +47,12 @@ _logger = logging.getLogger('turtleart-activity') class noKeyError(UserDict): + __missing__ = lambda x, y: 0 + class symbol: + def __init__(self, name): self.name = name self.nargs = None @@ -57,17 +60,22 @@ class symbol: def __str__(self): return self.name + def __repr__(self): return '#' + self.name + class logoerror(Exception): + def __init__(self, value): self.value = value + def __str__(self): return repr(self.value) # Utility functions + def numtype(x): """ Is x a number type? """ if type(x) == int: @@ -78,9 +86,10 @@ def numtype(x): return True return False + def str_to_num(x): """ Try to comvert a string to a number """ - xx = convert(x.replace(self.tw.decimal_point,'.'), float) + xx = convert(x.replace(self.tw.decimal_point, '.'), float) if type(xx) is float: return xx else: @@ -90,18 +99,21 @@ def str_to_num(x): else: raise logoerror("#syntaxerror") + def taand(x, y): """ Logical and """ - return x&y + return x & y + def taor(x, y): """ Logical or """ - return x|y + return x | y + def careful_divide(x, y): """ Raise error on divide by zero """ try: - return x/y + return x / y except ZeroDivisionError: raise logoerror("#zerodivide") except TypeError: @@ -114,10 +126,11 @@ def careful_divide(x, y): except TypeError: raise logoerror("#notanumber") + def taequal(x, y): """ Numeric and logical equal """ try: - return float(x)==float(y) + return float(x) == float(y) except TypeError: typex, typey = False, False if strtype(x): @@ -131,10 +144,11 @@ def taequal(x, y): except ValueError: raise logoerror("#syntaxerror") + def taless(x, y): """ Compare numbers and strings """ try: - return float(x)4: # There could be a '(', ')', '[' or ']'. + if len(dock) > 4: # There could be a '(', ')', '[' or ']'. code.append(dock[4]) if blk.name == 'savesvg': self.tw.saving_svg = True if blk.primitive is not None: # make a tuple (prim, blk) code.append((blk.primitive, self.tw.block_list.list.index(blk))) - elif len(blk.values)>0: # Extract the value from content blocks. + elif len(blk.values) > 0: # Extract the value from content blocks. if blk.name == 'number': try: code.append(float(blk.values[0])) @@ -495,22 +524,22 @@ class LogoCode: if type(blk.values[0]) == float or type(blk.values[0]) == int: if int(blk.values[0]) == blk.values[0]: blk.values[0] = int(blk.values[0]) - code.append('#s'+str(blk.values[0])) + code.append('#s' + str(blk.values[0])) else: - code.append('#s'+blk.values[0]) + code.append('#s' + blk.values[0]) elif blk.name == 'journal': if blk.values[0] is not None: - code.append('#smedia_'+str(blk.values[0])) + code.append('#smedia_' + str(blk.values[0])) else: code.append('#smedia_None') elif blk.name == 'description': if blk.values[0] is not None: - code.append('#sdescr_'+str(blk.values[0])) + code.append('#sdescr_' + str(blk.values[0])) else: code.append('#sdescr_None') elif blk.name == 'audio': if blk.values[0] is not None: - code.append('#saudio_'+str(blk.values[0])) + code.append('#saudio_' + str(blk.values[0])) else: code.append('#saudio_None') else: @@ -519,9 +548,9 @@ class LogoCode: return ['%nothing%'] if blk.connections is not None and len(blk.connections) > 0: for i in range(1, len(blk.connections)): - b = blk.connections[i] + b = blk.connections[i] dock = blk.docks[i] - if len(dock)>4: # There could be a '(', ')', '[' or ']'. + if len(dock) > 4: # There could be a '(', ')', '[' or ']'. for c in dock[4]: code.append(c) if b is not None: @@ -529,7 +558,7 @@ class LogoCode: elif blk.docks[i][0] not in ['flow', 'unavailable']: code.append('%nothing%') return code - + def setup_cmd(self, string): """ Execute the psuedocode. """ self.hidden_turtle = self.tw.active_turtle @@ -538,12 +567,12 @@ class LogoCode: blklist = self.readline(string) self.step = self.start_eval(blklist) - """ - Convert the pseudocode into a list of commands. + def readline(self, line): + """ + Convert the pseudocode into a list of commands. The block associated with the command is stored as the second element in a tuple, e.g., (#forward, 16) - """ - def readline(self, line): + """ res = [] while line: token = line.pop(0) @@ -609,8 +638,8 @@ class LogoCode: # In debugging modes, we pause between steps and show the turtle. if self.tw.step_time > 0: self.tw.active_turtle.show() - endtime = millis()+self.an_int(self.tw.step_time)*100 - while millis() 0: self.tw.display_coordinates() yield True - + def eval(self): """ Evaluate the next token on the line of code we are processing. """ token = self.iline.pop(0) @@ -706,12 +735,12 @@ class LogoCode: """ ufuncall """ self.ijmp(self.evline, body) yield True - + def doevalstep(self): """ evaluate one step """ starttime = millis() try: - while (millis()-starttime)<120: + while (millis() - starttime)<120: try: if self.step is not None: self.step.next() @@ -764,7 +793,7 @@ class LogoCode: my_string += "%s: %s\n" % (k, str(v)) self.tw.showlabel('info', my_string) return - + def undefined_check(self, token): """ Make sure token has a definition """ if token.fcn is not None: @@ -774,13 +803,13 @@ class LogoCode: else: errormsg = "%s %s" % (_("I don't know how to"), _(token.name)) raise logoerror(errormsg) - + def no_args_check(self): """ Missing argument ? """ if self.iline and self.iline[0] is not self.symnothing: return raise logoerror("#noinput") - + # # Primitives # @@ -802,13 +831,13 @@ class LogoCode: def prim_wait(self, time): """ Show the turtle while we wait """ self.tw.active_turtle.show() - endtime = millis()+self.an_int(time*1000) + endtime = millis() + self.an_int(time * 1000) while millis()