[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


Re: [Tutor] Help me understand this tkinter related code

2010-02-26 Thread Alan Gauld

Please don't hijack an old message to create a new subject.
Those of us using threaded readers might not see it and you
lose potential answers.


"Robert DeLaurentis"  wrote

The local function change_volume(v) requires the argument v to operate 
properly,
but I'm unclear where the value of that argument is being set or why the 
code fails

without it.



def change_volume(v):
   track.set_volume(volume.get())

volume_scale = Scale(app,
   variable = volume,
   from_= 0.0,
   to   = 1.0,
   resolution   = 0.1,
   command  = change_volume,
   label= "Volume",
   orient   = HORIZONTAL)


I'm not familiar with the wodget in question but it looks like the command 
callback
expects to pass a parameter to the callback function. Therefore the 
function needs

to provide a placeholder even though it doesn't use it!

You would need to find the documentation for the widget to confirm that 
theory

however.

A Google seartch threw up this:

http://infohost.nmt.edu/tcc/help/pubs/tkinter/scale.html

Which seems to confirm my theory :-)


--
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/


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


[Tutor] imaplib command to delete messages

2010-02-26 Thread Bill Campbell
I've written a script to go through varions Sent folders on
IMAP servers to archive the messages on a central server using
imaplib to do the heavy lifting.  Perhaps I'm a bit thick, but I
have not been able to figure out how to delete messages in the
folders after processing them.  The imaplib documentation as lots
of ways to delete mailboxes (folders) and expunge messages from
folders, but I don't see anything about marking individual
messages as deleted.

What am I missing?

Bill
-- 
INTERNET:   b...@celestial.com  Bill Campbell; Celestial Software LLC
URL: http://www.celestial.com/  PO Box 820; 6641 E. Mercer Way
Voice:  (206) 236-1676  Mercer Island, WA 98040-0820
Fax:(206) 232-9186  Skype: jwccsllc (206) 855-5792

Our Foreign dealings are an Open Book, generally a Check Book.
Will Rogers
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Help me understand this tkinter related code

2010-02-26 Thread Robert DeLaurentis
I've just begun learning Python and programming using the Head First 
Programming from O'Reilly. The following code works on my Mac just fine, but 
there is a mystery as to how its working that escapes me so far.

The local function change_volume(v) requires the argument v to operate 
properly, but I'm unclear where the value of that argument is being set or why 
the code fails without it. It appears to be a detail that isn't explained in 
the text. When I compare the two widgets and their associated functions, both 
seem to use the .get method to access the value set by the widget, so in that 
case the v seems superfluous.

I thought that perhaps the answer might be found in the tkinter code that 
defines Scale, but I'm not even sure where on the computer that code is located.

Thank you!
Bob


Here is the code (the comments are mine, otherwise the code matches the book 
except that I grouped some items differently -- I like to keep variables all in 
one place as much as possible):


#!/usr/local/bin/python3

# load external libraries
from tkinter import *
import pygame.mixer

# initialize the visual interface
app = Tk()
app.title("Head First Mix")
app.geometry('250x100+200+100')

# initialize the sound player
mixer = pygame.mixer
mixer.init()

# initialize the variables
sound_file = "50459_M_RED_Nephlimizer.wav"
track = mixer.Sound(sound_file)
track_playing = IntVar()

# local functions
def shutdown():
track.stop()
app.destroy()

def track_toggle():
if track_playing.get() == 1:
track.play(loops = -1)
else:
track.stop()

def change_volume(v):
track.set_volume(volume.get())



# define interface widgets
track_button = Checkbutton(app, variable = track_playing, 
command = track_toggle, 
text = sound_file)

track_button.pack()

volume = DoubleVar()
volume.set(track.get_volume())
volume_scale = Scale(app,
variable = volume,
from_= 0.0,
to   = 1.0,
resolution   = 0.1,
command  = change_volume,
label= "Volume",
orient   = HORIZONTAL)

volume_scale.pack()

# main entry point
app.protocol("WM_DELETE_WINDOW", shutdown)
app.mainloop()




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


Re: [Tutor] How to use pydoc

2010-02-26 Thread David Hutto


--- On Fri, 2/26/10, Ricardo Aráoz  wrote:

From: Ricardo Aráoz 
Subject: [Tutor] How to use pydoc
To: tutor@python.org
Date: Friday, February 26, 2010, 8:31 AM

Checked the manuals on pydoc and wanted to try it. Must certainly be
doing something wrong but I can't figure what. Here's my session :

Python 2.5.2 (r252:60911, Feb 21 2008, 13:11:45) [MSC v.1310 32 bit
(Intel)] on
win32
Type "help", "copyright", "credits" or "license" for more information.
>>> pydoc sys
  File "", line 1
    pydoc sys
            ^
SyntaxError: invalid syntax
>>> import pydoc
>>> pydoc sys
  File "", line 1
    pydoc sys
            ^
SyntaxError: invalid syntax
>>> pydoc

>>> import sys
>>> pydoc sys
  File "", line 1
    pydoc sys
            ^
