I have a Python script which processes a .txt file which contains report usage information. I'd like to find a way to cleanly print the attributes of an object using pprint's pprint(vars(object)) function.
The script reads the file and creates instances of a Report class. Here's the class.
class Report(object):
def __init__(self, line, headers):
self.date_added=get_column_by_header(line,headers,"Date Added")
self.user=get_column_by_header(line,headers,"Login ID")
self.report=get_column_by_header(line,headers,"Search/Report Description")
self.price=get_column_by_header(line,headers,"Price")
self.retail_price=get_column_by_header(line,headers,"Retail Price")
def __str__(self):
from pprint import pprint
return str(pprint(vars(self)))
I'd like to be able to print instances of Report cleanly a-la-pprint.
for i,line in enumerate(open(path+file_1,'r')):
line=line.strip().split("|")
if i==0:
headers=line
if i==1:
record=Report(line,headers)
print record
When I call
print record
for a single instance of Report, this is what I get in the shell.
{'date_added': '1/3/2012 14:06',
'price': '0',
'report': 'some_report',
'retail_price': '0.25',
'user': 'some_username'}
None
My question is two-fold.
First, is this a good / desired way to print an object's attributes cleanly? Is there a better way to do this with or without pprint?
Second, why does
None
print to the shell at the end? I'm confused where that's coming from.
Thanks for any tips.
pprint.pprint
doesn't return a string; it actually does the printing (by default to stdout, but you can specify an output stream). So when you writeprint record
,record.__str__()
gets called, which callspprint
, which returnsNone
.str(None)
is'None'
, and that getsprint
ed, which is why you seeNone
.You should use
pprint.pformat
instead. (Alternatively, you can pass aStringIO
instance topprint
.)Dan's solution is just wrong, and Ismail's in incomplete.
__str__()
is not called,__repr__()
is called.__repr__()
should return a string, as pformat does.Here is an example
This prints
Which is not perfect, but passable.
For pretty-printing objects which contain other objects, etc.
pprint
is not enough. Try IPython's lib.pretty, which is based on a Ruby module.I think beeprint is what you need.
Just
pip install beeprint
and change your code to:pprint
is just another form of print. When you saypprint(vars(self))
it prints vars into stdout and returns none because it is a void function. So when you cast it to a string it turnsNone
(returned bypprint
) into a string which is then printed from the initial print statement. I would suggest changing your print topprint
or redefine print as print if its all you use it for.One alternative is to use a formatted output:
Hope this helped