Re: [Tutor] Tutorial executable from python script.

2010-03-21 Thread Karim Liateni


Hello Lie,

Thanks for your advices.

To have correct updates from ITs is really a true pain. The network
is worldwide in our company. I found issues having decent version.
On my local workstation I have Python v1.5, on compute farm LSF
machines sometimes 2.2 , 2.3, 2.6. That's why I don't want to rely
on machine installation and provide a unique version with my
application installation. I know we did the same for TCL to be sure
to have 8.4 version. I just wanted to know if there is some tutos about
this topic.

Regards
Karim

Lie Ryan wrote:

On 03/21/2010 06:00 AM, Karim Liateni wrote:
  

Hello Alan,

In fact, I want to be sure the users can run it on every machine in our
network.
Especially, I want to be able to run it on Solaris 5.8 with python 1.5
(Unix machine).
I wanted to know if I could make some custom executable like in C when
you want
to build a executable with a static library to be sure if the system
does not have
the correct shares libraries.



If you know that the machine contains `python` (whatever the version is)
you can use sys.version to check the system python's version. It can be
as simple as:

import sys
if int(sys.version[0]) > 1 or
   (int(sys.version[0]) == 1 and int(sys.version[2] >= 5)):
   # or you can start a subprocess instead,
   # abusing import makes "if __name__ == '__main__':" magic not work
   import MyMainProgram
else:
   # parentheses guards for python 3
   print ('script is only compatible with python version 1.5 and above')

Otherwise, if you cannot even rely on python being available, you may
need to use shell script.

  

Perhaps the better is to build a python version embedded in my
application installation.
Do you have any examples or tutorial on how integrate python inside a
pplication to be
sure that we have all in one and not depand on any local machine
installation environment.
I need as to embed gtk python library for graphical use.



That is indeed possible, however is there any reason why the server
don't upgrade its python version? CPython makes it easy to do parallel
installation of two different python version. If you can persuade the
machine administrator to install python2.6 as an altinstall; you can
simply change the hashbang line of your script to "#!/usr/bin/env
python2.6" and all is well.

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

  


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Tutorial executable from python script.

2010-03-20 Thread Karim Liateni


Hello Alan,

In fact, I want to be sure the users can run it on every machine in our 
network.
Especially, I want to be able to run it on Solaris 5.8 with python 1.5 
(Unix machine).
I wanted to know if I could make some custom executable like in C when 
you want
to build a executable with a static library to be sure if the system 
does not have

the correct shares libraries.

Perhaps the better is to build a python version embedded in my 
application installation.
Do you have any examples or tutorial on how integrate python inside a 
pplication to be
sure that we have all in one and not depand on any local machine 
installation environment.

I need as to embed gtk python library for graphical use.

Thanks
Karim

Alan Gauld wrote:


"Karim Liateni"  wrote

on machine which doesn't have recent version (2.0) of python. 


Given that v2 is at least 10 years old now that's not really "recent"
I'd be surprised if any current Linux distros had anything that old on 
them! Even the ones designed for old hardware.
In fact, are you sure your code runs on v1 python? There have been a 
lot of language changes since then.



the compile()
method but how can I use it to make all in one executable 


compile() doesn't make an exe, it compiles python script into python 
byte code - much as the javac compiler compiles java source into java 
bytecode.


run on old system (old python). 


Exactly how old is old?

If you write the code to run on an old Python interpreter it should 
still work(mostly!) on Python 2.6. The trick is not to compile the 
code but to write code that is consistent wityh the oldest version of 
Python you need to run on.


HTH,



___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Tutorial executable from python script.

2010-03-19 Thread Karim Liateni


Hello,

I want to do a executable for linux/unix from python scripts and thus o 
allow to run
on machine which doesn't have recent version (2.0) of python. I found 
the compile()
method but how can I use it to make all in one executable which could be 
run on old

system (old python). If you have any links I would love to have it

Thanks
Karim
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] parsing a "chunked" text file

2010-03-19 Thread Karim Liateni

Hello,

Thanks both of you for these useful information.

Regards
Karim

Hugo Arts wrote:

On Thu, Mar 18, 2010 at 12:54 PM, Stefan Behnel  wrote:
  

Karim Liateni, 04.03.2010 01:23:

Yes, a *big* difference in the true sense of the word. Your code (assuming
you meant to write "... for line in ..." ) evaluates the entire list
comprehension before returning from the call. Steven's code returns a
generator that only handles one line (or a couple of empty lines) at a time.
So, assuming that this runs against a large file, Steven's code uses only a
constant amount of memory, compared to the whole file in your case, and is
likely also a lot faster than your code as it involves less looping.




Though, if you changed the brackets into parentheses, you'd get a
generator expression, which *is* equivalent to Steven's version,
except that it calls strip() twice, which is a bit wasteful.

