Re: [Tutor] repeated times

2007-10-30 Thread Alan Gauld

"linda.s" <[EMAIL PROTECTED]> wrote

>I want to run an .exe file and get the output many times.
> Can any one give me an example about how to use python to generate 
> the
> automatic process?

Given that I know that you know about loops I have to
ask what you see as the problem? Just run the process
many times in a loop? Must be more to it I suspect?

Alan G. 


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Looking for suggestions for improving chessTimer.py code

2007-10-30 Thread Kent Johnson
Dick Moores wrote:

>>> 2. I've used 1 for White player, -1 for Black player, and (-1)*player
>>> to alternate players. Is there a better way?
>>> 3. I've used a lot of variables. Too Many?
>>
>> I think it might work well to either put the player variables into 
>> lists or simple classes.
> 
> I have no confidence with classes, but how about this for lists:
> 
> playerName = ['', 'White', 'Black']
> moveCounter = [0, whiteMoveCounter, blackMoveCounter}
> remainingPlayerTime = [0, remainingWhiteTime, remainingBlackTime]

> Is that something like what you were thinking?

No. I was thinking more like this:
whitePlayer = [ 'White', whiteMoveCounter, remainingWhiteTime]
blackPlayer = [ 'Black', blackMoveCounter, remainingBlackTime]

You could even define offsets:
NAME = 0
MOVES = 1
TIME = 2

and refer to
currentPlayer[NAME]

 From here to simple classes is not a big step:

class player(object):
   def __init__(self, name, moveCounter, remainingTime):
 self.name = name
 self.moveCounter = moveCounter
 self.remainingTime = remainingTime

whitePlayer = player('White', 1, timeLimit)
blackPlayer = player('Black', 1, timeLimit)

Then playerName(player) becomes player.name and 
remainingPlayerTime(player) becomes player.remainingTime.

> Any thoughts about my number 6?: "I thought I had a way to make this 
> script useable on unix as well
> as Windows. Thus the section with the 3 classes. But it won't run on
> unix, because it was necessary to import msvcrt outside of the
> classes--it wouldn't compile otherwise, and because of the need for
> the line  'if msvcrt.kbhit():'

Why do you need to call kbhit() at all? Why not just call getch() and 
wait for the next character?

Kent
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] [Fwd: ANN: Learning Python 3rd Edition]

2007-10-30 Thread Kent Johnson
There is a new edition of one of my recommended books.

Kent

 Original Message 
Subject: ANN: Learning Python 3rd Edition
Date: Mon, 29 Oct 2007 12:28:10 -0700 (GMT-07:00)
From: [EMAIL PROTECTED]
Reply-To: [EMAIL PROTECTED], [EMAIL PROTECTED]
To: [EMAIL PROTECTED]

I'm pleased to announce the release of the 3rd Edition of
the book Learning Python.

This new edition has been updated to cover Python 2.5, and
includes numerous pointers for migrating to Python 3.0 in the
future.  Among other things, this edition has been augmented
with material on function decorators, context managers, the
new relative import syntax, generator expressions, and more.

In addition, this edition has been enhanced to be even more
of a self-paced learning resource, with new end-of-chapter
quizzes, new introductory chapters on types and syntax, and
new materials derived from recent Python training sessions.

For more details, see O'Reilly's web page:

http://www.oreilly.com/catalog/9780596513986/

O'Reilly also has a press release about the book here:

http://press.oreilly.com/pub/pr/1843

Thanks,
--Mark Lutz


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Looking for suggestions for improving chessTimer.py code

2007-10-30 Thread bob gailer
I also encourage you to jump into classes. Yes there is a learning 
curve. It is IMHO worth it.

Borrowing from Kent Johnson:

class Player(object):
   def __init__(self, name, moveCounter, remainingTime):
 self.name = name
 self.moveCounter = moveCounter
 self.remainingTime = remainingTime


Note I capitalized the class name. That's the Python "style".

Also refer to the class-based code I wrote for you a week or 2 ago. That 
should help you.



___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] perplexing error with shelve

