Re: [Tutor] Problem with msvcrt

2007-11-01 Thread Alan Gauld
"Ricardo Aráoz" <[EMAIL PROTECTED]> wrote

> if msvcrt.kbhit() :
> key = msvcrt.getch()
> if key == 'h' :
> print 'Hello'
>
> This is XP with 2.5 in Idle. Any ideas?

IDLE is a GUI app running under Tkinters event loop.
I doubt if msvcrt works under Tkinter.
Try running your code in a DOS box.

Any solution that detects keyboard input will be
very sensitive to the operational environment.
Running in a GUI (any GUI) will cause keyboard
events that are handled by the GUI toolkit.
msvcrt is designed to catch keyboasrd events
within a DOS environment.

See the event handling page of my tutor for how
to detect keypresses within Tkinter.

HTH,

-- 
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld



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


Re: [Tutor] Getting single values of a tuple & storing them

2007-11-01 Thread Alan Gauld

"Trey Keown" <[EMAIL PROTECTED]> wrote

> I was wondering, how could I get each value inside of a tuple, say 
> it's
> (2,4) .
> The only value I really need is the second one (the tuple will 
> always have
> only two values.

Tuples are like any other Python collection or sequence.
You can access by indexing into the tuple:

second = tup[1]   # zero based index

you can also iterate over them:

for index, item in enumerate(tup):
   print index, item
   if index = 1: second = item

And additionally collections can be 'unpacked':

one, two, three = (1,2,3)

These techniques also work with lists and strings.

So pick the method that suits you best.

HTH,

-- 
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld 


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


Re: [Tutor] sorting variables

2007-11-01 Thread Evert Rol
> i was thinking of doing something like
>
>objSprites = pygame.sprite.OrderedUpdates((var1,
> var2, var3).sort)
>
> but i don't think thats gunna work

It won't indeed, because of 3 things:
1. you're trying to sort a tuple. Tuples don't have a sort() method,  
use a list instead
2. sort doesn't return! (well, it returns None in a way.) It's a  
thing that has caught me once too often. You'll need to do the  
sorting before using the sorted list again
3. you cannot insert a list (or tuple) as argument to a function and  
expect it to be automatically expanded to several arguments. Use the  
'*' in front of the list to expand (tuple won't work).

So what should work is:
args = [var1, var2, var3]
args.sort()
objSprites = pygame.sprite.OrderedUpdates(*args)

It's a few more lines, but that's the way it works. Hope that that  
helps.

Cheers,

   Evert

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


Re: [Tutor] Regular Expression help - parsing AppleScript Lists as Strings

2007-11-01 Thread Kent Johnson
Andrew Wu wrote:

>pattern3 = '''
>   ^{
>   (
>   %s
>   | {%s}   # Possible to have 1 level of nested lists
>   ,?)* # Items are comma-delimited, except for the last item
>   }$
>''' % (pattern2, pattern2)

The above doesn't allow comma after the first instance of pattern2 and 
it doesn't allow space after either instance. Here is a version that 
passes your tests:

pattern3 = '''
   ^{
   (
   (%s
   | {%s})   # Possible to have 1 level of nested lists
   ,?\s*)* # Items are comma-delimited, except for the last item
   }$
''' % (pattern2, pattern2)

You might want to look at doing this with pyparsing, I think it will 
make it easier to get the data out vs just recognizing the correct pattern.

Kent

PS Please post in plain text, not HTML.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Regular Expression help - parsing AppleScript Lists as Strings

2007-11-01 Thread Kent Johnson
Kent Johnson wrote:
> You might want to look at doing this with pyparsing, I think it will 
> make it easier to get the data out vs just recognizing the correct pattern.

Here is a pyparsing version that correctly recognizes all of your 
patterns and returns a (possibly nested) Python list in case of a match.

Note that this version will parse lists that are nested arbitrarily 
deeply. If you don't want that you will have to define two kinds of 
lists, a singly-nested list and a non-nested list.

Kent

from pyparsing import *

List = Forward()
T = Literal('true').setParseAction( lambda s,l,t: [ True ] )
F = Literal('false').setParseAction( lambda s,l,t: [ False ] )
String = QuotedString('"')
Number = Word(nums).setParseAction( lambda s,l,t: [ int(t[0]) ] )
List << Literal('{').suppress() + 
delimitedList(T|F|String|Number|Group(List)) + Literal('}').suppress()

def IsASList(s):
# AppleScript lists are bracked by curly braces with items separate 
by commas
# Each item is an alphanumeric label(?) or a string enclosed by
# double quotes or a list itself
# e.g. {2, True, "hello"}
try:
parsed = List.parseString(s)
return parsed.asList()
except Exception, e:
return None

sample_strs = [
'{}',  # Empty list
'{a}', # Should not match
'{a, b, c}', # Should not match
'{"hello"}',
'{"hello", "kitty"}',
'{true}',
'{false}',
'{true, false}',
'{9}',
'{9,10, 11}',
'{93214, true, false, "hello", "kitty"}',
'{{1, 2, 3}}',  # This matches
'{{1, 2, "cat"}, 1}',  # This matches

 # These don't match:
'{{1,2,3},1,{4,5,6},2}',
'{1, {2, 3, 4}, 3}',
'{{1, 2, 3}, {4, 5, 6}, 1}',
'{1, {1, 2, 3}}',  # Should match but doesn't
'{93214, true, false, "hello", "kitty", {1, 2, 3}}',  # Should match 
but doesn't
'{label: "hello", value: false, num: 2}',  # AppleScript dictionary 
- should not match
]

for sample in sample_strs:
result = IsASList(sample)
print 'Is AppleScript List:  %s;   String:  %s' % (bool(result), sample)
if result:
print result
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Regular Expression help - parsing AppleScript Lists as Strings

2007-11-01 Thread Andrew Wu
Ah - thanks for the correction!  I missed the extra grouping and the
extra spacing ... doh!  Sorry about the HTML-formatted e-mail ...

Thanks also for the pyparsing variant as well - I didn't know the
module existed before!



Andrew

On 11/1/07, Kent Johnson <[EMAIL PROTECTED]> wrote:
> Andrew Wu wrote:
>
> >pattern3 = '''
> >   ^{
> >   (
> >   %s
> >   | {%s}   # Possible to have 1 level of nested lists
> >   ,?)* # Items are comma-delimited, except for the last item
> >   }$
> >''' % (pattern2, pattern2)
>
> The above doesn't allow comma after the first instance of pattern2 and
> it doesn't allow space after either instance. Here is a version that
> passes your tests:
>
> pattern3 = '''
>^{
>(
>(%s
>| {%s})   # Possible to have 1 level of nested lists
>,?\s*)* # Items are comma-delimited, except for the last item
>}$
> ''' % (pattern2, pattern2)
>
> You might want to look at doing this with pyparsing, I think it will
> make it easier to get the data out vs just recognizing the correct pattern.
>
> Kent
>
> PS Please post in plain text, not HTML.
>
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Turning multiple Popen calls into a function

2007-11-01 Thread jay
Hello,

If I have multiple Popen calls I need to make, how can I turn these into a
function?

from subprocess import Popen, PIPE

p1 = Popen(['ls', '-l', '-a', '/etc'],stdout=PIPE)
p2 = Popen(['grep', 'hosts'], stdin=p1.stdout, stdout=PIPE)
p3 = Popen(['awk', '{print $1}'], stdin=p2.stdout, stdout=PIPE)
output = p3.communicate()[0]

p1 = Popen(['ps', '-ef'], stdout=PIPE)
p2 = Popen(['grep', '-v', '2348'], stdin=p1.stdout, stdout=PIPE)
output = p2.communicate()[0]

I would be sending an arbitrary number of PIPES with each function call.

I'm a little stumped as to how to handle the variables.  If I have an
arbitrary number of PIPES, how do I declare my variables (p1, p2, p3,
etc...) ahead of time in the function??

Thanks for any suggestions!

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


Re: [Tutor] Turning multiple Popen calls into a function

2007-11-01 Thread Eric Walstad
Hi Jay...
jay wrote:
...
> I would be sending an arbitrary number of PIPES with each function call.
> 
> I'm a little stumped as to how to handle the variables.  If I have an 
> arbitrary number of PIPES, how do I declare my variables (p1, p2, p3, 
> etc...) ahead of time in the function??
> 
> Thanks for any suggestions!


 >>> def foo(bar, *fizz, **bang):
... print "bar:", bar
... print "fizz:", fizz
... print "bang:", bang
...

 >>> foo('and now for something completely different')
bar: and now for something completely different
fizz: ()
bang: {}

 >>> foo('hello', 'and', 'cows', 'eat', 'grass')
bar: hello
fizz: ('and', 'cows', 'eat', 'grass')
bang: {}

 >>> foo('hello', 'and', 'cows', 'eat', 'grass', greeting='hello', 
location='world')
bar: hello
fizz: ('and', 'cows', 'eat', 'grass')
bang: {'greeting': 'hello', 'location': 'world'}

 >>> a_tuple = ('and', 'cows', 'eat', 'grass')
 >>> a_dict = dict(greeting='hello', location='world')
 >>> foo('hello', *a_tuple, **a_dict)
bar: hello
fizz: ('and', 'cows', 'eat', 'grass')
bang: {'location': 'world', 'greeting': 'hello'}


Or, just pass the pipes in as an iterable to your function:
def pipe_handler(pipes):
 for n, pipe in enumerate(pipes):
 print "Pipe %d: '%s'" % (n, pipe)

pipes = []
pipes.append(some_pipe)
...
pipe_handler(pipes)

See also:


I hope that helps,

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


Re: [Tutor] Turning multiple Popen calls into a function

2007-11-01 Thread Eric Brunson
jay wrote:
> Hello,
>
> If I have multiple Popen calls I need to make, how can I turn these 
> into a function?

Since you're only interested in the output of the last command on the 
pipeline, I don't see a reason to keep track of them all.  I'd do 
something like this:

def pipeline( *commandlist ):
last = None
for command in commandlist:
last = Popen( command,
  stdin=last.stdout if last else None,
  stdout=PIPE )

return last.communicate()[0]

print pipeline( ("ls", "-la", "/etc"),
("grep", "hosts"),
("awk", "{print $1}") )

returns:
lrwxrwxrwx



Hope that helps,
e.

> from subprocess import Popen, PIPE
>
> p1 = Popen(['ls', '-l', '-a', '/etc'],stdout=PIPE)
> p2 = Popen(['grep', 'hosts'], stdin=p1.stdout, stdout=PIPE)
> p3 = Popen(['awk', '{print $1}'], stdin=p2.stdout, stdout=PIPE)
> output = p3.communicate()[0]
>
> p1 = Popen(['ps', '-ef'], stdout=PIPE)
> p2 = Popen(['grep', '-v', '2348'], stdin=p1.stdout, stdout=PIPE)
> output = p2.communicate()[0]
>
> I would be sending an arbitrary number of PIPES with each function call.
>
> I'm a little stumped as to how to handle the variables.  If I have an 
> arbitrary number of PIPES, how do I declare my variables (p1, p2, p3, 
> etc...) ahead of time in the function??
>
> Thanks for any suggestions!
>
> Jay
> 
>
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>   

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


[Tutor] sorting / outputting varibales

2007-11-01 Thread ted b
If i input the following in python:
   var1 = 7
   var2 = 9
   var3 = 5
   args = [var1, var2, var3]
   args.sort()

then if if type:

   args

the output is

   [5, 7, 9]

but i want the output to be

   [var3, var1, var2]

is there a function that will output the variable
names in the order they have been sorted instead of
the variable contents themselves?
thanks

__
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Turning multiple Popen calls into a function

2007-11-01 Thread Alan Gauld

"jay" <[EMAIL PROTECTED]> wrote


> I'm a little stumped as to how to handle the variables.  If I have 
> an
> arbitrary number of PIPES, how do I declare my variables (p1, p2, 
> p3,
> etc...) ahead of time in the function??

How about passing a list of pipes?
Then you can access them as pipes[0],pipes[1], etc?

HTH,

Alan G 


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


Re: [Tutor] sorting / outputting varibales

2007-11-01 Thread Evert Rol
> If i input the following in python:
>var1 = 7
>var2 = 9
>var3 = 5
>args = [var1, var2, var3]
>args.sort()
>
> then if if type:
>
>args
>
> the output is
>
>[5, 7, 9]
>
> but i want the output to be
>
>[var3, var1, var2]
>
> is there a function that will output the variable
> names in the order they have been sorted instead of
> the variable contents themselves?


Hm, I don't know directly how to get the name of a variable. I'd  
hoped there was a private method (var3.), but looks  
like there isn't it.

Perhaps you can do something with a dictionary, eval and/or map? Not  
sure how that would work out, you'll have to play around a bit to get  
it working.

But I'm not really sure why you'd want to pass the variable names to  
a function; in the end, you're going to use the actual values anyway,  
not their names, right?


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


Re: [Tutor] Turning multiple Popen calls into a function

2007-11-01 Thread jay
Eric and Eric :-)

Thanks both for the suggestions!  I learned from each!
I believe the small function will work for me.  Its simple since I don't
have to track each pipe I open with a variable.  I had no idea I could do
that nifty if/then for the stdin, that makes it so easy. :-)