If the unnecessary extra call bothers you, you could do one of two things:
1) Learn how the yield keyword works. You should do this. It's an
awesome feature, and you'll come across it many more times.
2) go functional and import itertools. ifilter with a generator
expression, like so (pure functional programmers can also use imap
instead of the generator expr., which might be faster. profile to be
sure)

def skip_blanks(lines):
return ifilter(None, (l.strip() for l in lines))

Very short, has all the memory and speed benefits of the generator.
Personally I really like terse functional programming like this,
though I believe the general consensus in the python community is that
imperative alternatives are usually clearer to read.

If you want to know more about the yield keyword:
A terse description (assumes that you know how iterators work) is
here: http://docs.python.org/tutorial/classes.html#generators
A more detailed description of iterators and generators can be found
here: http://www.ibm.com/developerworks/library/l-pycon.html

Hugo
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

  


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] parsing a "chunked" text file

2010-03-03 Thread Karim Liateni


Hello Steven,

Is there a big difference to write your first functions as below because 
I am not familiar with yield keyword?


def skip_blanks(lines):
   """Remove leading and trailing whitespace, ignore blank lines."""
   return [line.strip() in lines if line.strip()]


I tried to write as well the second function but it is not as straight 
forward.

I begin to understand the use of yield in it.

Regards
Karim

Steven D'Aprano wrote:

On Tue, 2 Mar 2010 05:22:43 pm Andrew Fithian wrote:
  

Hi tutor,

I have a large text file that has chunks of data like this:

headerA n1
line 1
line 2
...
line n1
headerB n2
line 1
line 2
...
line n2

Where each chunk is a header and the lines that follow it (up to the
next header). A header has the number of lines in the chunk as its
second field.



And what happens if the header is wrong? How do you handle situations 
like missing headers and empty sections, header lines which are wrong, 
and duplicate headers?


line 1
line 2
headerB 0
headerC 1
line 1
headerD 2
line 1
line 2
line 3
line 4
headerE 23
line 1
line 2
headerB 1
line 1



This is a policy decision: do you try to recover, raise an exception, 
raise a warning, pad missing lines as blank, throw away excess lines, 
or what?



  

I would like to turn this file into a dictionary like:
dict = {'headerA':[line 1, line 2, ... , line n1], 'headerB':[line1,
line 2, ... , line n2]}

Is there a way to do this with a dictionary comprehension or do I
have to iterate over the file with a "while 1" loop?



I wouldn't do either. I would treat this as a pipe-line problem: you 
have a series of lines that need to be processed. You can feed them 
through a pipe-line of filters:


def skip_blanks(lines):
"""Remove leading and trailing whitespace, ignore blank lines."""
for line in lines:
line = line.strip()
if line:
yield line

def collate_section(lines):
"""Return a list of lines that belong in a section."""
current_header = ""
accumulator = []
for line in lines:
if line.startswith("header"):
yield (current_header, accumulator)
current_header = line
accumulator = []
else:
accumulator.append(line)
yield (current_header, accumulator)


Then put them together like this:


fp = open("my_file.dat", "r")
data = {}  # don't shadow the built-in dict
non_blank_lines = skip_blanks(fp)
sections = collate_sections(non_blank_lines)
for (header, lines) in sections:
data[header] = lines


Of course you can add your own error checking.


  


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Any Tutor there ? Removing redundant parameters in a models file having include files.

2010-03-03 Thread Karim Liateni


Hello Alan, Steven,

I was narrow minded about this topic and did not see the benefits of 
these multiple Python

implementations. You opened my eyes.

Regards
Karim


Steven D'Aprano wrote:

On Tue, 2 Mar 2010 11:25:44 am Andreas Kostyrka wrote:
  

Furthermore I do not think that most of the "core" community has a
problem with the alternate implementations, as they provide very
useful functions (it helps on the architecture side, because it
limits somewhat what can be done, it helps on the personal side,
because it increases the value of Python skills, ...), ...



The Python development team values alternative implementations, as it 
gives Python the language a much wider user base.


It also allows other people to shoulder some of the development burden. 
For example, people who want Python without the limitations of the C 
call stack can use Stackless Python, instead of ordinary CPython. 
Google is sponsoring a highly optimized version of Python with a JIT 
compiler: Unladen Swallow. It looks likely that Unladen Swallow will 
end up being merged with CPython too, which will be a great benefit.



  


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Any Tutor there ? Removing redundant parameters in a models file having include files.

2010-03-02 Thread Karim Liateni


Hello,

Thanks a lot for this state of the art of the language, very instructive.
I see now top of the iceberg  ;o)

Karim

Steven D'Aprano wrote:

On Tue, 2 Mar 2010 07:07:57 am Karim Liateni wrote:
  

Thanks for this precision!
I'm using standard python so this is ok!
Why people use proprietary python ?
To have more trouble ? To be different from the rest of community ?



