[Sugar-devel] [PATCH Finance] pep8ized

Manuel Kaufmann humitos at gmail.com
Tue Jul 3 08:06:52 EDT 2012


Fixed a lot of code style related with PEP8

Signed-off-by: Manuel Kaufmann <humitos at gmail.com>
---
 budgetscreen.py   |   81 +++++++------
 chartscreen.py    |   56 ++++-----
 finance.py        |  341 ++++++++++++++++++++++++++++++++---------------------
 registerscreen.py |   76 +++++++-----
 setup.py          |    2 +
 5 files changed, 328 insertions(+), 228 deletions(-)

diff --git a/budgetscreen.py b/budgetscreen.py
index c38617e..4115d79 100644
--- a/budgetscreen.py
+++ b/budgetscreen.py
@@ -1,38 +1,40 @@
-# Copyright 2008 by Wade Brainerd.  
+# Copyright 2008 by Wade Brainerd.
 # This file is part of Finance.
 #
 # Finance is free software: you can redistribute it and/or modify
 # it under the terms of the GNU General Public License as published by
 # the Free Software Foundation, either version 3 of the License, or
 # (at your option) any later version.
-# 
+#
 # Finance is distributed in the hope that it will be useful,
 # but WITHOUT ANY WARRANTY; without even the implied warranty of
 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 # GNU General Public License for more details.
-# 
+#
 # You should have received a copy of the GNU General Public License
 # along with Finance.  If not, see <http://www.gnu.org/licenses/>.
 
 # Import standard Python modules.
-import logging, os, math, time, copy, json, time, datetime, locale
-from gettext import gettext as _
+import datetime
+import locale
 
-# Set up localization.
-locale.setlocale(locale.LC_ALL, '')
+from gettext import gettext as _
 
 from gi.repository import Gtk
 from gi.repository import Gdk
 from gi.repository import GObject
 
-# Import Sugar UI modules.
-import sugar3.activity.activity
-from sugar3.graphics import *
+# Set up localization.
+locale.setlocale(locale.LC_ALL, '')
 
 # Import activity module
 import finance
 
-BUDGET_HELP = _('The Budget view allows you to set a monthly budget for each expense category, and to keep track of your\nbudgets.  To set a budget, type the amount in the box to the right of the category.')
+BUDGET_HELP = _(
+    'The Budget view allows you to set a monthly budget for each expense '
+    'category, and to keep track of your\nbudgets. To set a budget, type '
+    'the amount in the box to the right of the category.')
+
 
 class BudgetScreen(Gtk.VBox):
     def __init__(self, activity):
@@ -42,7 +44,7 @@ class BudgetScreen(Gtk.VBox):
 
         self.category_total = {}
         self.sorted_categories = []
- 
+
         self.budgetbox = Gtk.VBox()
 
         scroll = Gtk.ScrolledWindow()
@@ -57,12 +59,12 @@ class BudgetScreen(Gtk.VBox):
         for t in self.activity.visible_transactions:
             cat = t['category']
             amount = t['amount']
-            
+
             if t['type'] == 'debit':
-                if not self.category_total.has_key(cat):
+                if not cat in self.category_total:
                     self.category_total[cat] = amount
-                else: 
-                    self.category_total[cat] += amount 
+                else:
+                    self.category_total[cat] += amount
 
         # Generate a list of names sorted by total.
         self.sorted_categories = self.category_total.keys()
@@ -74,12 +76,12 @@ class BudgetScreen(Gtk.VBox):
 
         # Build header.
         catlabel = Gtk.Label()
-        catlabel.set_markup('<b><big>'+_('Category')+'</big></b>')
+        catlabel.set_markup('<b><big>' + _('Category') + '</big></b>')
         spentlabel = Gtk.Label()
-        spentlabel.set_markup('<b><big>'+_('Spent')+'</big></b>')
+        spentlabel.set_markup('<b><big>' + _('Spent') + '</big></b>')
         budgetlabel = Gtk.Label()
-        budgetlabel.set_markup('<b><big>'+_('Budget')+'</big></b>')
-        
+        budgetlabel.set_markup('<b><big>' + _('Budget') + '</big></b>')
+
         headerbox = Gtk.HBox()
         headerbox.pack_start(catlabel, False, True, 20)
         headerbox.pack_start(spentlabel, True, True, 10)
@@ -94,7 +96,7 @@ class BudgetScreen(Gtk.VBox):
 
         budgetgroup = Gtk.SizeGroup(Gtk.SizeGroupMode.HORIZONTAL)
         budgetgroup.add_widget(budgetlabel)
-        
+
         # Build categories.
         for c in self.sorted_categories:
             description = c
@@ -120,7 +122,7 @@ class BudgetScreen(Gtk.VBox):
             budgetentry = Gtk.Entry()
             budgetentry.connect('changed', self.budget_changed_cb, c)
             budgetentry.set_width_chars(10)
-            if self.activity.data['budgets'].has_key(c):
+            if c in self.activity.data['budgets']:
                 b = self.activity.data['budgets'][c]
                 budgetentry.set_text(locale.currency(b['amount'], False))
             budgetgroup.add_widget(budgetentry)
@@ -141,7 +143,7 @@ class BudgetScreen(Gtk.VBox):
             self.budgetbox.pack_start(hbox, False, False, 5)
 
         self.show_all()
-            
+
         self.activity.set_help(BUDGET_HELP)
 
     def bar_draw_cb(self, widget, cr, category):
@@ -149,13 +151,18 @@ class BudgetScreen(Gtk.VBox):
 
         # Draw amount of time spent in period if sensible.
         period_ratio = None
-        if self.activity.period != _('Day') and self.activity.period != _('Forever'):
-            period_length = (self.activity.get_next_period(self.activity.period_start) - self.activity.period_start).days
-            period_ratio = float((datetime.date.today() - self.activity.period_start).days) / period_length
+        if self.activity.period != _('Day') and \
+                self.activity.period != _('Forever'):
+            period_length = (
+                self.activity.get_next_period(self.activity.period_start) - \
+                    self.activity.period_start).days
+            period_ratio = float(
+                (datetime.date.today() - self.activity.period_start).days) / \
+                period_length
 
             if period_ratio > 0:
                 cr.set_source_rgb(0.9, 0.9, 0.9)