Thanks again!

Jay

On 11/1/07, Eric Brunson <[EMAIL PROTECTED]> wrote:
>
> jay wrote:
> > Hello,
> >
> > If I have multiple Popen calls I need to make, how can I turn these
> > into a function?
>
> Since you're only interested in the output of the last command on the
> pipeline, I don't see a reason to keep track of them all.  I'd do
> something like this:
>
> def pipeline( *commandlist ):
> last = None
> for command in commandlist:
> last = Popen( command,
>   stdin=last.stdout if last else None,
>   stdout=PIPE )
>
> return last.communicate()[0]
>
> print pipeline( ("ls", "-la", "/etc"),
> ("grep", "hosts"),
> ("awk", "{print $1}") )
>
> returns:
> lrwxrwxrwx
>
>
>
> Hope that helps,
> e.
>
> > from subprocess import Popen, PIPE
> >
> > p1 = Popen(['ls', '-l', '-a', '/etc'],stdout=PIPE)
> > p2 = Popen(['grep', 'hosts'], stdin=p1.stdout, stdout=PIPE)
> > p3 = Popen(['awk', '{print $1}'], stdin=p2.stdout, stdout=PIPE)
> > output = p3.communicate()[0]
> >
> > p1 = Popen(['ps', '-ef'], stdout=PIPE)
> > p2 = Popen(['grep', '-v', '2348'], stdin=p1.stdout, stdout=PIPE)
> > output = p2.communicate()[0]
> >
> > I would be sending an arbitrary number of PIPES with each function call.
> >
> > I'm a little stumped as to how to handle the variables.  If I have an
> > arbitrary number of PIPES, how do I declare my variables (p1, p2, p3,
> > etc...) ahead of time in the function??
> >
> > Thanks for any suggestions!
> >
> > Jay
> > 
> >
> > ___
> > Tutor maillist  -  Tutor@python.org
> > http://mail.python.org/mailman/listinfo/tutor
> >
>
>
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Automating operations... os module, mysql operations, more...

