From b9029092b88a641f8ff89d8a22c724201d2f40b0 Mon Sep 17 00:00:00 2001 From: Walter Bender Date: Thu, 11 Mar 2010 19:02:59 +0000 Subject: pylint cleanup --- (limited to 'talogo.py') diff --git a/talogo.py b/talogo.py index 54cb7fb..99c2725 100644 --- a/talogo.py +++ b/talogo.py @@ -22,28 +22,25 @@ #THE SOFTWARE. import gtk -import gobject from time import clock from math import sqrt from random import uniform from operator import isNumberType -import audioop -import subprocess from UserDict import UserDict try: from sugar.datastore import datastore except: pass -from taconstants import PALETTES, PALETTE_NAMES, BOX_STYLE, TAB_LAYER +from taconstants import PALETTES, PALETTE_NAMES, TAB_LAYER from tagplay import play_audio, play_movie_from_file, stop_media from tajail import myfunc, myfunc_import -from tautils import get_pixbuf_from_journal, movie_media_type,\ +from tautils import get_pixbuf_from_journal, movie_media_type, convert, \ audio_media_type, text_media_type, round_int from gettext import gettext as _ class noKeyError(UserDict): - __missing__=lambda x,y: 0 + __missing__ = lambda x, y: 0 class symbol: def __init__(self, name): @@ -54,7 +51,7 @@ class symbol: def __str__(self): return self.name def __repr__(self): - return '#'+self.name + return '#' + self.name class logoerror(Exception): def __init__(self, value): @@ -62,28 +59,10 @@ class logoerror(Exception): def __str__(self): return repr(self.value) -""" -Utility functions -""" - -''' -The strategy for mixing numbers and strings is to first try -converting the string to a float; then if the string is a single -character, try converting it to an ord; finally, just treat it as a -string. Numbers appended to strings are first trreated as ints, then -floats. -''' -def convert(x, fn, try_ord=True): - try: - return fn(x) - except ValueError: - if try_ord: - xx, flag = chr_to_ord(x) - if flag: - return fn(xx) - return x +# Utility functions def numtype(x): + """ Is x a number type? """ if type(x) == int: return True if type(x) == float: @@ -93,6 +72,7 @@ def numtype(x): return False def strtype(x): + """ Is x a string type? """ if type(x) == str: return True if type(x) == unicode: @@ -100,6 +80,7 @@ def strtype(x): return False def str_to_num(x): + """ Try to comvert a string to a number """ xx = convert(x, float) if type(xx) is float: return xx @@ -111,6 +92,7 @@ def str_to_num(x): raise logoerror("#syntaxerror") def chr_to_ord(x): + """ Try to comvert a string to an ord """ if strtype(x) and len(x) == 1: try: return ord(x[0]), True @@ -119,12 +101,15 @@ def chr_to_ord(x): return x, False def taand(x, y): + """ Logical and """ return x&y def taor(x, y): + """ Logical or """ return x|y def careful_divide(x, y): + """ Raise error on divide by zero """ try: return x/y except ZeroDivisionError: @@ -138,6 +123,7 @@ def careful_divide(x, y): raise logoerror("#syntaxerror") def taequal(x, y): + """ Numeric and logical equal """ try: return float(x)==float(y) except TypeError: @@ -154,6 +140,7 @@ def taequal(x, y): raise logoerror("#syntaxerror") def taless(x, y): + """ Compare numbers and strings """ try: return float(x)0: # Extract the value from content blocks. - if blk.name=='number': + if blk.name == 'number': try: code.append(float(blk.values[0])) except ValueError: code.append(float(ord(blk.values[0][0]))) - elif blk.name=='string' or blk.name=='title': + elif blk.name == 'string' or blk.name == 'title': 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])) else: code.append('#s'+blk.values[0]) - elif blk.name=='journal': + elif blk.name == 'journal': if blk.values[0] is not None: code.append('#smedia_'+str(blk.values[0])) else: code.append('#smedia_None') - elif blk.name=='description': + elif blk.name == 'description': if blk.values[0] is not None: code.append('#sdescr_'+str(blk.values[0])) else: code.append('#sdescr_None') - elif blk.name=='audio': + elif blk.name == 'audio': if blk.values[0] is not None: code.append('#saudio_'+str(blk.values[0])) else: @@ -533,14 +524,12 @@ class LogoCode: code.append('%nothing%') return code - """ - Execute the psuedocode. - """ - def setup_cmd(self, str): + def setup_cmd(self, string): + """ Execute the psuedocode. """ self.tw.active_turtle.hide() # Hide the turtle while we are running. self.procstop = False - list = self.readline(str) - self.step = self.start_eval(list) + blklist = self.readline(string) + self.step = self.start_eval(blklist) """ Convert the pseudocode into a list of commands. @@ -553,12 +542,12 @@ class LogoCode: token = line.pop(0) bindex = None if type(token) == tuple: - (token, bindex) = token + (token, bindex) = token if isNumberType(token): res.append(token) elif token.isdigit(): res.append(float(token)) - elif token[0]=='-' and token[1:].isdigit(): + elif token[0] == '-' and token[1:].isdigit(): res.append(-float(token[1:])) elif token[0] == '"': res.append(token[1:]) @@ -574,16 +563,14 @@ class LogoCode: res.append((self.intern(token), bindex)) return res - """ - Step through the list. - """ - def start_eval(self, list): + def start_eval(self, blklist): + """ Step through the list. """ if self.tw.running_sugar: self.tw.activity.stop_button.set_icon("stopiton") else: self.tw.toolbar_shapes['stopiton'].set_layer(TAB_LAYER) self.running = True - self.icall(self.evline, list) + self.icall(self.evline, blklist) yield True if self.tw.running_sugar: self.tw.activity.stop_button.set_icon("stopitoff") @@ -592,19 +579,15 @@ class LogoCode: yield False self.running = False - """ - Add a function and its arguments to the program stack. - """ def icall(self, fcn, *args): + """ Add a function and its arguments to the program stack. """ self.istack.append(self.step) self.step = fcn(*(args)) - """ - Evaluate a line of code from the list. - """ - def evline(self, list): + def evline(self, blklist): + """ Evaluate a line of code from the list. """ oldiline = self.iline - self.iline = list[:] + self.iline = blklist[:] self.arglist = None while self.iline: token = self.iline[0] @@ -652,10 +635,8 @@ class LogoCode: self.tw.display_coordinates() yield True - """ - Evaluate the next token on the line of code we are processing. - """ def eval(self): + """ Evaluate the next token on the line of code we are processing. """ token = self.iline.pop(0) bindex = None if type(token) == tuple: @@ -680,10 +661,8 @@ class LogoCode: self.ireturn(res) yield True - """ - Process primitive associated with symbol token - """ def evalsym(self, token): + """ Process primitive associated with symbol token """ self.debug_trace(token) self.undefined_check(token) oldcfun, oldarglist = self.cfun, self.arglist @@ -717,11 +696,12 @@ class LogoCode: yield True def ufuncall(self, body): - print "ufuncall: ", self.evline, body - ijmp(self.evline, body) + """ ufuncall """ + self.ijmp(self.evline, body) yield True def doevalstep(self): + """ evaluate one step """ starttime = millis() try: while (millis()-starttime)<120: @@ -740,18 +720,19 @@ class LogoCode: return True def ireturn(self, res=None): + """ return value """ self.step = self.istack.pop() - # print "ireturn: ", self.step self.iresult = res def ijmp(self, fcn, *args): - # print "ijmp: ", fcn, args + """ ijmp """ self.step = fcn(*(args)) def debug_trace(self, token): + """ Display debugging information """ if self.trace: if token.name in PALETTES[PALETTE_NAMES.index('turtle')]: - my_string = "%s\n%s=%d\n%s=%d\n%s=%d\n%s=%d" %\ + my_string = "%s\n%s=%d\n%s=%d\n%s=%d\n%s=%d" % \ (token.name, _('xcor'), int(self.tw.canvas.xcor), _('ycor'), int(self.tw.canvas.ycor), _('heading'), int(self.tw.canvas.heading), _('scale'), int(self.scale)) @@ -760,7 +741,7 @@ class LogoCode: penstatus = _('pen down') else: penstatus = _('pen up') - my_string = "%s\n%s\n%s=%d\n%s=%d\n%s=%.1f" %\ + my_string = "%s\n%s\n%s=%d\n%s=%d\n%s=%.1f" % \ (token.name, penstatus, _('color'), int(self.tw.canvas.color), _('shade'), int(self.tw.canvas.shade), _('pen size'), @@ -769,10 +750,11 @@ class LogoCode: my_string = "%s\n" % (token.name) for k, v in self.boxes.iteritems(): my_string += "%s: %s\n" % (k, str(v)) - self.tw.showlabel('info',my_string) + self.tw.showlabel('info', my_string) return def undefined_check(self, token): + """ Make sure token has a definition """ if token.fcn is not None: return False if token.name == '%nothing%': @@ -782,6 +764,7 @@ class LogoCode: 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") @@ -791,14 +774,17 @@ class LogoCode: # def prim_clear(self): + """ Clear screen """ stop_media(self) self.tw.canvas.clearscreen() def prim_start(self): + """ Start block: recenter """ if self.tw.running_sugar: self.tw.activity.recenter() def prim_wait(self, time): + """ Show the turtle while we wait """ self.tw.active_turtle.show() endtime = millis()+self.an_int(time*1000) while millis()