From 1a369d53def607f526bc5c9af29b699d68086837 Mon Sep 17 00:00:00 2001 From: Walter Bender Date: Wed, 07 Jul 2010 12:04:52 +0000 Subject: pep8 --- diff --git a/NEWS b/NEWS index 5032112..f95820e 100644 --- a/NEWS +++ b/NEWS @@ -1,3 +1,11 @@ +23 + +* general code cleanup + +22 + +* fixed SVG bug in old Sugar builds + 21 * general clean up of sprite library diff --git a/VisualMatchActivity.py b/VisualMatchActivity.py index 8bc513f..4979dab 100644 --- a/VisualMatchActivity.py +++ b/VisualMatchActivity.py @@ -136,7 +136,7 @@ class VisualMatchActivity(activity.Activity): if not hasattr(self, '_saved_state'): self._saved_state = None self.vmw.new_game(self._saved_state, self._deck_index) - if self._editing_word_list is True: + if self._editing_word_list: self.vmw.editing_word_list = True self.vmw.edit_word_list() @@ -481,17 +481,17 @@ class VisualMatchActivity(activity.Activity): """ Dump game data to the journal.""" data = [] for i in self.vmw.grid.grid: - if i is None or self.vmw.editing_word_list == True: + if i is None or self.vmw.editing_word_list: data.append(None) else: data.append(i.index) for i in self.vmw.clicked: - if i is None or self.vmw.editing_word_list == True: + if i is None or self.vmw.editing_word_list: data.append(None) else: data.append(self.vmw.deck.spr_to_card(i).index) for i in self.vmw.deck.cards: - if i is None or self.vmw.editing_word_list == True: + if i is None or self.vmw.editing_word_list: data.append(None) else: data.append(i.index) diff --git a/activity/activity-visualmatch.svg b/activity/activity-visualmatch.svg index 082a3db..fa3f393 100644 --- a/activity/activity-visualmatch.svg +++ b/activity/activity-visualmatch.svg @@ -12,7 +12,7 @@ id="g2925" style="fill:&stroke_color;;fill-opacity:1;stroke-width:3;stroke-miterlimit:4;stroke-dasharray:none"> @@ -22,7 +22,7 @@ id="g2925-4" style="fill:&stroke_color;;fill-opacity:1;stroke-width:3;stroke-miterlimit:4;stroke-dasharray:none"> @@ -32,7 +32,7 @@ id="g3063" style="fill:none;stroke-width:3;stroke-miterlimit:4;stroke-dasharray:none"> diff --git a/deck.py b/deck.py index b35d191..87b1ee7 100644 --- a/deck.py +++ b/deck.py @@ -15,7 +15,7 @@ pygtk.require('2.0') import gtk from random import randrange -from constants import HIGH, FILLS, SHAPES, NUMBER, COLORS +from constants import HIGH, FILLS, SHAPES, NUMBER, COLORS, COLOR_PAIRS from card import Card from gencards import generate_pattern_card, generate_number_card, \ generate_word_card diff --git a/game.py b/game.py index 8569d25..be85739 100644 --- a/game.py +++ b/game.py @@ -126,7 +126,7 @@ class Game(): self.deck.hide() self.deck.shuffle() self.grid.deal(self.deck) - if self._find_a_match() is False: + if not self._find_a_match(): self.grid.deal_extra_cards(self.deck) self.matches = 0 self.robot_matches = 0 @@ -149,8 +149,7 @@ class Game(): def _sharing(self): """ Are we sharing? """ - if self.sugar is True and \ - hasattr(self.activity, 'chattube') and \ + if self.sugar and hasattr(self.activity, 'chattube') and \ self.activity.chattube is not None: return True return False diff --git a/gencards.py b/gencards.py index 3022cee..cd16e9a 100755 --- a/gencards.py +++ b/gencards.py @@ -76,8 +76,8 @@ class SVG: def _svg_check(self, x): svg_string = "%s%s%s%s%s%f%s" %\ - (" len(self.list)-1: + elif i > len(self.list) - 1: self.list.append(spr) else: self.list.insert(i, spr) @@ -122,17 +120,18 @@ class Sprites: list = self.list[:] list.reverse() for spr in list: - if spr.hit(pos): return spr + if spr.hit(pos): + return spr return None def redraw_sprites(self): for spr in self.list: spr.draw() -# -# A class for the individual sprites -# + class Sprite: + """ A class for the individual sprites """ + def __init__(self, sprites, x, y, image): self._sprites = sprites self._x = int(x) @@ -147,20 +146,20 @@ class Sprite: self._color = None self._width = 0 self._height = 0 - self._margins = [0,0,0,0] + self._margins = [0, 0, 0, 0] self.layer = 100 self.labels = [] self.images = [] - self._dx = [] # image offsets + self._dx = [] # image offsets self._dy = [] self.set_image(image) self._sprites.append_to_list(self) def set_image(self, image, i=0, dx=0, dy=0): - while len(self.images) < i+1: - self.images.append(None) - self._dx.append(0) - self._dy.append(0) + while len(self.images) < i + 1: + self.images.append(None) + self._dx.append(0) + self._dy.append(0) self.images[i] = image self._dx[i] = dx self._dy[i] = dy @@ -169,7 +168,7 @@ class Sprite: _h = self.images[i].get_height() else: _w, _h = self.images[i].get_size() - if i == 0: # Always reset width and height when base image changes. + if i == 0: # Always reset width and height when base image changes. self._width = _w + dx self._height = _h + dy else: @@ -180,7 +179,7 @@ class Sprite: def move(self, pos): self.inval() - self._x,self._y = int(pos[0]),int(pos[1]) + self._x, self._y = int(pos[0]), int(pos[1]) self.inval() def move_relative(self, pos): @@ -218,25 +217,25 @@ class Sprite: self._extend_labels_array(i) if type(new_label) is str or type(new_label) is unicode: # pango doesn't like nulls - self.labels[i] = new_label.replace("\0"," ") + self.labels[i] = new_label.replace("\0", " ") else: self.labels[i] = str(new_label) self.inval() def set_margins(self, l=0, t=0, r=0, b=0): - self._margins = [l,t,r,b] + self._margins = [l, t, r, b] def _extend_labels_array(self, i): if self._fd is None: - self.set_font('Sans') + self.set_font('Sans') if self._color is None: - self._color = self._sprites.cm.alloc_color('black') - while len(self.labels) < i+1: - self.labels.append(" ") - self._scale.append(self._scale[0]) - self._rescale.append(self._rescale[0]) - self._horiz_align.append(self._horiz_align[0]) - self._vert_align.append(self._vert_align[0]) + self._color = self._sprites.cm.alloc_color('black') + while len(self.labels) < i + 1: + self.labels.append(" ") + self._scale.append(self._scale[0]) + self._rescale.append(self._rescale[0]) + self._horiz_align.append(self._horiz_align[0]) + self._vert_align.append(self._vert_align[0]) def set_font(self, font): self._fd = pango.FontDescription(font) @@ -257,19 +256,19 @@ class Sprite: self._sprites.remove_from_list(self) def inval(self): - self._sprites.area.invalidate_rect( - gtk.gdk.Rectangle(self._x,self._y,self._width,self._height), False) + self._sprites.area.invalidate_rect(gtk.gdk.Rectangle(self._x, self._y, + self._width, self._height), False) def draw(self): - for i,img in enumerate(self.images): + for i, img in enumerate(self.images): if isinstance(img, gtk.gdk.Pixbuf): self._sprites.area.draw_pixbuf( - self._sprites.gc, img, 0, 0, self._x+self._dx[i], - self._y+self._dy[i]) + self._sprites.gc, img, 0, 0, self._x + self._dx[i], + self._y + self._dy[i]) elif img is not None: self._sprites.area.draw_drawable( - self._sprites.gc, img, 0, 0, self._x+self._dx[i], - self._y+self._dy[i], -1, -1) + self._sprites.gc, img, 0, 0, self._x + self._dx[i], + self._y + self._dy[i], -1, -1) if len(self.labels) > 0: self.draw_label() @@ -277,52 +276,52 @@ class Sprite: x, y = pos if x < self._x: return False - if x > self._x+self._width: + if x > self._x + self._width: return False if y < self._y: return False - if y > self._y+self._height: + if y > self._y + self._height: return False return True def draw_label(self): - my_width = self._width-self._margins[0]-self._margins[2] + my_width = self._width - self._margins[0] - self._margins[2] if my_width < 0: my_width = 0 - my_height = self._height-self._margins[1]-self._margins[3] + my_height = self._height - self._margins[1] - self._margins[3] for i in range(len(self.labels)): pl = self._sprites.canvas.create_pango_layout(str(self.labels[i])) - self._fd.set_size(int(self._scale[i]*pango.SCALE)) + self._fd.set_size(int(self._scale[i] * pango.SCALE)) pl.set_font_description(self._fd) - w = pl.get_size()[0]/pango.SCALE + w = pl.get_size()[0] / pango.SCALE if w > my_width: - if self._rescale[i] is True: + if self._rescale[i]: self._fd.set_size( - int(self._scale[i]*pango.SCALE*my_width/w)) + int(self._scale[i] * pango.SCALE * my_width / w)) pl.set_font_description(self._fd) - w = pl.get_size()[0]/pango.SCALE + w = pl.get_size()[0] / pango.SCALE else: - j = len(self.labels[i])-1 + j = len(self.labels[i]) - 1 while(w > my_width and j > 0): pl = self._sprites.canvas.create_pango_layout( - "…"+self.labels[i][len(self.labels[i])-j:]) - self._fd.set_size(int(self._scale[i]*pango.SCALE)) + "…" + self.labels[i][len(self.labels[i]) - j:]) + self._fd.set_size(int(self._scale[i] * pango.SCALE)) pl.set_font_description(self._fd) - w = pl.get_size()[0]/pango.SCALE + w = pl.get_size()[0] / pango.SCALE j -= 1 if self._horiz_align[i] == "center": - x = int(self._x+self._margins[0]+(my_width-w)/2) + x = int(self._x + self._margins[0] + (my_width - w) / 2) elif self._horiz_align[i] == 'left': - x = int(self._x+self._margins[0]) + x = int(self._x + self._margins[0]) else: # right - x = int(self._x+self._width-w-self._margins[2]) - h = pl.get_size()[1]/pango.SCALE + x = int(self._x + self._width - w - self._margins[2]) + h = pl.get_size()[1] / pango.SCALE if self._vert_align[i] == "middle": - y = int(self._y+self._margins[1]+(my_height-h)/2) + y = int(self._y + self._margins[1] + (my_height - h) / 2) elif self._vert_align[i] == "top": - y = int(self._y+self._margins[1]) + y = int(self._y + self._margins[1]) else: # bottom - y = int(self._y+self._height-h-self._margins[3]) + y = int(self._y + self._height - h - self._margins[3]) self._sprites.gc.set_foreground(self._color) self._sprites.area.draw_layout(self._sprites.gc, x, y, pl) @@ -330,37 +329,37 @@ class Sprite: max = 0 for i in range(len(self.labels)): pl = self._sprites.canvas.create_pango_layout(self.labels[i]) - self._fd.set_size(int(self._scale[i]*pango.SCALE)) + self._fd.set_size(int(self._scale[i] * pango.SCALE)) pl.set_font_description(self._fd) - w = pl.get_size()[0]/pango.SCALE + w = pl.get_size()[0] / pango.SCALE if w > max: max = w return max def label_safe_width(self): - return self._width-self._margins[0]-self._margins[2] - + return self._width - self._margins[0] - self._margins[2] + def label_safe_height(self): - return self._height-self._margins[1]-self._margins[3] - + return self._height - self._margins[1] - self._margins[3] + def label_left_top(self): - return (self._margins[0], self._margins[1]) + return(self._margins[0], self._margins[1]) def get_pixel(self, pos, i=0): x, y = pos - x = x-self._x - y = y-self._y - if y > self.images[i].get_height()-1: - return (-1,-1,-1,-1) + x = x - self._x + y = y - self._y + if y > self.images[i].get_height() - 1: + return(-1, -1, -1, -1) try: array = self.images[i].get_pixels() if array is not None: - offset = (y*self.images[i].get_width()+x)*4 - r,g,b,a = ord(array[offset]), ord(array[offset+1]),\ - ord(array[offset+2]), ord(array[offset+3]) - return (r,g,b,a) + offset = (y * self.images[i].get_width() + x) * 4 + r, g, b, a = ord(array[offset]), ord(array[offset + 1]),\ + ord(array[offset + 2]), ord(array[offset + 3]) + return(r, g, b, a) else: - return (-1,-1,-1,-1) + return(-1, -1, -1, -1) except IndexError: print "Index Error: %d %d" % (len(array), offset) - return (-1,-1,-1,-1) + return(-1, -1, -1, -1) -- cgit v0.9.1