-                cr.rectangle(0, 0, bounds.width*period_ratio, bounds.height)
+                cr.rectangle(0, 0, bounds.width * period_ratio, bounds.height)
                 cr.fill()
 
         # Draw outline.
@@ -166,7 +173,7 @@ class BudgetScreen(Gtk.VBox):
         # Draw arrow and cost.
         total = self.category_total[category]
 
-        if self.activity.data['budgets'].has_key(category):
+        if category in self.activity.data['budgets']:
             budget = self.activity.data['budgets'][category]['amount']
 
             # Convert from monthly budget.
@@ -182,10 +189,10 @@ class BudgetScreen(Gtk.VBox):
             ratio = total / budget
 
             cr.move_to(5, 5)
-            cr.line_to(ratio * (bounds.width-30), 5)
-            cr.line_to(ratio * (bounds.width-5), bounds.height/2)
-            cr.line_to(ratio * (bounds.width-30), bounds.height-5)
-            cr.line_to(5, bounds.height-5)
+            cr.line_to(ratio * (bounds.width - 30), 5)
+            cr.line_to(ratio * (bounds.width - 5), bounds.height / 2)
+            cr.line_to(ratio * (bounds.width - 30), bounds.height - 5)
+            cr.line_to(5, bounds.height - 5)
             cr.close_path()
 
             if ratio > 1.0:
@@ -195,7 +202,7 @@ class BudgetScreen(Gtk.VBox):
             else:
                 cr.set_source_rgb(0.6, 1.0, 0.6)
             cr.fill_preserve()
-            
+
             cr.set_source_rgb(0.5, 0.5, 0.5)
             cr.stroke()
 
@@ -205,14 +212,14 @@ class BudgetScreen(Gtk.VBox):
 
         cr.set_font_size(20)
         x_bearing, y_bearing, width, height = cr.text_extents(text)[:4]
-    
-        cr.move_to(20, (bounds.height-height)/2 - y_bearing)
+
+        cr.move_to(20, (bounds.height - height) / 2 - y_bearing)
         cr.show_text(text)
 
     def budget_changed_cb(self, widget, category):
         text = widget.get_text()
         if text != '':
-            self.activity.data['budgets'][category] = { 'amount': float(text) }
+            self.activity.data['budgets'][category] = {'amount': float(text)}
         else:
-            del self.activity.data['budgets'][category] 
+            del self.activity.data['budgets'][category]
         self.queue_draw()
diff --git a/chartscreen.py b/chartscreen.py
index cf1a412..3538836 100644
--- a/chartscreen.py
+++ b/chartscreen.py
@@ -1,38 +1,39 @@
-# Copyright 2008 by Wade Brainerd.  
+# Copyright 2008 by Wade Brainerd.
 # This file is part of Finance.
 #
 # Finance is free software: you can redistribute it and/or modify
 # it under the terms of the GNU General Public License as published by
 # the Free Software Foundation, either version 3 of the License, or
 # (at your option) any later version.
-# 
+#
 # Finance is distributed in the hope that it will be useful,
 # but WITHOUT ANY WARRANTY; without even the implied warranty of
 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 # GNU General Public License for more details.
-# 
+#
 # You should have received a copy of the GNU General Public License
 # along with Finance.  If not, see <http://www.gnu.org/licenses/>.
 
 # Import standard Python modules.
-import logging, os, math, time, copy, json, time, datetime, locale
-from gettext import gettext as _
+import math
+import locale
 
-# Set up localization.
-locale.setlocale(locale.LC_ALL, '')
+# Import activity module
+import finance
+
+from gettext import gettext as _
 
 from gi.repository import Gtk
 from gi.repository import Gdk
 from gi.repository import GObject
 
-# Import Sugar UI modules.
-import sugar3.activity.activity
-from sugar3.graphics import *
+# Set up localization.
+locale.setlocale(locale.LC_ALL, '')
 
-# Import activity module
-import finance
+CHART_HELP = _(
+    'The Chart view shows the proportion of your expenses that is in each '
+    'category.\nYou can categorize transactions in the Register view.')
 
-CHART_HELP = _('The Chart view shows the proportion of your expenses that is in each category.\nYou can categorize transactions in the Register view.')
 
 class ChartScreen(Gtk.HBox):
     def __init__(self, activity):
@@ -47,7 +48,7 @@ class ChartScreen(Gtk.HBox):
         self.area.connect('draw', self.chart_draw_cb)
 
         label = Gtk.Label()
-        label.set_markup('<b>'+_('Debit Categories')+'</b>')
+        label.set_markup('<b>' + _('Debit Categories') + '</b>')
 
         self.catbox = Gtk.VBox()
 
@@ -60,28 +61,29 @@ class ChartScreen(Gtk.HBox):
         box.pack_start(self.catbox, False, False, 10)
         box.pack_start(Gtk.Box(orientation=Gtk.Orientation.VERTICAL),
                        True, True, 0)
- 
+
         self.pack_start(self.area, True, True, 0)
         self.pack_start(box, False, False, 40)
 
         self.show_all()
-       
+
     def build(self):
         # Build the category totals.
         self.category_total = {}
         for t in self.activity.visible_transactions:
             cat = t['category']
             amount = t['amount']
-            
+
             if t['type'] == 'debit':
-                if not self.category_total.has_key(cat):
+                if not cat self.category_total:
                     self.category_total[cat] = amount
-                else: 
-                    self.category_total[cat] += amount 
+                else:
+                    self.category_total[cat] += amount
 
         # Generate a list of names sorted by total.
         self.sorted_categories = self.category_total.keys()
-        #self.sorted_categories.sort(lamba a, b: cmp(self.category_total[a], self.category_total[b]))
+        # self.sorted_categories.sort(lamba a, b: cmp(self.category_total[a],
+        #                                             self.category_total[b]))
 
         # Clear and rebuild the labels box.
         for w in self.catbox.get_children():
