[Tutor] Removing redundant parameters in a models file having include files.

karim.liateni at free.fr karim.liateni at free.fr
Sat Feb 27 01:09:54 CET 2010

Hello All,

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 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')

def cat(lines, outfile):
  """Concat the content of a strings list to an outputfile."""
  f = open(outfile, 'w')

def getLines(file):
  """Get the content of a file in a lines list form."""
  f = open(file, 'r')
  lines = f.readlines()
  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
    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
    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


More information about the Tutor mailing list