[Tutor] Any Tutor there ? Removing redundant parameters in a models file having include files.
Karim Liateni
karim.liateni at free.fr
Sun Feb 28 12:53:05 CET 2010
Hello Tutor,
Since Friday I get no answers to my first post.
So I re-post it was missed by the numerous arriving email:
This is my first program in python.
I am doing electrical simulation in spectre (spice like).
I have a models file which holds many parameters and
include files of parameters. But in batch mode the automatic
construction (by the simulator) of this file is not correct.
It concatenates both parameters and include files with the same
parameters definitions. That trigs errors during simulation and
it complains about parameters double definition.
The program must removes includes files of already existing parameters
in the models file.
The file format is shown below:
// ------------------------------------------------------
// Mismatch Variations Selection
// ------------------------------------------------------
parameters param1=0
parameters param2=0
parameters param3=0
parameters param4=0
// ------------------------------------------------------
// Global Flags Definition
// ------------------------------------------------------
parameters gflag_param = 0
// ------------------------------------------------------
// Models Library Files References
// ------------------------------------------------------
include "/project/hvt.scs" section=TT
include "/project/lvt.scs" section=TT
include "/project/svt.scs" section=TT
include "/project/veriloga.scs" section=typ
include "/project/switch_all.scs" section=no
-------------------------------------------------------
Now my code is shown below:
___________________________
#!/usr/bin/env python
# File: correctCorners K.Liateni 2010-02-25
import re
# -------------------------------
# Functions Declaration
# -------------------------------
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()
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
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
# -------------------------------
# Main Start here
# -------------------------------
parRe = '^[ \t]*(parameters[ \t]+[^ \t]+)[ \t]*='
comRe = '^[ \t]*//'
include = re.compile(incRe)
param = re.compile(parRe)
comment = re.compile(comRe)
lines = getLines("models.scs")
linesWithNoInclude = [ e for e in lines if not include.search(e) ]
linesWithNoParam = [ e for e in lines if not param.search(e) and not
comment.search(e) ]
parameters = [ param.search(e).group(1) for e in linesWithNoInclude
if param.search(e) ]
includeFiles = [ include.search(e).group(1) for e in linesWithNoParam ]
includeFilesToWrite = [ e for e in includeFiles if not
isParamExist(e, param, parameters) ]
includeFilesToWrite = [ e for e in linesWithNoParam if
include.search(e).group(3) in includeFilesToWrite ]
cat(linesWithNoInclude+includeFilesToWrite, "models_modified.scs")
copy("models_modified.scs", "models.scs"):
# ----------- end of file ----------- #
The code works well but I am not fully happy with it.
includeFilesToWrite is computed in 2 steps.
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 am using python 2.2 so I have not use latest feature I want my code to
run on old Solaris too (linux user).
I use as much as possible list comprehension following your advices.
I don't like the part:
if matchParam:
return True
else:
return False
Is there an easy way to do the same behavior. I am not sure but I saw
one time something like using
the int value of True (1) and False (0) to select return value in a list
of 2 elements 'return [False,True](matchParam)'
Is it correct?
Thanks a lot
Karim
_______________________________________________
Tutor maillist - Tutor at python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
More information about the Tutor
mailing list