SyntaxError: invalid syntax
>>>

What's my mistake?

TIA


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


It looks like you're using idle, you place the command in the command 
prompt/terminal like:

python -m pydoc sys

Look here about three down it's in the page summary:

http://www.google.com/search?hl=en&client=firefox-a&rls=com.ubuntu%3Aen-US%3Aofficial&q=pydoc+sys&aq=f&aqi=g1g-s1g-sx1&aql=&oq=



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


[Tutor] How to use pydoc

2010-02-26 Thread Ricardo Aráoz
Checked the manuals on pydoc and wanted to try it. Must certainly be
doing something wrong but I can't figure what. Here's my session :

Python 2.5.2 (r252:60911, Feb 21 2008, 13:11:45) [MSC v.1310 32 bit
(Intel)] on
win32
Type "help", "copyright", "credits" or "license" for more information.
>>> pydoc sys
  File "", line 1
pydoc sys
^
SyntaxError: invalid syntax
>>> import pydoc
>>> pydoc sys
  File "", line 1
pydoc sys
^
SyntaxError: invalid syntax
>>> pydoc

>>> import sys
>>> pydoc sys
  File "", line 1
pydoc sys
^
SyntaxError: invalid syntax
>>>

What's my mistake?

TIA


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


Re: [Tutor] Top posters for 2009

2010-02-26 Thread Stefan Behnel

Shashwat Anand, 26.02.2010 04:57:
> @Kent: thanks for the script. It is kool.
> 
> Here is 2010 list of Top-20 (as of now):
> 
> 2010 (1155 posts, 204 posters)
> 
> Alan Gauld 127 (11.0%)
> Kent Johnson 108 (9.4%)
> spir 52 (4.5%)
> Wayne Watson 46 (4.0%)
> Luke Paireepinart 32 (2.8%)
> Shashwat Anand 30 (2.6%)
> Wayne Werner 29 (2.5%)
> Steven D'Aprano 28 (2.4%)
> Stefan Behnel 24 (2.1%)
> Dave Angel 22 (1.9%)
> Lie Ryan 19 (1.6%)
> Hugo Arts 16 (1.4%)
> Benno Lang 14 (1.2%)
> David 14 (1.2%)
> Giorgio 14 (1.2%)
> Serdar Tumgoren 14 (1.2%)
> Grigor Kolev 13 (1.1%)
> Eike Welk 13 (1.1%)
> Christian Witts 12 (1.0%)
> invincible patriot 12 (1.0%)
> 
> No more spamming now :P
> 
> ~l0nwlf
> 
> On Fri, Feb 26, 2010 at 9:10 AM, Shashwat Anand 
> wrote:
> 
>> How about Top-40 posters (so that I can make the cut..Yayyy) :P
>>
>>
>> 2009 (7730 posts, 709 posters)
>> 
>> Alan Gauld 969 (12.5%)
>> Kent Johnson 804 (10.4%)
>> Dave Angel 254 (3.3%)
>> spir 254 (3.3%)
>> Wayne Watson 222 (2.9%)
>> bob gailer 191 (2.5%)
>> Lie Ryan 186 (2.4%)
>> David 127 (1.6%)
>> Emile van Sebille 115 (1.5%)
>> Wayne 112 (1.4%)
>> Sander Sweers 111 (1.4%)
>> Serdar Tumgoren 100 (1.3%)
>> Luke Paireepinart 99 (1.3%)
>> wesley chun 99 (1.3%)
>> W W 74 (1.0%)
>> Marc Tompkins 72 (0.9%)
>> A.T.Hofkamp 71 (0.9%)
>> Robert Berman 68 (0.9%)
>> vince spicer 63 (0.8%)
>> Emad Nawfal 62 (0.8%)
>> Andre Engels 61 (0.8%)
>> Rich Lovely 60 (0.8%)
>> Christian Witts 57 (0.7%)
>> Martin Walsh 51 (0.7%)
>> Eduardo Vieira 47 (0.6%)
>> Tim Golden 47 (0.6%)
>> prasad rao 47 (0.6%)
>> Dinesh B Vadhia 47 (0.6%)
>> John Fouhy 42 (0.5%)
>> Norman Khine 41 (0.5%)
>> Che M 41 (0.5%)
>> Stephen Nelson-Smith 40 (0.5%)
>> Mark Tolonen 40 (0.5%)
>> Chris Fuller 38 (0.5%)
>> Stefan Behnel 35 (0.5%)
>> Wayne Werner 34 (0.4%)
>> Steve Willoughby 32 (0.4%)
>> Shashwat Anand 32 (0.4%)
>> Eike Welk 31 (0.4%)
>> Albert-Jan Roskam 30 (0.4%)
>>
>> ~l0nwlf
>>
>>
>> On Fri, Feb 26, 2010 at 9:04 AM, Shashwat Anand 
>> wrote:
>>
>>> nice. Kudos to all top posters. May be I should search my rank ;)
>>>
>>> ~l0nwlf
>>>
>>>
>>> On Fri, Feb 26, 2010 at 8:23 AM, Kent Johnson  wrote:
>>>
 It's not really about keeping score :-), but once again I've compiled
 a list of the top 20 posters to the tutor list for the last year. For
 2009, the rankings are

 2009 (7730 posts, 709 posters)
 
 Alan Gauld 969 (12.5%)
 Kent Johnson 804 (10.4%)
 Dave Angel 254 (3.3%)
 spir 254 (3.3%)
 Wayne Watson 222 (2.9%)
 bob gailer 191 (2.5%)
 Lie Ryan 186 (2.4%)
 David 127 (1.6%)
 Emile van Sebille 115 (1.5%)
 Wayne 112 (1.4%)
 Sander Sweers 111 (1.4%)
 Serdar Tumgoren 100 (1.3%)
 Luke Paireepinart 99 (1.3%)
 wesley chun 99 (1.3%)
 W W 74 (1.0%)
 Marc Tompkins 72 (0.9%)
 A.T.Hofkamp 71 (0.9%)
 Robert Berman 68 (0.9%)
 vince spicer 63 (0.8%)
 Emad Nawfal 62 (0.8%)

 Alan, congratulations, you pulled ahead of me for the first time in
 years! You posted more than in 2008, I posted less. Overall posts are
 up from last year, which was the slowest year since I started
 measuring (2003).

 Thank you to everyone who asks and answers questions here!

 The rankings are compiled by scraping the monthly author pages from
 the tutor archives, using Beautiful Soup to extract author names. I
 consolidate counts for different capitalizations of the same name but
 not for different spellings. The script is below.

 Kent

 ''' Counts all posts to Python-tutor by author'''
 # -*- coding: latin-1 -*-
 from datetime import date, timedelta
 import operator, urllib2
 from BeautifulSoup import BeautifulSoup

 today = date.today()

 for year in range(2009, 2010):