2007-11-01 Thread John
I should begin by explaining I am not a sysadmin, I'm merely one trying to
use the right tool for the right job, and for my job I believe Python to be
that tool. Here is an outline of what I wish to accomplish, pointers to
modules, tools of interest, etc. would be greatly appreciated... Below each
number I propose the modules I've found, but please let me know if there are
better ones.. or others I should be aware of. Also, any good resources
regarding #2?? Thanks in advance!!

1) Maintain a mysql database which contains information, as well as
'control' variables, pointers to directories, etc.  for various shell
scripts / fortran code.
->pyMySQL

2) Query various servers to see if there is 'idyl' time/resources... send a
job to the server if it's available...


 3) Use python to check the file system for existing files (glob?) , decide
which 'jobs' need to be run, etc.
->os, sys, glob

4) Use python as glue between the db and the shell /fortran routines. For
this I picture python querying the db, 'writing' new CONTROL files, and
using subprocess to run the scripts / fortran.
->sys, os, subprocess

5) Update the db with information regarding the status of the various
scripts / fortran.
->pyMySQL

6) Have a portion of the information available to a plone site for dynamic
html generation.
->ZMySQLDBConn
->Z SQL Methods



-- 
Configuration
``
Plone 2.5.3-final,
CMF-1.6.4,
Zope (Zope 2.9.7-final, python 2.4.4, linux2),
Five 1.4.1,
Python 2.4.4 (#1, Jul 3 2007, 22:58:17) [GCC 4.1.1 20070105 (Red Hat
4.1.1-51)],
PIL 1.1.6
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] sorting / outputting varibales