Python is a language, but there can be many different implementations of 
that language, just like there are different C compilers or different 
Javascript engines.


CPython is the version which was made first, it is the most common 
version, but it is not the only one. It is called CPython because it is 
written in C.


Jython is a version of Python written in Java, and it was created by 
people wanting to use Python as a front-end to Java libraries, and to 
take advantage of Java's garbage collector.


IronPython is Microsoft's version of Python written for .Net and Mono.

PyPy is an experimental version of Python written in Python, used by 
people wanting to experiment with Python compilers.


"Python for S60" is a version of Python written for Nokia's S60 devices.

CapPython is an experimental version of Python designed for security.

There are many others, they are all Python, but they have differences.
  


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Any Tutor there ? Removing redundant parameters in a models file having include files.

2010-03-01 Thread Karim Liateni


Thanks for this precision!
I'm using standard python so this is ok!
Why people use proprietary python ?
To have more trouble ? To be different from the rest of community ?
Anyway in opensource people do whatever they want but you know
multiple version that was the same before Common C or Lisp it was war 
fields.
It's better to spent energy to participate with the core developers to 
make the

common langage evoluate.

Regards

Karim

Alan Gauld wrote:


"Karim Liateni"  wrote


def getLines(file):
try: lines = open(filename).readlines() ; return lines
except IOError: #handle error


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?


Sure, the lines variable holds a reference to the list returned by 
readlines.

There is no variable referring to the file object so immediately after
readlines completes the file will be ready to be closed (at least in
CPython as already pointed out by Lie)

HTH,



___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Any Tutor there ? Removing redundant parameters in a models file having include files.

2010-02-28 Thread Karim Liateni

Lie Ryan wrote:

On 03/01/10 02:49, Karim Liateni wrote:
  

Lie Ryan wrote:


On 03/01/10 01:12, Alan Gauld wrote:
  
  

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



Remember why we have the new with-block? It's precisely because files
will be automagically closed only if we're running on CPython; Python
the language and thus alternative implementations doesn't guarantee
automatic closing. I'd agree with the function having minimal utility
value though:

with open(file) as f:
lines = f.readlines()
# f.close() will be called by the context manager

and if you're just copying to another file:

from contextlib import nested
with nested(open(infile), open(outfile, 'w')) as (fin, fout):
fout.write(fin.read())

or even better, as Alan suggested, using shutil.copyfile().

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

  
  

Thank you Lie but I have a restriction on the python version I must use
v2.2.
This feature is available only on later version 2.5 I believe.



Then you should at the least use the try-finally block, I think that one
has been there since 2.2? If you didn't use try-finally, there is no
guarantee that f.close() would be called if an exception happened in the
middle of reading/writing (e.g. KeyboardInterrupt, or perhaps user
plugging off the thumbdrive, or bit more realistic having a full
harddisk or exceeded some disk quota)

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

  

Thank you Lie.
Yes, in fact I did it all the time during my years of Java development.
In perl I used or open() or die( "msg" ) structure.
I bypassed it because I wanted to construct the blocs very quickly like 
a 'beginner'.
I was excited to make it work as fast as ossible to see if I can program 
something

decent in Python. (gaining confidence? haaa human nature!).

I definitely enjoy python (and not very far in spirit my loving Lisp).
I definitely HATE tcl, java.

Now I will improve quality and robustness.

Thanks a lot!

Karim

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Any Tutor there ? Removing redundant parameters in a models file having include files.

2010-02-28 Thread Karim Liateni

Lie Ryan wrote:

On 03/01/10 01:12, Alan Gauld wrote:
  

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



Remember why we have the new with-block? It's precisely because files
will be automagically closed only if we're running on CPython; Python
the language and thus alternative implementations doesn't guarantee
automatic closing. I'd agree with the function having minimal utility
value though:

with open(file) as f:
lines = f.readlines()
# f.close() will be called by the context manager

and if you're just copying to another file:

from contextlib import nested
with nested(open(infile), open(outfile, 'w')) as (fin, fout):
fout.write(fin.read())

or even better, as Alan suggested, using shutil.copyfile().

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

  
Thank you Lie but I have a restriction on the python version I must use 
v2.2.

This feature is available only on later version 2.5 I believe.

Regards
Karim

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Any Tutor there ? Removing redundant parameters in a models file having include files.

2010-02-28 Thread Karim Liateni


Hello Alan,


Alan Gauld wrote:

"Karim Liateni"  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


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


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

2010-02-28 Thread Karim Liateni


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@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


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

2010-02-27 Thread Karim Liateni


Ok I must add an explanation The program must removes includes
files of already existing parameters in the models file.

Karim

karim.liat...@free.fr wrote:

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')
  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@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

  


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


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

2010-02-26 Thread karim . liateni

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')
  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@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


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

2010-02-26 Thread karim . liateni

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')
  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@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor