From b6da50d20f20b42b01c99d6fcd4512755e3076e5 Mon Sep 17 00:00:00 2001 From: Shlomi Fish Date: Fri, 31 May 2019 19:23:27 +0300 Subject: [PATCH] Extract a function or class to step away from God Object. See: * https://en.wikipedia.org/wiki/God_object * https://www.c-sharpcorner.com/article/god-object-a-code-smell/ . This is Refactoring / code cleanup. See: * https://refactoring.com/catalog/extractMethod.html * https://en.wikipedia.org/wiki/Code_refactoring * https://www.refactoring.com/ * https://www.joelonsoftware.com/2002/01/23/rub-a-dub-dub/ Some small optimisations may have slipped in as well. --- pysollib/game.py | 164 ++++++++++++++++++++++++----------------------- 1 file changed, 84 insertions(+), 80 deletions(-) diff --git a/pysollib/game.py b/pysollib/game.py index 753a6783a5..f5e9d90dae 100644 --- a/pysollib/game.py +++ b/pysollib/game.py @@ -84,6 +84,89 @@ # ************************************************************************ +def _updateStatus_process_key_val(tb, sb, k, v): + if k == "gamenumber": + if v is None: + if sb: + sb.updateText(gamenumber="") + # self.top.wm_title("%s - %s" + # % (TITLE, self.getTitleName())) + return + if isinstance(v, six.string_types): + if sb: + sb.updateText(gamenumber=v) + # self.top.wm_title("%s - %s %s" % (TITLE, + # self.getTitleName(), v)) + return + if k == "info": + # print 'updateStatus info:', v + if v is None: + if sb: + sb.updateText(info="") + return + if isinstance(v, str): + if sb: + sb.updateText(info=v) + return + if k == "moves": + if v is None: + # if tb: tb.updateText(moves="Moves\n") + if sb: + sb.updateText(moves="") + return + if isinstance(v, tuple): + # if tb: tb.updateText(moves="Moves\n%d/%d" % v) + if sb: + sb.updateText(moves="%d/%d" % v) + return + if isinstance(v, int): + # if tb: tb.updateText(moves="Moves\n%d" % v) + if sb: + sb.updateText(moves="%d" % v) + return + if isinstance(v, str): + # if tb: tb.updateText(moves=v) + if sb: + sb.updateText(moves=v) + return + if k == "player": + if v is None: + if tb: + tb.updateText(player=_("Player\n")) + return + if isinstance(v, six.string_types): + if tb: + # if self.app.opt.toolbar_size: + if tb.getSize(): + tb.updateText(player=_("Player\n") + v) + else: + tb.updateText(player=v) + return + if k == "stats": + if v is None: + if sb: + sb.updateText(stats="") + return + if isinstance(v, tuple): + t = "%d: %d/%d" % (v[0]+v[1], v[0], v[1]) + if sb: + sb.updateText(stats=t) + return + if k == "time": + if v is None: + if sb: + sb.updateText(time='') + if isinstance(v, six.string_types): + if sb: + sb.updateText(time=v) + return + if k == 'stuck': + if sb: + sb.updateText(stuck=v) + return + raise AttributeError(k) + + class Game(object): # for self.gstats.updated U_PLAY = 0 @@ -1036,86 +1119,7 @@ def updateStatus(self, **kw): return tb, sb = self.app.toolbar, self.app.statusbar for k, v in kw.items(): - if k == "gamenumber": - if v is None: - if sb: - sb.updateText(gamenumber="") - # self.top.wm_title("%s - %s" - # % (TITLE, self.getTitleName())) - continue - if isinstance(v, six.string_types): - if sb: - sb.updateText(gamenumber=v) - # self.top.wm_title("%s - %s %s" % (TITLE, - # self.getTitleName(), v)) - continue - if k == "info": - # print 'updateStatus info:', v - if v is None: - if sb: - sb.updateText(info="") - continue - if isinstance(v, str): - if sb: - sb.updateText(info=v) - continue - if k == "moves": - if v is None: - # if tb: tb.updateText(moves="Moves\n") - if sb: - sb.updateText(moves="") - continue - if isinstance(v, tuple): - # if tb: tb.updateText(moves="Moves\n%d/%d" % v) - if sb: - sb.updateText(moves="%d/%d" % v) - continue - if isinstance(v, int): - # if tb: tb.updateText(moves="Moves\n%d" % v) - if sb: - sb.updateText(moves="%d" % v) - continue - if isinstance(v, str): - # if tb: tb.updateText(moves=v) - if sb: - sb.updateText(moves=v) - continue - if k == "player": - if v is None: - if tb: - tb.updateText(player=_("Player\n")) - continue - if isinstance(v, six.string_types): - if tb: - # if self.app.opt.toolbar_size: - if self.app.toolbar.getSize(): - tb.updateText(player=_("Player\n") + v) - else: - tb.updateText(player=v) - continue - if k == "stats": - if v is None: - if sb: - sb.updateText(stats="") - continue - if isinstance(v, tuple): - t = "%d: %d/%d" % (v[0]+v[1], v[0], v[1]) - if sb: - sb.updateText(stats=t) - continue - if k == "time": - if v is None: - if sb: - sb.updateText(time='') - if isinstance(v, six.string_types): - if sb: - sb.updateText(time=v) - continue - if k == 'stuck': - if sb: - sb.updateText(stuck=v) - continue - raise AttributeError(k) + _updateStatus_process_key_val(tb, sb, k, v) def _unmapHandler(self, event): # pause game if root window has been iconified