Elegant ways to print out a bunch of instance attributes in python 2.6?
Posted
by wds
on Stack Overflow
See other posts from Stack Overflow
or by wds
Published on 2010-06-08T11:56:10Z
Indexed on
2010/06/08
12:02 UTC
Read the original article
Hit count: 129
First some background. I'm parsing a simple file format, and wish to re-use the results in python code later, so I made a very simple class hierarchy and wrote the parser to construct objects from the original records in the text files I'm working from.
At the same time I'd like to load the data into a legacy database, the loader files for which take a simple tab-separated format. The most straightforward way would be to just do something like:
print "%s\t%s\t....".format(record.id, record.attr1, len(record.attr1), ...)
Because there are so many columns to print out though, I thought I'd use the Template
class to make it a bit easier to see what's what, i.e.:
templ = Template("$id\t$attr1\t$attr1_len\t...")
And I figured I could just use the record in place of the map used by a substitute
call, with some additional keywords for derived values:
print templ.substitute(record, attr1_len=len(record.attr1), ...)
Unfortunately this fails, complaining that the record instance does not have an attribute __getitem__
.
So my question is twofold:
- do I need to implement
__getitem__
and if so how? - is there a more elegant way for something like this where you just need to output a bunch of attributes you already know the name for?
© Stack Overflow or respective owner