[Tutor] Any Tutor there ? Removing redundant parameters in a models file having include files.
Alan Gauld
alan.gauld at btinternet.com
Sun Feb 28 15:12:59 CET 2010
"Karim Liateni" <karim.liateni at free.fr> wrote
> It concatenates both parameters and include files with the same
> parameters definitions. That trigs errors during simulation and
> it complains about parameters double definition.
I'd suggest you construct a dictionary based on the param names
You can check before you add ca param if one already exists.
Or alternatively make a Set of param names and check that
for existence before adding a new one.
> def copy(infile, outfile):
> """Copy of the content of an input file to an outputfile."""
> fout = open(outfile, 'w')
> fout.writelines(getLines(infile))
> fout.close()
Its probably easier tyo use shutil.copyfile()
> def cat(lines, outfile):
> """Concat the content of a strings list to an outputfile."""
> f = open(outfile, 'w')
> f.writelines(lines)
> f.close()
> def getLines(file):
> """Get the content of a file in a lines list form."""
> f = open(file, 'r')
> lines = f.readlines()
> f.close()
> return lines
I'm not sure these functions add enough value to ghave them. I';d probably
just use
try: open(outfile,'w').writelines(lines)
except IOError: #handle error
try: lines = open(filename).readlines()
except IOError: #handle error
The close will be done automatically since you don't hold a reference to
the file
> def isParamExist(file, pattern, parameters):
> """Check if a particular regex pattern parameter is existing in a
> parameters file."""
> lines = getLines(file)
> paramExpressions = [ e for e in lines if pattern.search(e) ]
> matchParam = [ e for e in paramExpressions if
> pattern.search(e).group(1) in parameters ]
> if matchParam:
> return True
> else:
> return False
return bool(matchParam) instead of the if/else
> I am pretty sure that it can be simplify by using a dictionnary with the
> full path file as the key
> and the string 'include' nominal line expression as the value to
> construct the final file with filtered include.
I think dictionaries or Sets could improve things
> I don't like the part:
> if matchParam:
> return True
> else:
> return False
See the note above.
HTH,
--
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/
More information about the Tutor
mailing list