startDate = date(year, 1, 1)
endDate = date(year, 12, 31)
thirtyOne = timedelta(days=31)
counts = {}

# Collect all the counts for a year by scraping the monthly author
 archive pages
while startDate < endDate and startDate < today:
dateString = startDate.strftime('%Y-%B')

url = 'http://mail.python.org/pipermail/tutor/%s/author.html'
 % dateString
data = urllib2.urlopen(url).read()
soup = BeautifulSoup(data)

li = soup.findAll('li')[2:-2]

for l in li:
name = l.i.string.strip()
counts[name] = counts.get(name, 0) + 1

startDate += thirtyOne

# Consolidate names that vary by case under the most popular spelling
nameMap = dict() # Map lower-case name to most popular name

# Use counts.items() so we can delete from the dict.
for name, count in sorted(counts.items(),
 key=operator.itemgetter(1), reverse=True):
   lower = name.lower()
   if lower in nameMap:
  # Add counts for a name we have seen already 

Re: [Tutor] PyAutoRun

2010-02-26 Thread Luke Paireepinart
On Fri, Feb 26, 2010 at 1:41 AM, Zubin Mithra wrote:

> I have been using python for quite some time; however this is the
> first python project i have worked on.
>
> The code is hosted at http://github.com/zubin71/PyAutoRun
>
> The code needs re-factoring and feature additions; i have put up a
> TODO list there too. It`d be great if anyone could work on this; i
> intend to develop this further(with a bit of help) and will request
> for its addition into debian and ubuntu repositories, in time.
>
> Also, any kind of code-review, criticism, is also appreciated.
> However, it`d be awesome if you could just fork it at github, pull,
> modify and push. :)
>


I'm not too clear, perhaps you could explain what advantage this has over
just writing a shell script / makefiles?

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


Re: [Tutor] Top posters for 2009

2010-02-26 Thread spir
On Fri, 26 Feb 2010 09:27:04 +0530
Shashwat Anand  wrote:

> @Kent: thanks for the script. It is kool.
> 
> Here is 2010 list of Top-20 (as of now):
> 
> 2010 (1155 posts, 204 posters)
> 
> Alan Gauld 127 (11.0%)
> Kent Johnson 108 (9.4%)
> spir 52 (4.5%)
> Wayne Watson 46 (4.0%)
> Luke Paireepinart 32 (2.8%)
> Shashwat Anand 30 (2.6%)
> Wayne Werner 29 (2.5%)
> Steven D'Aprano 28 (2.4%)

I think and hope Steven will fast climb up. His posts beeing so accurate and 
informative for me.

Denis
-- 


la vita e estrany

spir.wikidot.com

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


Re: [Tutor] Top posters for 2009

2010-02-26 Thread spir
On Thu, 25 Feb 2010 21:53:24 -0500
Kent Johnson  wrote:

> 2009 (7730 posts, 709 posters)
> 
> Alan Gauld 969 (12.5%)
> Kent Johnson 804 (10.4%)
> Dave Angel 254 (3.3%)
> spir 254 (3.3%)
> Wayne Watson 222 (2.9%)

I hope Dave is as thin as I am (*).

Denis

(*) for us to be able to climb on the same step of the podium ;-)

-- 


la vita e estrany

spir.wikidot.com

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