From 7096a96d59d40b9802bf7002b9233b8b84871953 Mon Sep 17 00:00:00 2001 From: skomoroh Date: Sat, 25 Aug 2007 21:19:40 +0000 Subject: [PATCH] * refactoring: replaced map(lambda...), filter(lambda...) to list comprehension (thanks to 2to3) git-svn-id: https://pysolfc.svn.sourceforge.net/svnroot/pysolfc/PySolFC/trunk@199 39dd0a4e-7c14-0410-91b3-c4f2d318f732 --- pysollib/actions.py | 3 ++- pysollib/app.py | 22 ++++++++++++---------- pysollib/game.py | 16 ++++++++++------ pysollib/games/acesup.py | 2 +- pysollib/games/canfield.py | 2 +- pysollib/games/fan.py | 3 ++- pysollib/games/mahjongg/mahjongg.py | 6 +++--- pysollib/games/montana.py | 4 ++-- pysollib/games/siebenbisas.py | 6 +++--- pysollib/games/special/pegged.py | 4 ++-- pysollib/games/spider.py | 2 +- pysollib/games/terrace.py | 2 +- pysollib/move.py | 3 +-- pysollib/pysolgtk/selectcardset.py | 2 +- pysollib/pysolgtk/selecttile.py | 4 ++-- pysollib/resource.py | 10 +++++----- pysollib/stack.py | 8 ++++---- pysollib/tile/selectcardset.py | 2 +- pysollib/tile/selecttile.py | 4 ++-- pysollib/tk/selectcardset.py | 2 +- pysollib/tk/selecttile.py | 4 ++-- 21 files changed, 59 insertions(+), 52 deletions(-) diff --git a/pysollib/actions.py b/pysollib/actions.py index 54917d87..0b3fa166 100644 --- a/pysollib/actions.py +++ b/pysollib/actions.py @@ -40,6 +40,7 @@ import os, locale # PySol imports from mfxutil import SubclassResponsibility from mfxutil import Struct, openURL +from mfxutil import print_err from pysolrandom import constructRandom from settings import TITLE, PACKAGE_URL from settings import TOP_TITLE @@ -651,7 +652,7 @@ class PysolMenubarActions: ## TODO pass else: - print "stats problem:", mode, demo, player + print_err("stats problem: %s %s %s" % (mode, demo, player)) pass if d.status != 0: break diff --git a/pysollib/app.py b/pysollib/app.py index 8ed276ce..b4108726 100644 --- a/pysollib/app.py +++ b/pysollib/app.py @@ -142,6 +142,7 @@ class GameStat: game_start_time = game.gstats.start_time # update number of games # status: + # -1 - NOT WON (not played) # 0 - LOST # 1 - WON # 2 - PERFECT @@ -223,7 +224,7 @@ class Statistics: if gameid == 0: del games[player] else: - games[player] = filter(lambda a, b=gameid: a[0] != b, games[player]) + games[player] = [g for g in games[player] if g[0] != gameid] def getStats(self, player, gameid): # returned (won, lost) @@ -241,10 +242,10 @@ class Statistics: return (0, 0, 0, 0) def getSessionStats(self, player, gameid): - g = self.session_games.get(player, []) - g = filter(lambda a, b=gameid: a[0] == b, g) - won = len(filter(lambda a, b=gameid: a[2] > 0, g)) - lost = len(filter(lambda a, b=gameid: a[2] == 0, g)) + games = self.session_games.get(player, []) + games = [g for g in games if g[0] == gameid] + won = len([g for g in games if g[2] > 0]) + lost = len([g for g in games if g[2] == 0]) return won, lost def updateStats(self, player, game, status): @@ -1291,11 +1292,11 @@ Please select a %s type %s. if not DEBUG: return if field: - print '_parseCardsetConfig error: line #%d, fields#%d %s' \ - % (line, field, msg) + print_err('_parseCardsetConfig error: line #%d, field #%d %s' + % (line, field, msg)) else: - print '_parseCardsetConfig error: line #%d: %s' \ - % (line, msg) + print_err('_parseCardsetConfig error: line #%d: %s' + % (line, msg)) if len(line) < 6: perr(1, msg='number of lines') return 0 @@ -1421,7 +1422,8 @@ Please select a %s type %s. found.append(cs) #print '+', cs.name else: - print 'fail _readCardsetConfig:', d, f1 + print_err('fail _readCardsetConfig: %s %s' + % (d, f1)) pass except Exception, err: ##traceback.print_exc() diff --git a/pysollib/game.py b/pysollib/game.py index 613a32e8..e064177b 100644 --- a/pysollib/game.py +++ b/pysollib/game.py @@ -161,8 +161,10 @@ class Game: if self.app.intro.progress: self.app.intro.progress.update(step=1) self.createGame() # set some defaults - self.sg.openstacks = filter(lambda s: s.cap.max_accept >= s.cap.min_accept, self.sg.openstacks) - self.sg.hp_stacks = filter(lambda s: s.cap.max_move >= 2, self.sg.dropstacks) + self.sg.openstacks = [s for s in self.sg.openstacks + if s.cap.max_accept >= s.cap.min_accept] + self.sg.hp_stacks = [s for s in self.sg.dropstacks + if s.cap.max_move >= 2] self.createSnGroups() # convert stackgroups to tuples (speed) self.allstacks = tuple(self.allstacks) @@ -307,8 +309,10 @@ class Game: # create game self.createGame() # set some defaults - self.sg.openstacks = filter(lambda s: s.cap.max_accept >= s.cap.min_accept, self.sg.openstacks) - self.sg.hp_stacks = filter(lambda s: s.cap.max_move >= 2, self.sg.dropstacks) + self.sg.openstacks = [s for s in self.sg.openstacks + if s.cap.max_accept >= s.cap.min_accept] + self.sg.hp_stacks = [s for s in self.sg.dropstacks + if s.cap.max_move >= 2] # init the stack view for stack in self.allstacks: stack.prepareStack() @@ -822,7 +826,7 @@ class Game: i = i - 1 sitems.sort() sitems.reverse() - scards = map(lambda item: item[2], sitems) + scards = [item[2] for item in sitems] return cards, scards @@ -2006,7 +2010,7 @@ Congratulations, you did it ! width = 4 xmargin, ymargin = 0, 0 x0, y0 = x+width/2-xmargin, y+width/2-ymargin - x1, y1 = x+w-width-xmargin, y+h-width-ymargin + x1, y1 = x+w-width/2-xmargin, y+h-width/2-ymargin r = MfxCanvasRectangle(self.canvas, x0, y0, x1, y1, width=width, fill=None, outline=color) self.canvas.update_idletasks() diff --git a/pysollib/games/acesup.py b/pysollib/games/acesup.py index 11d97604..0a08974d 100644 --- a/pysollib/games/acesup.py +++ b/pysollib/games/acesup.py @@ -150,7 +150,7 @@ class Fortunes(AcesUp): class RussianAces_Talon(DealRowTalonStack): def dealCards(self, sound=False): - rows = filter(lambda s: not s.cards, self.game.s.rows) + rows = [s for s in self.game.s.rows if not s.cards] if not rows: rows = self.game.s.rows return self.dealRowAvail(rows=rows, sound=sound) diff --git a/pysollib/games/canfield.py b/pysollib/games/canfield.py index c41afe38..79b5aa48 100644 --- a/pysollib/games/canfield.py +++ b/pysollib/games/canfield.py @@ -716,7 +716,7 @@ class Demon(Canfield): INITIAL_RESERVE_CARDS = 40 RowStack_Class = StackWrapper(AC_RowStack, mod=13) def createGame(self): - Canfield.createGame(self, rows=8, max_rounds=UNLIMITED_REDEALS, num_deal=1) + Canfield.createGame(self, rows=8, max_rounds=UNLIMITED_REDEALS, num_deal=3) # /*********************************************************************** diff --git a/pysollib/games/fan.py b/pysollib/games/fan.py index ba367154..8b3e5cb3 100644 --- a/pysollib/games/fan.py +++ b/pysollib/games/fan.py @@ -942,7 +942,8 @@ registerGame(GameInfo(516, Troika, "Troika", registerGame(GameInfo(517, Quads, "Quads", GI.GT_FAN_TYPE | GI.GT_OPEN | GI.GT_ORIGINAL, 1, 0, GI.SL_MOSTLY_SKILL)) registerGame(GameInfo(625, FascinationFan, "Fascination Fan", - GI.GT_FAN_TYPE, 1, 6, GI.SL_BALANCED)) + GI.GT_FAN_TYPE, 1, 6, GI.SL_BALANCED, + altnames=('Demon Fan',) )) registerGame(GameInfo(647, Crescent, "Crescent", GI.GT_FAN_TYPE, 2, 3, GI.SL_MOSTLY_SKILL)) registerGame(GameInfo(714, ShamrocksII, "Shamrocks II", diff --git a/pysollib/games/mahjongg/mahjongg.py b/pysollib/games/mahjongg/mahjongg.py index c30d3307..176652dc 100644 --- a/pysollib/games/mahjongg/mahjongg.py +++ b/pysollib/games/mahjongg/mahjongg.py @@ -202,11 +202,11 @@ class Mahjongg_RowStack(OpenStack): OpenStack._position(self, card) # if TOOLKIT == 'tk': - rows = filter(lambda s: s.cards, self.game.s.rows[:self.id]) + rows = [s for s in self.game.s.rows[:self.id] if s.cards] if rows: self.group.tkraise(rows[-1].group) return - rows = filter(lambda s: s.cards, self.game.s.rows[self.id+1:]) + rows = [s for s in self.game.s.rows[self.id+1:] if s.cards] if rows: self.group.lower(rows[0].group) return @@ -728,7 +728,7 @@ class AbstractMahjonggGame(Game): # check if this layout is solvable (backtracking) ret = create_solvable(cards[:], nc) if ret: - ret = filter(lambda x: x != 1, ret) + ret = [x for x in ret if x != 1] return ret nc[s1.id] = nc[s2.id] = None # try another way diff --git a/pysollib/games/montana.py b/pysollib/games/montana.py index cba58a66..0f6a4431 100644 --- a/pysollib/games/montana.py +++ b/pysollib/games/montana.py @@ -50,7 +50,7 @@ class Montana_Hint(DefaultHint): def computeHints(self): game = self.game RLEN, RSTEP, RBASE = game.RLEN, game.RSTEP, game.RBASE - freerows = filter(lambda s: not s.cards, game.s.rows) + freerows = [s for s in game.s.rows if not s.cards] # for each stack for r in game.s.rows: if not r.cards: @@ -272,7 +272,7 @@ class BlueMoon(Montana): if i % self.RSTEP == 0: # left column continue self.s.talon.dealRow(rows=(self.s.rows[i],), frames=frames) - ace_rows = filter(lambda r: r.cards and r.cards[-1].rank == ACE, self.s.rows) + ace_rows = [r for r in self.s.rows if r.cards and r.cards[-1].rank == ACE] j = 0 for r in ace_rows: self.moveMove(1, r, self.s.rows[j]) diff --git a/pysollib/games/siebenbisas.py b/pysollib/games/siebenbisas.py index 767d2fe8..a74eb6bf 100644 --- a/pysollib/games/siebenbisas.py +++ b/pysollib/games/siebenbisas.py @@ -51,7 +51,7 @@ from pysollib.hint import AbstractHint, DefaultHint, CautiousDefaultHint class SiebenBisAs_Hint(CautiousDefaultHint): def computeHints(self): game = self.game - freerows = filter(lambda s: not s.cards, game.s.rows) + freerows = [s for s in game.s.rows if not s.cards] # for each stack for r in game.sg.dropstacks: if not r.cards: @@ -149,7 +149,7 @@ class SiebenBisAs(Game): self.startDealSample() self.s.talon.dealRow() self.s.talon.dealRow(rows=self.s.reserves) - stacks = filter(lambda r: r.cards[-1].rank == 6, self.s.rows) + stacks = [r for r in self.s.rows if r.cards[-1].rank == 6] for r in stacks: self.moveMove(1, r, self.s.foundations[r.cards[-1].suit]) @@ -246,7 +246,7 @@ class Maze(Game): self.s.talon.dealRow(rows=(self.s.rows[i],), frames=frames) def isGameWon(self): - rows = filter(lambda s: s.cards, self.s.rows) + rows = [s for s in self.s.rows if s.cards] if len(rows) != 48: return False # no cards dealt yet i = 0 diff --git a/pysollib/games/special/pegged.py b/pysollib/games/special/pegged.py index 55a6b66d..eba92f25 100644 --- a/pysollib/games/special/pegged.py +++ b/pysollib/games/special/pegged.py @@ -51,7 +51,7 @@ class Pegged_Hint(AbstractHint): def computeHints(self): game = self.game # get free stacks - stacks = filter(lambda r: not r.cards, game.s.rows) + stacks = [r for r in game.s.rows if not r.cards] # for t in stacks: for dx, dy in game.STEPS: @@ -192,7 +192,7 @@ class Pegged(Game): def getWinStatus(self): won, status, updated = Game.getWinStatus(self) if status == 2: - stacks = filter(lambda r: r.cards, self.s.rows) + stacks = [r for r in self.s.rows if r.cards] assert len(stacks) == 1 if stacks[0].id != self.EMPTY_STACK_ID: # not perfect diff --git a/pysollib/games/spider.py b/pysollib/games/spider.py index aae21dc0..c693fb33 100644 --- a/pysollib/games/spider.py +++ b/pysollib/games/spider.py @@ -188,7 +188,7 @@ class GroundForADivorce_Talon(TalonStack): # A single click deals a new cards to each non-empty row. def dealCards(self, sound=True): if self.cards: - rows = filter(lambda r: r.cards, self.game.s.rows) + rows = [r for r in self.game.s.rows if r.cards] if not rows: # deal one card to first row if all rows are emtpy rows = self.game.s.rows[:1] diff --git a/pysollib/games/terrace.py b/pysollib/games/terrace.py index b13c2698..97169f4b 100644 --- a/pysollib/games/terrace.py +++ b/pysollib/games/terrace.py @@ -107,7 +107,7 @@ class Terrace_RowStack(AC_RowStack): self.game.moveMove(ncards, self, to_stack, frames=frames, shadow=shadow) for s in self.game.s.foundations: s.cap.base_rank = to_stack.cards[0].rank - freerows = filter(lambda s: not s.cards, self.game.s.rows) + freerows = [s for s in self.game.s.rows if not s.cards] self.game.s.talon.dealRow(rows=freerows, sound=True) self.game.s.talon.dealCards() # deal first card to WasteStack diff --git a/pysollib/move.py b/pysollib/move.py index 1bcb1e0e..44c17391 100644 --- a/pysollib/move.py +++ b/pysollib/move.py @@ -435,14 +435,13 @@ class AShuffleStackMove(AtomicMove): def __init__(self, stack, game): self.stack_id = stack.id # save cards and state - self.card_ids = tuple(map(lambda c: c.id, stack.cards)) + self.card_ids = tuple([c.id for c in stack.cards]) self.state = game.random.getstate() def redo(self, game): stack = game.allstacks[self.stack_id] # paranoia assert stack is game.s.talon - assert self.card_ids == tuple(map(lambda c: c.id, stack.cards)) # shuffle (see random) game.random.setstate(self.state) seq = stack.cards diff --git a/pysollib/pysolgtk/selectcardset.py b/pysollib/pysolgtk/selectcardset.py index 12ee2bb5..03594a27 100644 --- a/pysollib/pysolgtk/selectcardset.py +++ b/pysollib/pysolgtk/selectcardset.py @@ -158,7 +158,7 @@ class SelectCardsetDialogWithPreview(MfxDialog): gobject.TYPE_INT) manager = self.manager all_cardsets = manager.getAllSortedByName() - all_cardsets = filter(lambda obj: not obj.error, all_cardsets) + all_cardsets = [obj for obj in all_cardsets if not obj.error] cs = self._selectCardset(all_cardsets, None) self._addCardsets(store, None, 'All cadsets', cs) diff --git a/pysollib/pysolgtk/selecttile.py b/pysollib/pysolgtk/selecttile.py index 79b73ed2..4d6ace67 100644 --- a/pysollib/pysolgtk/selecttile.py +++ b/pysollib/pysolgtk/selecttile.py @@ -118,8 +118,8 @@ class SelectTileDialogWithPreview(MfxDialog): index += 1 # tiles = manager.getAllSortedByName() - tiles = filter(lambda obj: not obj.error, tiles) - tiles = filter(lambda tile: tile.index > 0 and tile.filename, tiles) + tiles = [obj for obj in tiles if not obj.error] + tiles = [tile for tile in tiles if tile.index > 0 and tile.filename] # iter = model.append(None) model.set(iter, 0, _('All Backgrounds'), 1, -1) diff --git a/pysollib/resource.py b/pysollib/resource.py index e01a0bac..e067209e 100644 --- a/pysollib/resource.py +++ b/pysollib/resource.py @@ -114,9 +114,9 @@ class ResourceManager: def getAllSortedByName(self): if self._objects_by_name is None: - l = map(lambda obj: (obj.getSortKey(), obj), self._objects) + l = [(obj.getSortKey(), obj) for obj in self._objects] l.sort() - self._objects_by_name = tuple(map(lambda item: item[1], l)) + self._objects_by_name = tuple([item[1] for item in l]) return self._objects_by_name # @@ -470,14 +470,14 @@ class CardsetManager(ResourceManager): cs.si.size = CSI.SIZE_XLARGE # keys = cs.styles[:] - cs.si.styles = tuple(filter(lambda s: s in CSI.STYLE, keys)) + cs.si.styles = tuple([s for s in keys if s in CSI.STYLE]) for s in cs.si.styles: self.registered_styles[s] = self.registered_styles.get(s, 0) + 1 - cs.si.nationalities = tuple(filter(lambda s: s in CSI.NATIONALITY, keys)) + cs.si.nationalities = tuple([s for s in keys if s in CSI.NATIONALITY]) for s in cs.si.nationalities: self.registered_nationalities[s] = self.registered_nationalities.get(s, 0) + 1 keys = (cs.year / 100,) - cs.si.dates = tuple(filter(lambda s: s in CSI.DATE, keys)) + cs.si.dates = tuple([s for s in keys if s in CSI.DATE]) for s in cs.si.dates: self.registered_dates[s] = self.registered_dates.get(s, 0) + 1 # diff --git a/pysollib/stack.py b/pysollib/stack.py index b2127fc3..f390b60f 100644 --- a/pysollib/stack.py +++ b/pysollib/stack.py @@ -200,7 +200,7 @@ def isAnySuitButOwnSequence(cards, mod=8192, dir=-1): return True def getNumberOfFreeStacks(stacks): - return len(filter(lambda s: not s.cards, stacks)) + return len([s for s in stacks if not s.cards]) # collect the top cards of several stacks into a pile def getPileFromStacks(stacks, reverse=0): @@ -372,11 +372,11 @@ class Stack: if isinstance(ox, int): self.CARD_XOFFSET = (ox,) else: - self.CARD_XOFFSET = tuple(map(int, map(round, ox))) + self.CARD_XOFFSET = tuple([int(round(x)) for x in ox]) if isinstance(oy, int): self.CARD_YOFFSET = (oy,) else: - self.CARD_YOFFSET = tuple(map(int, map(round, oy))) + self.CARD_YOFFSET = tuple([int(round(y)) for y in oy]) if self.can_hide_cards < 0: self.can_hide_cards = self.is_visible if self.cap.max_cards < 3: @@ -1946,7 +1946,7 @@ class GroundForADivorceTalonStack(DealRowRedealTalonStack): # A single click deals a new cards to each non-empty row. def dealCards(self, sound=True): if self.cards: - rows = filter(lambda r: r.cards, self.game.s.rows) + rows = [r for r in self.game.s.rows if r.cards] ## if not rows: ## # deal one card to first row if all rows are emtpy ## rows = self.game.s.rows[:1] diff --git a/pysollib/tile/selectcardset.py b/pysollib/tile/selectcardset.py index 94990a81..0efd6662 100644 --- a/pysollib/tile/selectcardset.py +++ b/pysollib/tile/selectcardset.py @@ -79,7 +79,7 @@ class SelectCardsetData(SelectDialogTreeData): def __init__(self, manager, key): SelectDialogTreeData.__init__(self) self.all_objects = manager.getAllSortedByName() - self.all_objects = filter(lambda obj: not obj.error, self.all_objects) + self.all_objects = [obj for obj in self.all_objects if not obj.error] self.no_contents = [ SelectCardsetLeaf(None, None, _("(no cardsets)"), key=None), ] # select_by_type = None diff --git a/pysollib/tile/selecttile.py b/pysollib/tile/selecttile.py index 19e0e82e..9ed8c6f5 100644 --- a/pysollib/tile/selecttile.py +++ b/pysollib/tile/selecttile.py @@ -74,8 +74,8 @@ class SelectTileData(SelectDialogTreeData): def __init__(self, manager, key): SelectDialogTreeData.__init__(self) self.all_objects = manager.getAllSortedByName() - self.all_objects = filter(lambda obj: not obj.error, self.all_objects) - self.all_objects = filter(lambda tile: tile.index > 0 and tile.filename, self.all_objects) + self.all_objects = [obj for obj in self.all_objects if not obj.error] + self.all_objects = [tile for tile in self.all_objects if tile.index > 0 and tile.filename] self.no_contents = [ SelectTileLeaf(None, None, _("(no tiles)"), key=None), ] e1 = isinstance(key, str) or len(self.all_objects) <=17 e2 = 1 diff --git a/pysollib/tk/selectcardset.py b/pysollib/tk/selectcardset.py index 81e917b6..b0cf3320 100644 --- a/pysollib/tk/selectcardset.py +++ b/pysollib/tk/selectcardset.py @@ -78,7 +78,7 @@ class SelectCardsetData(SelectDialogTreeData): def __init__(self, manager, key): SelectDialogTreeData.__init__(self) self.all_objects = manager.getAllSortedByName() - self.all_objects = filter(lambda obj: not obj.error, self.all_objects) + self.all_objects = [obj for obj in self.all_objects if not obj.error] self.no_contents = [ SelectCardsetLeaf(None, None, _("(no cardsets)"), key=None), ] # select_by_type = None diff --git a/pysollib/tk/selecttile.py b/pysollib/tk/selecttile.py index 5908aa9d..ec3846b6 100644 --- a/pysollib/tk/selecttile.py +++ b/pysollib/tk/selecttile.py @@ -72,8 +72,8 @@ class SelectTileData(SelectDialogTreeData): def __init__(self, manager, key): SelectDialogTreeData.__init__(self) self.all_objects = manager.getAllSortedByName() - self.all_objects = filter(lambda obj: not obj.error, self.all_objects) - self.all_objects = filter(lambda tile: tile.index > 0 and tile.filename, self.all_objects) + self.all_objects = [obj for obj in self.all_objects if not obj.error] + self.all_objects = [tile for tile in self.all_objects if tile.index > 0 and tile.filename] self.no_contents = [ SelectTileLeaf(None, None, _("(no tiles)"), key=None), ] e1 = isinstance(key, str) or len(self.all_objects) <=17 e2 = 1