2007-11-01 Thread Kent Johnson
Evert Rol wrote:
> Hm, I don't know directly how to get the name of a variable. I'd  
> hoped there was a private method (var3.), but looks  
> like there isn't it.

No, the mapping from names to values is one way. It's pretty easy to 
have a value associated with no name, or more than one name, so the 
question is not really answerable in the general case.

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


Re: [Tutor] sorting / outputting varibales

2007-11-01 Thread Kent Johnson
ted b wrote:
> If i input the following in python:
>var1 = 7
>var2 = 9
>var3 = 5
>args = [var1, var2, var3]
>args.sort()
> 
> then if if type:
> 
>args
> 
> the output is
> 
>[5, 7, 9]
> 
> but i want the output to be
> 
>[var3, var1, var2]
> 
> is there a function that will output the variable
> names in the order they have been sorted instead of
> the variable contents themselves?

Questions like this usually indicate that the questioner is on the wrong 
track. What are you trying to do? Probably it can be better done with a 
list or dictionary of values.

Is this a followup to your earlier "sorting variables" question? Evert 
showed you how to do that, is there a problem?

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


[Tutor] dictionary append

2007-11-01 Thread Dinesh B Vadhia
Hello!  I'm creating a dictionary called keywords that has multiple entries 
each with a variable list of values eg.

