Python Shell

Forest Bond forest at alittletooquiet.net
Tue Jan 11 09:19:37 EST 2011


Hi Christoph,

On Mon, Jan 10, 2011 at 11:04:50PM +0100, Christoph Holtermann wrote:
> My interest in gnucash is to have a python-shell-environment which lets me
> easily access and display data. I have written some things for myself like
> simple functions PrintTransaction(Transaction) and so for other objects.

You should generally avoid CamelCase for function and instance names in Python
code.  CamelCase should be used for class names.  PEP 8 is a good style
reference:

  http://www.python.org/dev/peps/pep-0008/

> I use IPython for that purpose. I would like to submit my tries in that
> direction but for myself I would like to find the best solution for at least
> these printing functions before I do to not submit something too preliminary.
> 
> I wonder in which way a function that prints an complex object should be done.
> 
> When looking around in the internet i saw things like object.prettyPrint(). I
> came across object.__format__().

You most likely want to use __str__ and/or __unicode__.

  http://docs.python.org/reference/datamodel.html#object.__str__

I suggest reading this page:

  http://stackoverflow.com/questions/1307014/python-str-versus-unicode

The Python documentation doesn't seem to cover __unicode__ very well...

__repr__ may also be of interest.

> When I print out for example a transaction i want to print out information of
> objects that are nested into it. For example I would probably also to print
> out the splits.
> 
> I would like to have some function of the objects that are always called the
> same way. In the end I would like to have something as easy as
> 
> print object.

If you implement __str__/__unicode__ methods, you can just do this:

>>> print obj

Hope this helps.

Thanks,
Forest
-- 
Forest Bond
http://www.alittletooquiet.net
http://www.pytagsfs.org
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 198 bytes
Desc: Digital signature
URL: <http://lists.gnucash.org/pipermail/gnucash-devel/attachments/20110111/906951d6/attachment.bin>


More information about the gnucash-devel mailing list