Best way to print a module?
Martin De Kauwe
mdekauwe at gmail.com
Mon Sep 5 19:02:41 EDT 2011
Hi,
Tim yes I had a feeling my posting might be read as ambiguous! Sorry I
was trying to quickly think of a good example. Essentially I have a
set of .ini parameter files which I read into my program using
configobj, I then replace the default module parameters if the user
file is different (in my program). When it comes to writing the data
back out I need to potentially loop over 5 module files and I need to
ignore the superfluous information. My guess is that the way I am
doing it might be a little on the slow side, hence the posting. Like
you said it might be that the way I am doing it is "fine", I just
wanted to see if there was a better way that is all.
Rantingrick I did actually try the dir() to start with, I can't
remember why I changed back. I will try your suggestion and see
(thanks)
So instead of sys as per my example my module more realistically looks
like this:
params.py
apples = 12.0
cats = 14.0
dogs = 1.3
so my fuller example then
import sys
sys.path.append("/Users/mdekauwe/Desktop/")
import params
#params.py contains
#apples = 12.0
#cats = 14.0
#dogs = 1.3
fname = "test.asc"
try:
ofile = open(fname, 'w')
except IOError:
raise IOError("Can't open %s file for write" % fname)
data = []
for attr in params.__dict__.keys():
if not attr.startswith('__') and not attr.endswith('__'):
attr_val = getattr(params, attr)
data.append((attr, attr_val))
data.sort()
try:
ofile.write("[params]\n")
for i in data:
ofile.write("%s = %s\n" % (i[0], i[1]))
except IOError:
raise IOError("Error writing params files, params section")
etc, etc
thanks
More information about the Python-list
mailing list