[Tutor] Any Tutor there ? Removing redundant parameters in a models file having include files.
Karim Liateni
karim.liateni at free.fr
Sun Feb 28 16:40:56 CET 2010
Hello Alan,
Alan Gauld wrote:
> "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.
Yes if I made it with Awk I would definitely use dictionary.
I was focused on list comprehension...First I wanted it to work
even if the method is 'awkward'. Now I will improve it for efficiency.
>
>> 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().
Thank you I was certain the function already exist!
But I did not know in which package.
>
>> 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 have 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
>
I don't like to repeat code. Is it ok to take your corrections and write
something like (not exactly the final code but approaching:
def cat(lines, outfile):
"""Concat the content of a strings list to an outputfile."""
try: open(outfile,'w').writelines(lines)
except IOError: #handle error
def getLines(file):
"""Get the content of a file in a lines list form."""
try: lines = open(filename).readlines() ; return lines
except IOError: #handle error
But In the first form I understand the close is not needed but in the
second 'lines = open(filename).readlines()'
I don't hold indirectly a reference to the file? Please, could you
explain more this point?
>> 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
One more times thanks! That was I have been searching for.
>
>
>> 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
My next goal.
>
>> I don't like the part:
>> if matchParam:
>> return True
>> else:
>> return False
>
> See the note above.
>
> HTH,
>
Alan, I am your obligé. Your remarks are very instructive.
Regards
Karim
More information about the Tutor
mailing list