@@ -89,7 +91,7 @@ class ChartScreen(Gtk.HBox):
 
         catgroup = Gtk.SizeGroup(Gtk.SizeGroupMode.HORIZONTAL)
         amountgroup = Gtk.SizeGroup(Gtk.SizeGroupMode.HORIZONTAL)
-        
+
         for c in self.sorted_categories:
             hbox = Gtk.HBox()
 
@@ -126,9 +128,9 @@ class ChartScreen(Gtk.HBox):
         # Draw pie chart.
         bounds = widget.get_allocation()
 
-        x = bounds.width/2
-        y = bounds.height/2
-        r = min(bounds.width, bounds.height)/2 - 10 
+        x = bounds.width / 2
+        y = bounds.height / 2
+        r = min(bounds.width, bounds.height) / 2 - 10
 
         total = 0
         for c in self.sorted_categories:
@@ -138,9 +140,9 @@ class ChartScreen(Gtk.HBox):
             angle = 0.0
 
             for c in self.sorted_categories:
-                slice = 2*math.pi * self.category_total[c] / total
+                slice = 2 * math.pi * self.category_total[c] / total
                 color = finance.get_category_color(c)
- 
+
                 context.move_to(x, y)
                 context.arc(x, y, r, angle, angle + slice)
                 context.close_path()
diff --git a/finance.py b/finance.py
index 3720873..c2c724f 100644
--- a/finance.py
+++ b/finance.py
@@ -1,98 +1,108 @@
-# Copyright 2008 by Wade Brainerd.  
+# Copyright 2008 by Wade Brainerd.
 # This file is part of Finance.
 #
 # Finance is free software: you can redistribute it and/or modify
 # it under the terms of the GNU General Public License as published by
 # the Free Software Foundation, either version 3 of the License, or
 # (at your option) any later version.
-# 
+#
 # Finance is distributed in the hope that it will be useful,
 # but WITHOUT ANY WARRANTY; without even the implied warranty of
 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 # GNU General Public License for more details.
-# 
+#
 # You should have received a copy of the GNU General Public License
 # along with Finance.  If not, see <http://www.gnu.org/licenses/>.
 #!/usr/bin/env python
+
 """Finance - Home financial software for the OLPC XO."""
 
 # Import standard Python modules.
-import logging, os, math, time, copy, time, datetime, locale
-from gettext import gettext as _
+import logging
+import datetime
+import locale
 
-from port import json
+# Import screen classes.
+import registerscreen
+import chartscreen
+import budgetscreen
 
-# Set up localization.
-locale.setlocale(locale.LC_ALL, '')
+from gettext import gettext as _
+from port import json
 
 from gi.repository import Gtk
 from gi.repository import Gdk
 
 # Import Sugar UI modules.
-import sugar3.activity.activity
 from sugar3.graphics.toggletoolbutton import ToggleToolButton
 from sugar3.graphics.toolbutton import ToolButton
 from sugar3.graphics.toolcombobox import ToolComboBox
-from sugar3.graphics import *
 
 from sugar3.graphics.toolbarbox import ToolbarBox
 from sugar3.graphics.toolbarbox import ToolbarButton
 from sugar3.graphics.radiotoolbutton import RadioToolButton
 from sugar3.activity.widgets import StopButton
 from sugar3.activity.widgets import ActivityToolbarButton
+from sugar3.activity import Activity
+
+# Set up localization.
+locale.setlocale(locale.LC_ALL, '')
 
 # Initialize logging.
 log = logging.getLogger('Finance')
 log.setLevel(logging.DEBUG)
 logging.basicConfig()
 
-# Import screen classes.
-import registerscreen, chartscreen, budgetscreen
 
 CATEGORY_COLORS = [
-    (1.0,1.0,1.0),
-    (1.0,1.0,0.6),
-    (1.0,1.0,0.8),
-    (1.0,0.6,1.0),
-    (1.0,0.6,0.6),
-    (1.0,0.6,0.8),
-    (1.0,0.8,1.0),
-    (1.0,0.8,0.6),
-    (1.0,0.8,0.8),
-    (0.6,1.0,1.0),
-    (0.6,1.0,0.6),
-    (0.6,1.0,0.8),
-    (0.6,0.6,1.0),
-    (0.6,0.6,0.6),
-    (0.6,0.6,0.8),
-    (0.6,0.8,1.0),
-    (0.6,0.8,0.6),
-    (0.6,0.8,0.8),
-    (0.8,1.0,1.0),
-    (0.8,1.0,0.6),
-    (0.8,1.0,0.8),
-    (0.8,0.6,1.0),
-    (0.8,0.6,0.6),
-    (0.8,0.6,0.8),
-    (0.8,0.8,1.0),
-    (0.8,0.8,0.6),
-    (0.8,0.8,0.8),
+    (1.0, 1.0, 1.0),
+    (1.0, 1.0, 0.6),
+    (1.0, 1.0, 0.8),
+    (1.0, 0.6, 1.0),
+    (1.0, 0.6, 0.6),
+    (1.0, 0.6, 0.8),
+    (1.0, 0.8, 1.0),
+    (1.0, 0.8, 0.6),
+    (1.0, 0.8, 0.8),
+    (0.6, 1.0, 1.0),
+    (0.6, 1.0, 0.6),
+    (0.6, 1.0, 0.8),
+    (0.6, 0.6, 1.0),
+    (0.6, 0.6, 0.6),
+    (0.6, 0.6, 0.8),
+    (0.6, 0.8, 1.0),
+    (0.6, 0.8, 0.6),
+    (0.6, 0.8, 0.8),
+    (0.8, 1.0, 1.0),
+    (0.8, 1.0, 0.6),
+    (0.8, 1.0, 0.8),
+    (0.8, 0.6, 1.0),
+    (0.8, 0.6, 0.6),
+    (0.8, 0.6, 0.8),
+    (0.8, 0.8, 1.0),
+    (0.8, 0.8, 0.6),
+    (0.8, 0.8, 0.8),
 ]
 
+
 def get_category_color(catname):
     return CATEGORY_COLORS[catname.__hash__() % len(CATEGORY_COLORS)]
 
+
 def get_category_color_str(catname):
     color = get_category_color(catname)
-    return "#%02x%02x%02x" % (int(color[0]*255), int(color[1]*255), int(color[2]*255))
+    return "#%02x%02x%02x" % \
+        (int(color[0] * 255), int(color[1] * 255), int(color[2] * 255))
+
 
 # This is the main Finance activity class.
-# 
-# It owns the main application window, and all the various toolbars and options.
-# Screens are stored in a stack, with the currently active screen on top.
-class Finance(sugar3.activity.activity.Activity):
-    def __init__ (self, handle):
-        sugar3.activity.activity.Activity.__init__(self, handle)
+#
+# It owns the main application window, and all the various toolbars
+# and options. Screens are stored in a stack, with the currently
+# active screen on top.
+class Finance(Activity):
+    def __init__(self, handle):
+        Activity.__init__(self, handle)
         self.set_title(_("Finance"))
         self.max_participants = 1
 
@@ -101,12 +111,12 @@ class Finance(sugar3.activity.activity.Activity):
         #   next_id
         #   transactions
         #     id, name, type, amount, date, category
-        #   budgets 
+        #   budgets
         #     category, period, amount, budget
         self.data = {
             'next_id': 0,
             'transactions': [],
-            'budgets': {} 
+            'budgets': {}
         }
 
         self.transaction_map = {}
@@ -116,7 +126,7 @@ class Finance(sugar3.activity.activity.Activity):
         self.category_names = {}
 
         #self.create_test_data()
-  
+
         # Initialize view period to the first of the month.
         self.period = _('Month')
         self.period_start = self.get_this_period()
@@ -127,7 +137,7 @@ class Finance(sugar3.activity.activity.Activity):
         self.budget = budgetscreen.BudgetScreen(self)
 
         self.build_toolbox()
-  
+
         self.screens = []
         self.screenbox = Gtk.VBox()
 
@@ -170,7 +180,8 @@ class Finance(sugar3.activity.activity.Activity):
         # Start with the main screen.
         self.push_screen(self.register)
 
-        # This has to happen last, because it calls the read_file method when restoring from the Journal.
+        # This has to happen last, because it calls the read_file
+        # method when restoring from the Journal.
         self.set_canvas(vbox)
 
         self.show_all()
@@ -195,7 +206,7 @@ class Finance(sugar3.activity.activity.Activity):
         transactionbar.insert(self.newcreditbtn, -1)
         transactionbar.insert(self.newdebitbtn, -1)
         transactionbar.insert(self.eraseitembtn, -1)
- 
+
         self.thisperiodbtn = ToolButton('go-down')
         self.thisperiodbtn.props.accelerator = '<Ctrl>Down'
         self.thisperiodbtn.connect('clicked', self.thisperiod_cb)
@@ -331,7 +342,7 @@ class Finance(sugar3.activity.activity.Activity):
     def push_screen(self, screen):
         if len(self.screens):
             self.screenbox.remove(self.screens[-1])
- 
+
         self.screenbox.pack_start(screen, True, True, 0)
         self.screens.append(screen)
 
@@ -373,7 +384,8 @@ class Finance(sugar3.activity.activity.Activity):
         elif self.period == _('Forever'):
             text = _('Forever')
 
-        self.periodlabel.set_markup("<span size='xx-large'><b>" + text + "</b></span>")
+        self.periodlabel.set_markup(
+            "<span size='xx-large'><b>" + text + "</b></span>")
 
     def update_summary(self):
         # Calculate starting balance.
@@ -382,16 +394,16 @@ class Finance(sugar3.activity.activity.Activity):
             d = t['date']
             if d < self.period_start.toordinal():
                 if t['type'] == 'credit':
-                    start += t['amount'] 
+                    start += t['amount']
                 else:
-                    start -= t['amount'] 
+                    start -= t['amount']
 
         # Calculate totals for this period.
         credit_count = 0
         credit_total = 0.0
         debit_count = 0
         debit_total = 0.0
-        total = start 
+        total = start
         for t in self.visible_transactions:
             if t['type'] == 'credit':
                 credit_count += 1
@@ -407,14 +419,17 @@ class Finance(sugar3.activity.activity.Activity):
             balancecolor = '#4040ff'
         else:
             balancecolor = '#ff4040'
-        balance = "<span size='xx-large' foreground='%s'><b>" % balancecolor 
+        balance = "<span size='xx-large' foreground='%s'><b>" % balancecolor
         balance += _('Balance: ') + locale.currency(total)
         balance += "</b></span>"
         self.balancelabel.set_markup(balance)
 
-        self.startlabel.set_markup('Starting Balance: ' + locale.currency(start))
-        self.creditslabel.set_markup('%s in %d credits' % (locale.currency(credit_total), credit_count))
-        self.debitslabel.set_markup('%s in %d debits' % (locale.currency(debit_total), debit_count))
+        self.startlabel.set_markup('Starting Balance: ' +
+                                   locale.currency(start))
+        self.creditslabel.set_markup(
+            '%s in %d credits' % (locale.currency(credit_total), credit_count))
+        self.debitslabel.set_markup(
+            '%s in %d debits' % (locale.currency(debit_total), debit_count))
 
     def update_toolbar(self):
         # Disable the navigation when Forever is selected.
@@ -479,12 +494,12 @@ class Finance(sugar3.activity.activity.Activity):
 
         elif self.period == _('Month'):
             if start.month == 12:
-                return datetime.date(start.year+1, 1, 1)
+                return datetime.date(start.year + 1, 1, 1)
             else:
-                return datetime.date(start.year, start.month+1, 1)
+                return datetime.date(start.year, start.month + 1, 1)
 
         elif self.period == _('Year'):
-            return datetime.date(start.year+1, 1, 1)
+            return datetime.date(start.year + 1, 1, 1)
 
     def get_prev_period(self, start):
         if self.period == _('Day'):
@@ -495,12 +510,12 @@ class Finance(sugar3.activity.activity.Activity):
 
         elif self.period == _('Month'):
             if start.month == 1:
-                return datetime.date(start.year-1, 12, 1)
+                return datetime.date(start.year - 1, 12, 1)
             else:
-                return datetime.date(start.year, start.month-1, 1)
+                return datetime.date(start.year, start.month - 1, 1)
 
         elif self.period == _('Year'):