keywords[1] = [1, 4, 6, 3]
keywords[2] = [67,2]
keywords[3] = [2, 8, 5, 66, 3, 23]
etc.

The keys and respective values (both are integers) are read in from a file.  
For each key, the value is append'ed until the next key.  Here is the code.

.
>>> keywords = {}
>>> with open("x.txt", "r") as f:
k=0
for line in f.readlines():
keywords[k], second = map(int, line.split())
keywords[k].append(second)
if keywords[k] != k:
k=k+1
   
Traceback (most recent call last):
  File "", line 5, in 
keywords[k].append(second)
AttributeError: 'int' object has no attribute 'append'
.

Any idea why I get this error?

Dinesh


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


Re: [Tutor] sorting / outputting varibales

2007-11-01 Thread Evert Rol
> Thanks, i think a dictionary may  be the way to go
>
>> in the end, you're going to use the
>> actual values anyway,
>> not their names, right?
>
> yeah, you're right, the way i was thinking about doing
> it probably won't work. The way I am doing it now is
> actually using attibutes of the var's themselves to
> determine how they get used in the function:
>
>   if var1.value > var2.value > var3.value:
>  objSprites = pygame.sprite.OrderedUpdates
> (var1, var2, var3)
>   elif var1.value > var3.value > var2.value:
>  objSprites = pygame.sprite.OrderedUpdates
> (var1, var3, var2)
>   elif var2.value > var3.value > var1.value:
>  objSprites = pygame.sprite.OrderedUpdates
> (var2, var3, var1)
>   elif var2.value > var1.value > var3.value:
>  objSprites = pygame.sprite.OrderedUpdates
> (var2, var1, var3)
>   elif var3.value > var1.value > var2.value:
>  objSprites = pygame.sprite.OrderedUpdates
> (var3, var1, var2)
>   elif var3.value > var2.value > var1.value:
>  objSprites = pygame.sprite.OrderedUpdates
> (var3, var2, var1)



Looks like you forgot to give one essential part of information in  
your first email: varX aren't 'simple' integers or floats, but more  
complex types. Not exactly what you gave in your example. Please do  
give all the information if you're not sure what you're asking about  
(it may even put people off in follow-up answering). Oh, and please  
reply to the whole list, not just me: it'll enlighten other people as  
well.

Anyway, with this, something like this (indeed a dictionary) would do  
it:

vars = [var1, var2, var3]
dictvars = {}
for var in vars:
   dictvars[var.value] = var
keys = dictvars.keys()
keys.sort()
sortedvars = [dictvars[key] for key in keys]
objSprites = pygame.sprite.OrderedUpdates(*sortedvars)


Arguably just as lengthly, but more consistent. Depending on the type  
of 'varX' variable you're using, you might be able to simplify things  
further (by using a dictionary instead of a home-made object, which I  
assume the 'varX' variables are).

Probably putting up a solution here isn't really "tutoring", but I'm  
curious if anyone else on the list has a cleaner solution than this.


Cheers,

   Evert

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


Re: [Tutor] sorting / outputting varibales

2007-11-01 Thread Kent Johnson
ted b wrote:

> Here's the code i am using, but its a lot of if /
> thens and i'm trying to find a better way:
> 
>   if var1.value > var2.value > var3.value: 
>  objSprites = pygame.sprite.OrderedUpdates
> (var1, var2, var3)

Did you see Evert's reply to your original question? It was pretty close 
to the mark. Though you had not said anything about the .value 
attributes until now.

Try this:
from operator import attrgetter
vars = [ var1, var2, var3 ]
vars.sort(key=attrgetter('value'))
objSprites = pygame.sprite.OrderedUpdates(*vars)

This will sort the vars list by the value attribute of each list item, 
then pass the list as the parameter list to OrderedUpdates().

Kent

PS Please use Reply All to reply to the list.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] sorting / outputting varibales

2007-11-01 Thread ted b
Thanks Kent, and Evert, and everyone,
I really appreciate the advice on etiqutte and all
your help. I will think through my questions much more
thoroughly before any further inquiries and will
'reply to all' as advised.
--- Kent Johnson <[EMAIL PROTECTED]> wrote:

> ted b wrote:
> 
> > Here's the code i am using, but its a lot of if /
> > thens and i'm trying to find a better way:
> > 
> >   if var1.value > var2.value > var3.value: 
> >  objSprites = pygame.sprite.OrderedUpdates
> > (var1, var2, var3)
> 
> Did you see Evert's reply to your original question?
> It was pretty close 
> to the mark. Though you had not said anything about
> the .value 
> attributes until now.
> 
> Try this:
> from operator import attrgetter
> vars = [ var1, var2, var3 ]
> vars.sort(key=attrgetter('value'))
> objSprites = pygame.sprite.OrderedUpdates(*vars)
> 
> This will sort the vars list by the value attribute
> of each list item, 
> then pass the list as the parameter list to
> OrderedUpdates().
> 
> Kent
> 
> PS Please use Reply All to reply to the list.
> 


__
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Automating operations... os module, mysql operations, more...

2007-11-01 Thread Michael Langford
>
> 1) Maintain a mysql database which contains information, as well as
> 'control' variables, pointers to directories, etc.  for various shell
> scripts / fortran code. ->pyMySQL
>

You're on the right track


> 2) Query various servers to see if there is 'idyl' time/resources... send
> a job to the server if it's available...
>

This and 3 can be helped out with ipython

3) Use python to check the file system for existing files (glob?) , decide
> which 'jobs' need to be run, etc.
> ->os, sys, glob
>
> 4) Use python as glue between the db and the shell /fortran routines. For
> this I picture python querying the db, 'writing' new CONTROL files, and
> using subprocess to run the scripts / fortran.
> ->sys, os, subprocess
>

I'd imagine SWIG may make it so you can actually call it if you make your
fortran compiler spit out a C compatible library. I know that one of the
Fortran95 compilers does this.

5) Update the db with information regarding the status of the various
scripts / fortran.
>
> ->pyMySQL
>
> 6) Have a portion of the information available to a plone site for dynamic
> html generation.
> ->ZMySQLDBConn
> ->Z SQL Methods
>
>
>
> --
> Configuration
> ``
> Plone 2.5.3-final,
> CMF-1.6.4,
> Zope (Zope 2.9.7-final, python 2.4.4, linux2),
> Five 1.4.1,
> Python 2.4.4 (#1, Jul 3 2007, 22:58:17) [GCC 4.1.1 20070105 (Red Hat
> 4.1.1-51)],
> PIL 1.1.6
>
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>
>