2007-10-30 Thread Orest Kozyar
I have a program which queries an online database (Medline) for XML data.
It caches all data using shelve to avoid hitting the database too many
times.  For some reason, I keep getting a RuntimeError: maximum recursion
depth exceeded when attempting to add a certain record.  This program has
successfully handled over 40,000 records from Medline, but for whatever
reason, this particular record (PMID: 16842422) produces this error.  If I
set maximum recursion depth to a larger value such as 5,000, I get a
segfault.  

Below is a script that should reproduce the problem I am having.  Any
guidance in solving this problem would greatly be appreciated.  

Thank you,
Orest

### START SCRIPT ###
import urllib, shelve
from xml.dom import minidom

baseurl = 'http://eutils.ncbi.nlm.nih.gov/entrez/eutils/efetch.fcgi?'

params = {
'db':   'pubmed',
'retmode':  'xml',
'rettype':  'medline'
}

badkey = '16842422'
goodkey = '16842423' #or just about any other ID

data = shelve.open('data.tmp', writeback=True)

try:
params['id'] = goodkey
url = baseurl + urllib.urlencode(params)
doc = minidom.parseString(urllib.urlopen(url).read())
print 'Successfully retrieved and parsed XML document with ID %s' %
goodkey
data[goodkey] = doc
print 'Successfully shelved XML document with ID %s' % goodkey
except RuntimeError:
print 'Should not see this error message!'

try:
params['id'] = badkey
url = baseurl + urllib.urlencode(params)
doc = minidom.parseString(urllib.urlopen(url).read())
print 'Successfully retrieved and parsed XML document with ID %s' %
badkey
data[badkey] = doc
#Should not get this message!
print 'Successfully shelved XML document with ID %s' % badkey
except RuntimeError, e:
print 'Error shelving XML document with ID %s' % badkey
print e
### END SCRIPT ###

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Fwd: [pycon-tutorials] Request: Python Instruction Needed for GIS Symposium

2007-10-30 Thread Greg Lindstrom
We received this on the PyCon tutorial list.  Anyone interested??

Thanks,
--greg

-- Forwarded message --
From: [EMAIL PROTECTED] <[EMAIL PROTECTED]>
Date: Oct 30, 2007 11:18 AM
Subject: [pycon-tutorials] Request: Python Instruction Needed for GIS
Symposium
To: [EMAIL PROTECTED]


I sent out the following hoping to get a Python Instructor for an April 2008
GIS Symposium in Kansas City.  If you would be so kind, please consider
forwarding it to your list of folks who may be giving tutorials at your
Chicago Conference.   We would be glad to do a little advertising for you in
return:

I am involved with MAGIC  http://www.magicgis.org/   an organization to
encourage GIS development, sharing, cooperation, etc. and educate
practitioners in GIS.  We hold a symposium every two years in April (next is
April 2008) and provide speakers and workshops in relevant GIS subjects.
ESRI's ArcGIS software holds  the market majority and has embrace Python
language as a preferred scripting, customization language.One area we
have trouble with for our symposium is  getting instructors for Python
workshops.   The symposium is in Kansas City on April 20-24, 2008 and the
hands on computer based Python course would be held for 4 hours on Sunday
April 20th in the afternoon for about 30 beginner and intermediate
programmers.   We would like some application to ArcGIS, but just basic
Python language instruction is also needed.  The instructors and speakers as
well as the planning committee are asked to volunteer to keep the cost down
to symposium attendees.Would you be a good fit (or know anyone) as an
instructor for this Introduction to Python language course?

The Symposium Steering Committee would like to get a commitment by Nov. 2,
2008 in order to publish a preliminary program.   Sorry for the short
notice, we thought we had an instructor but do not.   We need a short 1-2
paragraph summary or outline of the workshop by then.I am willing to
help develop any materials needed.

Contact me by phone or email if you have questions or would like to
volunteer.   Thanks.

Eric Foster, Senior Transportation Planner
MoDOT, 600 NE Colbern Rd. Lee's Summit, MO 64086
(816) 622-6330


___
pycon-tutorials mailing list
[EMAIL PROTECTED]
http://mail.python.org/mailman/listinfo/pycon-tutorials
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] perplexing error with shelve REVISED