-            return datetime.date(start.year-1, 1, 1)
+            return datetime.date(start.year - 1, 1, 1)
 
     def thisperiod_cb(self, widget):
         if self.period != _('Forever'):
@@ -531,7 +546,8 @@ class Finance(sugar3.activity.activity.Activity):
 
         else:
             period_start_ord = self.period_start.toordinal()
-            period_end_ord = self.get_next_period(self.period_start).toordinal()
+            period_end_ord = self.get_next_period(
+                self.period_start).toordinal()
 
             self.visible_transactions = []
             for t in self.data['transactions']:
@@ -539,14 +555,15 @@ class Finance(sugar3.activity.activity.Activity):
                 if d >= period_start_ord and d < period_end_ord:
                     self.visible_transactions.append(t)
 
-        self.visible_transactions.sort(lambda a,b: a['date'] - b['date'])
+        self.visible_transactions.sort(lambda a, b: a['date'] - b['date'])
 
     def build_transaction_map(self):
         self.transaction_map = {}
         for t in self.data['transactions']:
             self.transaction_map[t['id']] = t
 
-    def create_transaction(self, name='', type='debit', amount=0, category='', date=datetime.date.today()):
+    def create_transaction(self, name='', type='debit', amount=0,
+                           category='', date=datetime.date.today()):
         id = self.data['next_id']
         self.data['next_id'] += 1
 
@@ -556,13 +573,13 @@ class Finance(sugar3.activity.activity.Activity):
             'type': type,
             'amount': amount,
             'date': date.toordinal(),
-            'category': category 
+            'category': category
         }
         self.data['transactions'].append(t)
         self.transaction_map[id] = t
 
         self.build_visible_transactions()
-        
+
         return id
 
     def destroy_transaction(self, id):
@@ -580,126 +597,186 @@ class Finance(sugar3.activity.activity.Activity):
     def create_test_data(self):
         cur_date = datetime.date.today()
         cur_date = datetime.date(cur_date.year, cur_date.month, 1)
-        self.create_transaction('Initial Balance', type='credit', amount=632, category='Initial Balance', date=cur_date)
+        self.create_transaction('Initial Balance', type='credit', amount=632,
+                                category='Initial Balance', date=cur_date)
 
         cur_date += datetime.timedelta(days=1)
-        self.create_transaction('Fix Car', amount=75.84, category='Transportation', date=cur_date)
+        self.create_transaction('Fix Car', amount=75.84,
+                                category='Transportation', date=cur_date)
 
         cur_date += datetime.timedelta(days=2)
-        self.create_transaction('Adopt Cat', amount=100, category='Pets', date=cur_date)
-        self.create_transaction('New Coat', amount=25.53, category='Clothing', date=cur_date)
+        self.create_transaction('Adopt Cat', amount=100, category='Pets',
+                                date=cur_date)
+        self.create_transaction('New Coat', amount=25.53, category='Clothing',
+                                date=cur_date)
 
         cur_date += datetime.timedelta(days=2)
-        self.create_transaction('Pay Rent', amount=500, category='Housing', date=cur_date)
+        self.create_transaction('Pay Rent', amount=500, category='Housing',
+                                date=cur_date)
 
         cur_date += datetime.timedelta(days=1)
-        self.create_transaction('Funky Cafe', amount=5.20, category='Food', date=cur_date)
-        self.create_transaction('Groceries', amount=50.92, category='Food', date=cur_date)
-        self.create_transaction('Cat Food', amount=5.40, category='Pets', date=cur_date)
+        self.create_transaction('Funky Cafe', amount=5.20, category='Food',
+                                date=cur_date)
+        self.create_transaction('Groceries', amount=50.92, category='Food',
+                                date=cur_date)
+        self.create_transaction('Cat Food', amount=5.40, category='Pets',
+                                date=cur_date)
 
         cur_date += datetime.timedelta(days=4)
-        self.create_transaction('Paycheck', type='credit', amount=700, category='Paycheck', date=cur_date)
-        self.create_transaction('Gas', amount=21.20, category='Transportation', date=cur_date)
+        self.create_transaction('Paycheck', type='credit', amount=700,
+                                category='Paycheck', date=cur_date)
+        self.create_transaction('Gas', amount=21.20, category='Transportation',
+                                date=cur_date)
 
         cur_date += datetime.timedelta(days=2)
-        self.create_transaction('Cat Toys', amount=10.95, category='Pets', date=cur_date)
-        self.create_transaction('Gift for Sister', amount=23.20, category='Gifts', date=cur_date)
+        self.create_transaction('Cat Toys', amount=10.95, category='Pets',
+                                date=cur_date)
+        self.create_transaction('Gift for Sister', amount=23.20,
+                                category='Gifts', date=cur_date)
 
         cur_date += datetime.timedelta(days=2)
-        self.create_transaction('Pay Rent', amount=500, category='Housing', date=cur_date)
+        self.create_transaction('Pay Rent', amount=500, category='Housing',
+                                date=cur_date)
 
         cur_date += datetime.timedelta(days=1)
-        self.create_transaction('Funky Cafe', amount=5.20, category='Food', date=cur_date)
-        self.create_transaction('Groceries', amount=50.92, category='Food', date=cur_date)
-        self.create_transaction('Cat Food', amount=5.40, category='Pets', date=cur_date)
+        self.create_transaction('Funky Cafe', amount=5.20, category='Food',
+                                date=cur_date)
+        self.create_transaction('Groceries', amount=50.92, category='Food',
+                                date=cur_date)
+        self.create_transaction('Cat Food', amount=5.40, category='Pets',
+                                date=cur_date)
 
         cur_date += datetime.timedelta(days=4)
-        self.create_transaction('Paycheck', type='credit', amount=700, category='Paycheck', date=cur_date)
-        self.create_transaction('Gas', amount=21.20, category='Transportation', date=cur_date)
+        self.create_transaction('Paycheck', type='credit', amount=700,
+                                category='Paycheck', date=cur_date)
+        self.create_transaction('Gas', amount=21.20, category='Transportation',
+                                date=cur_date)
 
         cur_date += datetime.timedelta(days=2)
-        self.create_transaction('Cat Toys', amount=10.95, category='Pets', date=cur_date)
-        self.create_transaction('Gift for Sister', amount=23.20, category='Gifts', date=cur_date)
+        self.create_transaction('Cat Toys', amount=10.95, category='Pets',
+                                date=cur_date)
+        self.create_transaction('Gift for Sister', amount=23.20,
+                                category='Gifts', date=cur_date)
 
         cur_date += datetime.timedelta(days=2)
-        self.create_transaction('Pay Rent', amount=500, category='Housing', date=cur_date)
+        self.create_transaction('Pay Rent', amount=500, category='Housing',
+                                date=cur_date)
 
         cur_date += datetime.timedelta(days=1)
-        self.create_transaction('Funky Cafe', amount=5.20, category='Food', date=cur_date)
-        self.create_transaction('Groceries', amount=50.92, category='Food', date=cur_date)
-        self.create_transaction('Cat Food', amount=5.40, category='Pets', date=cur_date)
+        self.create_transaction('Funky Cafe', amount=5.20, category='Food',
+                                date=cur_date)
+        self.create_transaction('Groceries', amount=50.92, category='Food',
+                                date=cur_date)
+        self.create_transaction('Cat Food', amount=5.40, category='Pets',
+                                date=cur_date)
 
         cur_date += datetime.timedelta(days=4)
-        self.create_transaction('Paycheck', type='credit', amount=700, category='Paycheck', date=cur_date)
-        self.create_transaction('Gas', amount=21.20, category='Transportation', date=cur_date)
+        self.create_transaction('Paycheck', type='credit', amount=700,
+                                category='Paycheck', date=cur_date)
+        self.create_transaction('Gas', amount=21.20, category='Transportation',
+                                date=cur_date)
 
         cur_date += datetime.timedelta(days=2)
-        self.create_transaction('Cat Toys', amount=10.95, category='Pets', date=cur_date)
-        self.create_transaction('Gift for Sister', amount=23.20, category='Gifts', date=cur_date)
+        self.create_transaction('Cat Toys', amount=10.95, category='Pets',
+                                date=cur_date)
+        self.create_transaction('Gift for Sister', amount=23.20,
+                                category='Gifts', date=cur_date)
 
         cur_date += datetime.timedelta(days=2)
-        self.create_transaction('Pay Rent', amount=500, category='Housing', date=cur_date)
+        self.create_transaction('Pay Rent', amount=500, category='Housing',
+                                date=cur_date)
 
         cur_date += datetime.timedelta(days=1)
-        self.create_transaction('Funky Cafe', amount=5.20, category='Food', date=cur_date)
-        self.create_transaction('Groceries', amount=50.92, category='Food', date=cur_date)
-        self.create_transaction('Cat Food', amount=5.40, category='Pets', date=cur_date)
+        self.create_transaction('Funky Cafe', amount=5.20, category='Food',
+                                date=cur_date)
+        self.create_transaction('Groceries', amount=50.92, category='Food',
+                                date=cur_date)
+        self.create_transaction('Cat Food', amount=5.40, category='Pets',
+                                date=cur_date)
 
         cur_date += datetime.timedelta(days=4)
-        self.create_transaction('Paycheck', type='credit', amount=700, category='Paycheck', date=cur_date)
-        self.create_transaction('Gas', amount=21.20, category='Transportation', date=cur_date)
+        self.create_transaction('Paycheck', type='credit', amount=700,
+                                category='Paycheck', date=cur_date)
+        self.create_transaction('Gas', amount=21.20, category='Transportation',
+                                date=cur_date)
 
         cur_date += datetime.timedelta(days=2)
-        self.create_transaction('Cat Toys', amount=10.95, category='Pets', date=cur_date)
-        self.create_transaction('Gift for Sister', amount=23.20, category='Gifts', date=cur_date)
+        self.create_transaction('Cat Toys', amount=10.95, category='Pets',
+                                date=cur_date)
+        self.create_transaction('Gift for Sister', amount=23.20,
+                                category='Gifts', date=cur_date)
 
         cur_date += datetime.timedelta(days=2)
-        self.create_transaction('Pay Rent', amount=500, category='Housing', date=cur_date)
+        self.create_transaction('Pay Rent', amount=500, category='Housing',
+                                date=cur_date)
 
         cur_date += datetime.timedelta(days=1)
-        self.create_transaction('Funky Cafe', amount=5.20, category='Food', date=cur_date)
-        self.create_transaction('Groceries', amount=50.92, category='Food', date=cur_date)
-        self.create_transaction('Cat Food', amount=5.40, category='Pets', date=cur_date)
+        self.create_transaction('Funky Cafe', amount=5.20, category='Food',
+                                date=cur_date)
+        self.create_transaction('Groceries', amount=50.92, category='Food',
+                                date=cur_date)
+        self.create_transaction('Cat Food', amount=5.40, category='Pets',
+                                date=cur_date)
 
         cur_date += datetime.timedelta(days=4)
-        self.create_transaction('Paycheck', type='credit', amount=700, category='Paycheck', date=cur_date)
-        self.create_transaction('Gas', amount=21.20, category='Transportation', date=cur_date)
+        self.create_transaction('Paycheck', type='credit', amount=700,
+                                category='Paycheck', date=cur_date)
+        self.create_transaction('Gas', amount=21.20, category='Transportation',
+                                date=cur_date)
 
         cur_date += datetime.timedelta(days=2)
-        self.create_transaction('Cat Toys', amount=10.95, category='Pets', date=cur_date)
-        self.create_transaction('Gift for Sister', amount=23.20, category='Gifts', date=cur_date)
+        self.create_transaction('Cat Toys', amount=10.95, category='Pets',
+                                date=cur_date)
+        self.create_transaction('Gift for Sister', amount=23.20,
+                                category='Gifts', date=cur_date)
 
         cur_date += datetime.timedelta(days=2)
-        self.create_transaction('Pay Rent', amount=500, category='Housing', date=cur_date)
+        self.create_transaction('Pay Rent', amount=500, category='Housing',
+                                date=cur_date)
 
         cur_date += datetime.timedelta(days=1)
-        self.create_transaction('Funky Cafe', amount=5.20, category='Food', date=cur_date)
-        self.create_transaction('Groceries', amount=50.92, category='Food', date=cur_date)
-        self.create_transaction('Cat Food', amount=5.40, category='Pets', date=cur_date)
+        self.create_transaction('Funky Cafe', amount=5.20, category='Food',
+                                date=cur_date)
+        self.create_transaction('Groceries', amount=50.92, category='Food',
+                                date=cur_date)
+        self.create_transaction('Cat Food', amount=5.40, category='Pets',
+                                date=cur_date)
 
         cur_date += datetime.timedelta(days=4)
-        self.create_transaction('Paycheck', type='credit', amount=700, category='Paycheck', date=cur_date)
-        self.create_transaction('Gas', amount=21.20, category='Transportation', date=cur_date)
+        self.create_transaction('Paycheck', type='credit', amount=700,
+                                category='Paycheck', date=cur_date)
+        self.create_transaction('Gas', amount=21.20, category='Transportation',
+                                date=cur_date)
 
         cur_date += datetime.timedelta(days=2)
-        self.create_transaction('Cat Toys', amount=10.95, category='Pets', date=cur_date)
-        self.create_transaction('Gift for Sister', amount=23.20, category='Gifts', date=cur_date)
+        self.create_transaction('Cat Toys', amount=10.95, category='Pets',
+                                date=cur_date)
+        self.create_transaction('Gift for Sister', amount=23.20,
+                                category='Gifts', date=cur_date)
 
         cur_date += datetime.timedelta(days=2)
-        self.create_transaction('Pay Rent', amount=500, category='Housing', date=cur_date)
+        self.create_transaction('Pay Rent', amount=500, category='Housing',
+                                date=cur_date)
 
         cur_date += datetime.timedelta(days=1)
-        self.create_transaction('Funky Cafe', amount=5.20, category='Food', date=cur_date)
-        self.create_transaction('Groceries', amount=50.92, category='Food', date=cur_date)
-        self.create_transaction('Cat Food', amount=5.40, category='Pets', date=cur_date)
+        self.create_transaction('Funky Cafe', amount=5.20, category='Food',
+                                date=cur_date)
+        self.create_transaction('Groceries', amount=50.92, category='Food',
+                                date=cur_date)
+        self.create_transaction('Cat Food', amount=5.40, category='Pets',
+                                date=cur_date)
 
         cur_date += datetime.timedelta(days=4)
-        self.create_transaction('Paycheck', type='credit', amount=700, category='Paycheck', date=cur_date)
-        self.create_transaction('Gas', amount=21.20, category='Transportation', date=cur_date)
+        self.create_transaction('Paycheck', type='credit', amount=700,
+                                category='Paycheck', date=cur_date)
+        self.create_transaction('Gas', amount=21.20, category='Transportation',
+                                date=cur_date)
 
         cur_date += datetime.timedelta(days=2)
-        self.create_transaction('Cat Toys', amount=10.95, category='Pets', date=cur_date)
-        self.create_transaction('Gift for Sister', amount=23.20, category='Gifts', date=cur_date)
+        self.create_transaction('Cat Toys', amount=10.95, category='Pets',
+                                date=cur_date)
+        self.create_transaction('Gift for Sister', amount=23.20,
+                                category='Gifts', date=cur_date)
 
         self.build_transaction_map()
         self.build_names()
diff --git a/registerscreen.py b/registerscreen.py
index 969a474..8bc6053 100644
--- a/registerscreen.py
+++ b/registerscreen.py
@@ -1,37 +1,41 @@
-# Copyright 2008 by Wade Brainerd.  
+# Copyright 2008 by Wade Brainerd.
 # This file is part of Finance.
 #
 # Finance is free software: you can redistribute it and/or modify
 # it under the terms of the GNU General Public License as published by
 # the Free Software Foundation, either version 3 of the License, or
 # (at your option) any later version.
-# 
+#
 # Finance is distributed in the hope that it will be useful,
 # but WITHOUT ANY WARRANTY; without even the implied warranty of
 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 # GNU General Public License for more details.
-# 
+#
 # You should have received a copy of the GNU General Public License
 # along with Finance.  If not, see <http://www.gnu.org/licenses/>.
 
 # Import standard Python modules.
-import logging, os, math, time, copy, time, datetime, locale
+import time
+import datetime
+import locale
 from gettext import gettext as _
 
-# Set up localization.
-locale.setlocale(locale.LC_ALL, '')
-
 from gi.repository import Gtk
 from gi.repository import GObject
 
-# Import Sugar UI modules.
-import sugar3.activity.activity
-from sugar3.graphics import *
+# Set up localization.
+locale.setlocale(locale.LC_ALL, '')
 
 # Import activity module
 import finance
 
-REGISTER_HELP = _('<b>Welcome to Finance!</b>   This activity keeps track of income and expenses for anything that earns\nor spends money, like a school club.  To get started, use the Transaction box to add credits and debits.\nOnce you have entered some transactions, visit the Chart and Budget views to see more.')
+REGISTER_HELP = _(
+    '<b>Welcome to Finance!</b>   This activity keeps track of income '
+    'and expenses for anything that earns\nor spends money, like a school '
+    'club.  To get started, use the Transaction box to add credits and '
+    'debits.\nOnce you have entered some transactions, visit the Chart '
+    'and Budget views to see more.')
+
 
 class RegisterScreen(Gtk.VBox):
     def __init__(self, activity):
@@ -44,18 +48,20 @@ class RegisterScreen(Gtk.VBox):
         self.treeview.set_rules_hint(True)
         self.treeview.set_enable_search(False)
 
-        # Note that the only thing we store in our liststore is the transaction id.
-        # All the actual data is in the activity database.
+        # Note that the only thing we store in our liststore is the
+        # transaction id. All the actual data is in the activity
+        # database.
         self.liststore = Gtk.ListStore(GObject.TYPE_INT)
         self.treeview.set_model(self.liststore)
 
         # Construct the columns.
         renderer = Gtk.CellRendererText()
         renderer.props.editable = True
-        renderer.connect('editing-started', self.description_editing_started_cb)
+        renderer.connect('editing-started',
+                         self.description_editing_started_cb)
         renderer.connect('edited', self.description_edit_cb)
         col = Gtk.TreeViewColumn(_('Description'), renderer)
-        col.set_cell_data_func(renderer, self.description_render_cb) 
+        col.set_cell_data_func(renderer, self.description_render_cb)
         col.set_expand(True)
         self.treeview.append_column(col)
 
@@ -63,7 +69,7 @@ class RegisterScreen(Gtk.VBox):
         renderer.props.editable = True
         renderer.connect('edited', self.amount_edit_cb)
         col = Gtk.TreeViewColumn(_('Amount'), renderer)
-        col.set_cell_data_func(renderer, self.amount_render_cb) 
+        col.set_cell_data_func(renderer, self.amount_render_cb)
         col.set_alignment(0.5)
         col.set_min_width(120)
         self.treeview.append_column(col)
@@ -73,7 +79,7 @@ class RegisterScreen(Gtk.VBox):
         renderer.connect('edited', self.date_edit_cb)
         col = Gtk.TreeViewColumn(_('Date'), renderer)
         col.set_alignment(0.5)
-        col.set_cell_data_func(renderer, self.date_render_cb) 
+        col.set_cell_data_func(renderer, self.date_render_cb)
         col.set_min_width(150)
         self.treeview.append_column(col)
 
@@ -82,7 +88,7 @@ class RegisterScreen(Gtk.VBox):
         renderer.connect('editing-started', self.category_editing_started_cb)
         renderer.connect('edited', self.category_edit_cb)
         col = Gtk.TreeViewColumn(_('Category'), renderer)
-        col.set_cell_data_func(renderer, self.category_render_cb) 
+        col.set_cell_data_func(renderer, self.category_render_cb)
         col.set_alignment(0.5)
         col.set_min_width(300)
         self.treeview.append_column(col)
@@ -124,8 +130,9 @@ class RegisterScreen(Gtk.VBox):
         t = self.activity.transaction_map[id]
         t['name'] = new_text
 
-        # Automatically fill in category if empty, and if transaction name is known.
-        if t['category'] == '' and self.activity.transaction_names.has_key(new_text):
+        # Automatically fill in category if empty, and if transaction
+        # name is known.
+        if t['category'] == '' and new_text in self.activity.transaction_names:
             for ct in self.activity.data['transactions']:
                 if ct['name'] == new_text and ct['category'] != '':
                     t['category'] = ct['category']
@@ -136,10 +143,12 @@ class RegisterScreen(Gtk.VBox):
         cell_renderer.set_property('xalign', 1.0)
         if t['type'] == 'credit':
             cell_renderer.set_property('foreground', '#4040ff')
-            cell_renderer.set_property('text', locale.currency(t['amount'], False))
+            cell_renderer.set_property('text',
+                                       locale.currency(t['amount'], False))
         else:
             cell_renderer.set_property('foreground', '#ff4040')
-            cell_renderer.set_property('text', locale.currency(-t['amount'], False))
+            cell_renderer.set_property('text',
+                                       locale.currency(-t['amount'], False))
 
     def amount_edit_cb(self, cell_renderer, path, new_text):
         id = self.liststore[path][0]
@@ -166,7 +175,8 @@ class RegisterScreen(Gtk.VBox):
         id = model.get_value(iter, 0)
         t = self.activity.transaction_map[id]
         cell_renderer.set_property('text', t['category'])
-        cell_renderer.set_property('background', finance.get_category_color_str(t['category']))
+        cell_renderer.set_property(
+            'background', finance.get_category_color_str(t['category']))
 
     def category_editing_started_cb(self, cell_renderer, editable, path):
         completion = Gtk.EntryCompletion()
@@ -185,7 +195,7 @@ class RegisterScreen(Gtk.VBox):
         t = self.activity.transaction_map[id]
         t['category'] = new_text
         if new_text != '':
-            self.activity.category_names[new_text] = 1 
+            self.activity.category_names[new_text] = 1
 
     def newcredit_cb(self, widget):
         # Automatically display the register screen.
@@ -196,8 +206,9 @@ class RegisterScreen(Gtk.VBox):
         id = self.activity.create_transaction(_('New Credit'), 'credit', 0)
         iter = self.liststore.append((id,))
         # Set cursor and begin editing the description.
-        self.treeview.set_cursor(self.liststore.get_path(iter), self.treeview.get_column(0), True)
-        
+        self.treeview.set_cursor(self.liststore.get_path(iter),
+                                 self.treeview.get_column(0), True)
+
     def newdebit_cb(self, widget):
         # Automatically display the register screen.
         if self.activity.screens[-1] != self.activity.register:
@@ -207,10 +218,11 @@ class RegisterScreen(Gtk.VBox):
         id = self.activity.create_transaction(_('New Debit'), 'debit', 0)
         iter = self.liststore.append((id,))
         # Set cursor and begin editing the description.
-        self.treeview.set_cursor(self.liststore.get_path(iter), self.treeview.get_column(0), True)
-        
+        self.treeview.set_cursor(self.liststore.get_path(iter),
+                                 self.treeview.get_column(0), True)
+
     def eraseitem_cb(self, widget):
-        # Ignore unless on the register screen. 
+        # Ignore unless on the register screen.
         if self.activity.screens[-1] != self.activity.register:
             return
 
@@ -227,6 +239,6 @@ class RegisterScreen(Gtk.VBox):
             # Select the next item, or else the last item.
             sel.select_path(path)
             if not sel.path_is_selected(path):
-               row = path[0]-1
-               if row >= 0: 
-                   sel.select_path((row,))
+                row = path[0] - 1
+                if row >= 0:
+                    sel.select_path((row,))
diff --git a/setup.py b/setup.py
index c17ead5..e6bce08 100755
--- a/setup.py
+++ b/setup.py
@@ -1,4 +1,6 @@
 #!/usr/bin/env python
 from sugar3.activity import bundlebuilder
+
+
 if __name__ == "__main__":
     bundlebuilder.start()
-- 
1.7.10.4



More information about the Sugar-devel mailing list