-- 
Michael Langford
Phone: 404-386-0495
Consulting: http://www.TierOneDesign.com/
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Turning multiple Popen calls into a function

2007-11-01 Thread Alan Gauld

"Eric Brunson" <[EMAIL PROTECTED]> wrote 

>last = Popen( command,
>  stdin=last.stdout if last else None,
>  stdout=PIPE )

Where does the if/else syntax come from? It doesn't 
seem to work on my Python 2.4. Is it a new feature in 2.5?

Alan G.

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


Re: [Tutor] Automating operations... os module, mysql operations, more...

2007-11-01 Thread John
>
>
>
>
> > 2) Query various servers to see if there is 'idyl' time/resources...
> > send a job to the server if it's available...
> >
>
> This and 3 can be helped out with ipython
>
>
>
> >  3) Use python to check the file system for existing files (glob?) ,
> > decide which 'jobs' need to be run, etc.
> > ->os, sys, glob
> >
>
I'm not sure I follow this? ipython is a command line interpreter, no? How
ill that help me 'automate' tasks...

>
> > 4) Use python as glue between the db and the shell /fortran routines.
> > For this I picture python querying the db, 'writing' new CONTROL files, and
> > using subprocess to run the scripts / fortran.
> > ->sys, os, subprocess
> >
>
> I'd imagine SWIG may make it so you can actually call it if you make your
> fortran compiler spit out a C compatible library. I know that one of the
> Fortran95 compilers does this.
>

Seems this would require a bit more than is required... just running the
shell scripts, which run the fortran is all I'm desiring to accomplish today
;)


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


Re: [Tutor] Turning multiple Popen calls into a function

2007-11-01 Thread Jeff Younker
On Nov 1, 2007, at 3:04 PM, Alan Gauld wrote:

> Where does the if/else syntax come from? It doesn't
> seem to work on my Python 2.4. Is it a new feature in 2.5?


Yes, it's a new feature in 2.5.

- Jeff Younker - [EMAIL PROTECTED]


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


Re: [Tutor] dictionary append

2007-11-01 Thread Alan Gauld

"Dinesh B Vadhia" <[EMAIL PROTECTED]> wrote

keywords[1] = [1, 4, 6, 3]
keywords[2] = [67,2]
keywords[3] = [2, 8, 5, 66, 3, 23]
etc.

The keys and respective values (both are integers) are read
in from a file.  For each key, the value is append'ed until
the next key.  Here is the code.

.
>>> keywords = {}
>>> with open("x.txt", "r") as f:

You don;t need the with statement for this, just do

for line in open('x.txt'):

keywords[k], second = map(int, line.split())

So keywords[k] and second are both ints

keywords[k].append(second)

But you can't append to an int.
Try creating a temp value first:

first, second = map(int, line.split())
keywords[k] = [first]  # creates a list value instead of 
an int
keywords[k].append(second)

HTH,

-- 
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld 


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


Re: [Tutor] dictionary append

2007-11-01 Thread bob gailer
Dinesh B Vadhia wrote:
> Hello!  I'm creating a dictionary called keywords that has multiple 
> entries each with a variable list of values eg.
>  
> keywords[1] = [1, 4, 6, 3]
> keywords[2] = [67,2]
> keywords[3] = [2, 8, 5, 66, 3, 23]
> etc.
>  
> The keys and respective values (both are integers) are read in from a 
> file. 
Please show us the lines in the file that led to that outcome.

Your program starts with k == 0. That suggests there should be a 
keywords[0] entry.
> For each key, the value is append'ed until the next key.  Here is the 
> code.
>  
> ..
> >>> keywords = {}
> >>> with open("x.txt", "r") as f:
> k=0
> for line in f.readlines():
> keywords[k], second = map(int, line.split())
> keywords[k].append(second)
>if keywords[k] != k:
>k=k+1
>
> Traceback (most recent call last):
>   File "", line 5, in 
> keywords[k].append(second)
> AttributeError: 'int' object has no attribute 'append'
> ..
>  
> Any idea why I get this error?
Yes, and I'm surprised you have no idea! I say surprised, because a long 
time ago I learned to "walk through" my code line by line and write down 
what was happening. If you do that you should note that:

keywords[k], second = map(int, line.split())

creates 2 integer values, and assigns the first to keywords[0]. Then you 
try to use:

keywords[k].append(second)

to append the second integer to the first. As the message says, "'int' 
object has no attribute 'append'". append requires a list-like object.

Since your results seem incorrect anyway it is hard to diagnose. So 
please show us the input.

Also note that if you fix it so each dict entry is a list-like object, 
then keywords[k] != k will always be false, as that is comparing a list 
to an int.

Alan suggested creating a list using [first], but that only works for 
the first occurrence of each new key.

If this were my program, I'd guess that the input file looks like key, 
value pairs:

1 1
1 4
1 6
1 3
2 67
2 2
3 2
3 8
3 5
3 66
3 3
3 23

Is that accurate?

If so my program would be:

keywords = {}
for line in file("x.txt", "r"):
key, value = map(int, line.split())
if key not in keywords:
   keywords[key] = []
keywords[key].append(value)

 HTH

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


Re: [Tutor] Automating operations... os module, mysql operations, more...

2007-11-01 Thread Jeff Younker

On Nov 1, 2007, at 12:15 PM, John wrote:

1) Maintain a mysql database which contains information, as well as  
'control' variables, pointers to directories, etc.  for various  
shell scripts / fortran code.

->pyMySQL


Look at an object relational mapper like SQLObject or SQLAlchemy.  They
make the talking to the database much easier.

6) Have a portion of the information available to a plone site for  
dynamic html generation.

->ZMySQLDBConn
->Z SQL Methods


Zope might be overkill.  Something like Turbogears might be better
if you're just looking for reporting.  (It will do a lot more, but  
it's really

easy to get started to with turbogears.)

- Jeff Younker - [EMAIL PROTECTED]



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


Re: [Tutor] Problem with msvcrt

2007-11-01 Thread Ricardo Aráoz
Alan Gauld wrote:
> "Ricardo Aráoz" <[EMAIL PROTECTED]> wrote
> 
>> if msvcrt.kbhit() :
>> key = msvcrt.getch()
>> if key == 'h' :
>> print 'Hello'
>>
>> This is XP with 2.5 in Idle. Any ideas?
> 
> IDLE is a GUI app running under Tkinters event loop.
> I doubt if msvcrt works under Tkinter.
> Try running your code in a DOS box.
> 
> Any solution that detects keyboard input will be
> very sensitive to the operational environment.
> Running in a GUI (any GUI) will cause keyboard
> events that are handled by the GUI toolkit.
> msvcrt is designed to catch keyboasrd events
> within a DOS environment.
> 
> See the event handling page of my tutor for how
> to detect keypresses within Tkinter.
> 
> HTH,
> 

Thanks Alan.

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


Re: [Tutor] dictionary append

2007-11-01 Thread Kent Johnson
bob gailer wrote:
> if key not in keywords:
>keywords[key] = []
> keywords[key].append(value)

This can be written more succinctly as
   keywords.setdefault(key, []).append(value)

or in Python 2.5:
from collections import defaultdict
keywords = defaultdict(list)
...
   keywords[key].append(value)

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


Re: [Tutor] Build exe on Vista, have it run on XP?

2007-11-01 Thread O.R.Senthil Kumaran
* Michael Langford <[EMAIL PROTECTED]> [2007-11-01 01:15:31]:

> That's not really a working solution. My available development platform is a
> Vista machine. I don't have an available XP platform. XP built exes run fine
> on Vista, just not vice versa.

Then the best possible way would be to ask py2exe mailling list, if they have 
identified any solution for the backward compatiblities issues of VISTA exes on 
XP.

Thanks,
Senthil

-- 
O.R.Senthil Kumaran
http://uthcode.sarovar.org
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor