r20145 - gnucash/trunk - Bug #639906: Improve text output methods of python bindings

Christian Stimming cstim at code.gnucash.org
Sat Jan 22 09:39:56 EST 2011


Author: cstim
Date: 2011-01-22 09:39:56 -0500 (Sat, 22 Jan 2011)
New Revision: 20145
Trac: http://svn.gnucash.org/trac/changeset/20145

Added:
   gnucash/trunk/src/optional/python-bindings/example_scripts/str_methods.py
Modified:
   gnucash/trunk/AUTHORS
Log:
Bug #639906: Improve text output methods of python bindings

Patch by Christoph Holtermann:

For the more complex financial objects i chose a different approach.

The printing functions are located in one module. Importing this module adds
these functions as methods to the classes defined in gnucash_core.py.

Modified: gnucash/trunk/AUTHORS
===================================================================
--- gnucash/trunk/AUTHORS	2011-01-22 14:36:18 UTC (rev 20144)
+++ gnucash/trunk/AUTHORS	2011-01-22 14:39:56 UTC (rev 20145)
@@ -170,6 +170,7 @@
 Hendrik-Jan Heins <hjh at passys.nl> Dutch translation
 Joachim Herb <joachim.herb at gmx.de> Data table option for barchart reports
 Claus Hindsgaul <claus_h at image.dk> messages Danish translation
+Christoph Holtermann <c.holtermann at gmx.de> Python wrapper improvements
 Ori Hoch <ori at uumpa.com> Hebrew translation
 Péter Hosszú <hosszu at web.de> Hungarian translation
 Edward J. Huff <ejhuff at huff20may77.us> Date handling in reports, quarterly option

Added: gnucash/trunk/src/optional/python-bindings/example_scripts/str_methods.py
===================================================================
--- gnucash/trunk/src/optional/python-bindings/example_scripts/str_methods.py	                        (rev 0)
+++ gnucash/trunk/src/optional/python-bindings/example_scripts/str_methods.py	2011-01-22 14:39:56 UTC (rev 20145)
@@ -0,0 +1,82 @@
+#!/usr/bin/env python
+
+# str_methods.py -- Add __str__ and __unicode__ methods to financial objects
+#
+ 
+##  @file
+#   @brief Add __str__ and __unicode__ methods to financial objects so that @code print object @endcode leads to human readable results
+#   @author Christoph Holtermann, c.holtermann at gmx.de
+#   @ingroup python_bindings_examples
+
+import gnucash
+
+def __split__unicode__(self):
+  """__unicode__ method for split class"""
+  from gnucash import Split
+  import time
+  self=Split(instance=self)
+
+  lot=gnucash.GncLot(instance=self.GetLot())
+  if lot:
+    lot_str=lot.get_title()
+  else:
+    lot_str='---'
+
+  transaction=self.GetParent()
+
+  return u"{0:7}{1:20}{2:7}{3:7}{4:7}{5:30}{6:12}{7:15}{8:1}{9:30}{10:5}{11:10}{12:1}".format(
+    'Konto:',self.GetAccount().name,
+    'Value:',unicode(self.GetValue()),
+    'Memo:',self.GetMemo(),
+    'Transaction:',time.ctime(transaction.GetDate()),'-',transaction.GetDescription(),
+    'Lot: ',lot_str,'\n')
+
+def __split__str__(self):
+  """__str__ method for split class"""
+  
+  from gnucash import Split
+  self=Split(instance=self)
+
+  return unicode(self).encode('utf-8')
+
+gnucash.gnucash_core_c.__split__str__=__split__str__
+gnucash.Split.add_method("__split__str__","__str__")
+
+gnucash.gnucash_core_c.__split__unicode__=__split__unicode__
+gnucash.Split.add_method("__split__unicode__","__str__")
+
+def __transaction__str__(self):
+  """__str__ method for Transaction class"""
+  from gnucash import Transaction
+  import time
+  self=Transaction(instance=self)
+
+  transaction_str = u"{0:7}{1:25}{2:14}{3:40}{4:7}{5:40}{6:1}".format(
+      'Datum:',str(time.ctime(self.GetDate())),
+      'Description:',str(self.GetDescription()),
+      'Notes:',str(self.GetNotes()),'\n')
+
+  splits_str=""
+
+  splitlist = self.GetSplitList()
+  for n,split in enumerate(splitlist):
+    if not type(split)==gnucash.Split:
+      split=gnucash.Split(instance=split)
+    splits_str += u"[{0:>2}] ".format(str(n))
+    splits_str += unicode(split)
+
+  return transaction_str + splits_str
+
+def __transaction__unicode__(self):
+  """__unicode__ method for Transaction class"""
+  from gnucash import Transaction
+
+  self=Transaction(instance=self)
+  return unicode(self).encode('utf-8')
+
+# These lines add transaction_str as method __str__ to Transaction object
+gnucash.gnucash_core_c.__transaction__str__=__transaction__str__
+gnucash.Transaction.add_method("__transaction__str__","__str__")
+
+gnucash.gnucash_core_c.__transaction__unicode__=__transaction__unicode__
+gnucash.Transaction.add_method("__transaction__unicode__","__unicode__")



More information about the gnucash-changes mailing list