2007-10-30 Thread Bob Gailer
Orest Kozyar wrote:
> I have a program which queries an online database (Medline) for XML data.
> It caches all data using shelve to avoid hitting the database too many
> times.  For some reason, I keep getting a RuntimeError: maximum recursion
> depth exceeded when attempting to add a certain record.  
Please post the entire traceback (omitting duplicate lines). Else we 
can't (or won't try to) help.

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] perplexing error with shelve

2007-10-30 Thread bob gailer
Orest Kozyar wrote:
> I have a program which queries an online database (Medline) for XML data.
> It caches all data using shelve to avoid hitting the database too many
> times.  For some reason, I keep getting a RuntimeError: maximum recursion
> depth exceeded when attempting to add a certain record.  
Please post the entire traceback. Else we can't (or won't try to) help.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] perplexing error with shelve REVISED

2007-10-30 Thread Orest Kozyar
> Please post the entire traceback (omitting duplicate lines). 

Sorry, I should have included the traceback.  I've revised the sample script
so that it generates the traceback when run.  The sample script is at the
very bottom of this email.

### SCRIPT OUTPUT ###
[kozyar]:~$ python example.py 

Successfully retrieved and parsed XML document with ID 16842423
Successfully shelved XML document with ID 16842423
Successfully retrieved and parsed XML document with ID 16842422

Traceback (most recent call last):
  File "example.py", line 30, in 
data[badkey] = doc
  File "/usr/lib64/python2.5/shelve.py", line 123, in __setitem__
p.dump(value)
RuntimeError: maximum recursion depth exceeded

Exception exceptions.RuntimeError: 'maximum recursion depth exceeded' in
}> ignored


### START SCRIPT ###
import urllib, shelve
from xml.dom import minidom

baseurl = 'http://eutils.ncbi.nlm.nih.gov/entrez/eutils/efetch.fcgi?'

params = {
'db':   'pubmed',
'retmode':  'xml',
'rettype':  'medline'
}

badkey = '16842422'
goodkey = '16842423' #or just about any other ID

data = shelve.open('data.tmp', writeback=True)

params['id'] = goodkey
url = baseurl + urllib.urlencode(params)
doc = minidom.parseString(urllib.urlopen(url).read())
print 'Successfully retrieved and parsed XML document with ID %s' % goodkey
data[goodkey] = doc
print 'Successfully shelved XML document with ID %s' % goodkey

params['id'] = badkey
url = baseurl + urllib.urlencode(params)
doc = minidom.parseString(urllib.urlopen(url).read())
print 'Successfully retrieved and parsed XML document with ID %s' % badkey
data[badkey] = doc
#Will fail on the above line
print 'Successfully shelved XML document with ID %s' % badkey

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Looking for suggestions for improving chessTimer.py code

2007-10-30 Thread Dick Moores
At 03:49 AM 10/30/2007, Kent Johnson wrote:
>Why do you need to call kbhit() at all? Why not just call getch() 
>and wait for the next character?

Like this?: 
. The 
every-5-seconds time report doesn't report.

Compare


#!/usr/bin/env python
#coding=utf-8
# 21aForTutor.py
import time
import msvcrt
timeNow = time.time()
oldTimeNow = timeNow
while True:
 if msvcrt.kbhit():
 key = msvcrt.getch()
 if key == 'h':
 print 'Hello'
 if key == 'b':
 print 'Bye'
 if key == '\r': # Enter key
 break
 else:
 time.sleep(0.001)
 timeNow = time.time()
 if timeNow - oldTimeNow > 2:
 print "2 seconds passed"
 oldTimeNow = timeNow
==
with

===
#!/usr/bin/env python
#coding=utf-8
# 21bForTutor.py
import time
import msvcrt
timeNow = time.time()
oldTimeNow = timeNow
while True:
 #if msvcrt.kbhit():
 key = msvcrt.getch()
 if key == 'h':
 print 'Hello'
 if key == 'b':
 print 'Bye'
 if key == '\r': # Enter key
 break
 else:
 time.sleep(0.001)
 timeNow = time.time()
 if timeNow - oldTimeNow > 2:
 print "2 seconds passed"
 oldTimeNow = timeNow
=

In 21bForTutor.py the 2-second report is screwed up. Maybe you can fix it?

Dick


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] question re type()

2007-10-30 Thread Aditya Lal
On 10/29/07, Kent Johnson <[EMAIL PROTECTED]> wrote:
>
> Aditya Lal wrote:
> > or use types module
> >
> > import types
> >
> > if type(n) == types.IntType or type(n) == types.LongType :
> > blah!
>
> A few notes:
> - If you look at types.py, you find
> IntType = int
> LongType = long
>
> and so on for all the built-in types, so there is no need or advantage
> to importing types vs
>if type(n) == int
>
> - Common Python practice is to prefer the least restrictive type check
> possible. For Dick's specific case it doesn't matter, but I generally
> use isinstance() instead of checking for a specific type. The difference
> is that isinstance() is true for subtypes as well as the named type. You
> can also pass a tuple of types to isinstance() so you can say
>if isinstance(n, (int, long))
>
> Kent
>
>
I completely agree that the check " type(n) == int " is very intuitive and
simple. Its just that there are many more types that the basic ones and
'types' module provide a "consistent" way for checking for types. As an
example - consider a function :
def execMyFun( f ) :
   if type(f) == types.GeneratorType :
  return f.next()
   elif type(f) == types.FunctionType :
  return f()
   else :
  raise Exception("Invalid type for f : " + type(f) )

Here types module came to the rescue which otherwise I would have written
using exception handling. *Well ! if you have a cleaner solution do let me
know.*


BTW, isinstance is cool :) as it checks for all subclasses as well - didn't
think of that.


--
Aditya
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] perplexing error with shelve REVISED

2007-10-30 Thread Aditya Lal
On 10/31/07, Orest Kozyar <[EMAIL PROTECTED]> wrote:
>
> > Please post the entire traceback (omitting duplicate lines).
>
> Sorry, I should have included the traceback.  I've revised the sample
> script
> so that it generates the traceback when run.  The sample script is at the
> very bottom of this email.
>
> ### SCRIPT OUTPUT ###
> [kozyar]:~$ python example.py
>
> Successfully retrieved and parsed XML document with ID 16842423
> Successfully shelved XML document with ID 16842423
> Successfully retrieved and parsed XML document with ID 16842422
>
> Traceback (most recent call last):
>   File "example.py", line 30, in 
> data[badkey] = doc
>   File "/usr/lib64/python2.5/shelve.py", line 123, in __setitem__
> p.dump(value)
>
> RuntimeError: maximum recursion depth exceeded
>
> Exception exceptions.RuntimeError: 'maximum recursion depth exceeded' in
>  }> ignored
>
>
> ### START SCRIPT ###
> import urllib, shelve
> from xml.dom import minidom
>
> baseurl = 'http://eutils.ncbi.nlm.nih.gov/entrez/eutils/efetch.fcgi?'
>
> params = {
> 'db':   'pubmed',
> 'retmode':  'xml',
> 'rettype':  'medline'
> }
>
> badkey = '16842422'
> goodkey = '16842423' #or just about any other ID
>
> data = shelve.open('data.tmp', writeback=True)
>
> params['id'] = goodkey
> url = baseurl + urllib.urlencode(params)
> doc = minidom.parseString(urllib.urlopen(url).read())
> print 'Successfully retrieved and parsed XML document with ID %s' %
> goodkey
> data[goodkey] = doc
> print 'Successfully shelved XML document with ID %s' % goodkey
>
> params['id'] = badkey
> url = baseurl + urllib.urlencode(params)
> doc = minidom.parseString(urllib.urlopen(url).read())
> print 'Successfully retrieved and parsed XML document with ID %s' % badkey
> data[badkey] = doc
> #Will fail on the above line
> print 'Successfully shelved XML document with ID %s' % badkey
>
>
The program worked fine for me (python 2.5.1 - stackless on Mac OSX). Can
you provide details about your platform (python version, OS, etc.) ?
-- 
Aditya
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor