[Tutor] Animating changes with Numpy arrays

2007-06-26 Thread Andy Cheesman
Hi people

Thanks to people's most excellent help with the Automatic generation of
an "all possible combinations" array. I've got a beta version of my
program working.
However, I was wondering if there is an "easy" way to animate changes
between arrays and to show the number of the loop which the program is
running i.e a print out which changes at a controlable rate I've looked
for ascii art stuff with python but there are no pointers.
At the moment, I'm review the changes by walking through a screen print
out of all changes making pattern changes hard to spot but as this is
just a minor debuging tool, I don't want to spend weeks coding it in!

Thanks
Andy

Examples

from-=> to
1 1 1   1 2 1
1 2 1   1 1 1
1 1 1   1 1 1
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Animating changes with Numpy arrays

2007-06-26 Thread Luke Paireepinart
Andy Cheesman wrote:
> Hi people
>
> Thanks to people's most excellent help with the Automatic generation of
> an "all possible combinations" array. I've got a beta version of my
> program working.
> However, I was wondering if there is an "easy" way to animate changes
> between arrays and to show the number of the loop which the program is
> running i.e a print out which changes at a controlable rate I've looked
> for ascii art stuff with python but there are no pointers.
> At the moment, I'm review the changes by walking through a screen print
> out of all changes making pattern changes hard to spot but as this is
> just a minor debuging tool, I don't want to spend weeks coding it in!
>   
Andy -
Look into the Console module if you're on Windows
and Pycurses if you're on a Unix variant.
These let you have a character map of the terminal,
versus a top-to-bottom line printing mode.
HTH,
-Luke

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


[Tutor] Beginner Game: Rock, Paper, Scissors

2007-06-26 Thread Johnny Jelinek

Hello, I'm a beginner to python; I wanted to make a fun little game, so I
started with something simple: Rock, Paper, Scissors.

The program I made satisfies me, but I want to add graphics.  I installed
pygame, and have some svg's that I want to render into graphics.  I
installed cairo, but then realized that it is only used to draw svg's and
other graphics into files, not render them on the screen.  Any ideas how to
start turning this into a graphical game?  Feel free to add any other
suggestions as well :D:D:D! Thanks.

Here is the source code:

#! /usr/bin/env python
##Rock Paper Scissors, By: John Jelinek IV##
##GLOBAL VARS###
import random
import os
random = random.Random()
##FUNCTIONS#
def clear():# Clear's the screen
   os.system("clear")
def rounds():# Asks how many rounds
   rnum = input("How many rounds?!?: ")
   while rnum == 1 or rnum%2 == 0 or rnum <= -1:
   print "Must be an odd number of rounds and more than 1, try again!"
   rnum = input("How many rounds?!?: ")
   return rnum
def rps(rounds):# Begins the real game

   win = 0
   lose = 0
   tie = 0

   for i in range(1,rounds+1):
   decision = ('rock', 'paper', 'scissors')
   player = ''
   ai = random.choice(decision)

   while player != 'r' and player != 'p' and player != 's':
   print "\nROUND ", + i
   player = raw_input('What will YOU choose? (r)ock, (p)aper,
(s)cissors: ')

   if player == 'r':
   player = 'rock'
   elif player == 'p':
   player = 'paper'
   else:
   player = 'scissors'

   print "="
   print "you chose " + player.upper(),
   print ":: I choose " + ai.upper()

   if player.upper() == 'ROCK' and ai.upper() == 'SCISSORS':
   win += 1
   print "You WIN by CRUSHING those SCISSORS!"
   elif player.upper() == 'PAPER' and ai.upper() == 'ROCK':
   win += 1
   print "You WIN by WRAPPING that ROCK!"
   elif player.upper() == 'SCISSORS' and ai.upper() == 'PAPER':
   win += 1
   print "You WIN by CUTTING that PAPER!"
   elif player.upper() == ai.upper():
   tie += 1
   print "YOU TIE!"
   else:
   lose += 1
   print "YOU LOSE!"

   print "\nRounds Won: ", + win
   print "\nRounds Lost: ", + lose
   print "\nRounds Tied: ", + tie
##BEGIN PROGRAM#
clear()
print "Welcome to ROCK PAPER SCISSORS!  PREPARE FOR BATTEL!!\n\n"
rounds = rounds()
rps(rounds)
##END PROGRAM###
print "\n\nThanks for playing!\n"
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Fastest way to iterate through a file

2007-06-26 Thread Robert Hicks
Kent Johnson wrote:
> Robert Hicks wrote:
>> I have a script at work where I have a list of id numbers and I am doing a:
>>
>> for line in ehFile:
> 
> That is fine
> 
>>  for id in line:
> 
> I don't know what this is for - line is a string, iterating it will give 
> you every character is the line.
>>  
>> I am then going through that file and finding the line the id is on and 
>> printing the next line out. It takes a few seconds to see the output to 
>> the screen (the Perl version whips by) which got me to thinking I could 
>> be doing it faster (as I want to move it from Perl to Python).
>>
>> If you need all the code I can post that tomorrow or I can try any ideas 
>> posted to this.
> 
> A bit more code would help.
> 

This is the loop code:

for line in f2:
 for id in idList:
 if id in line:
 print "%s: %s" % (id, f2.next())
 found = "%s: %s" % (id, f2.next())
 f3.write(found)


I have an list, idList[], that contains a list of id numbers. That code 
will loop the the f2 file and for lines that have an id on it it will 
print the "next" line (so I can see what it is doing) and write it to a 
file. I will turn off that screen print after I get it going the way I 
want it to.

Robert

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


Re: [Tutor] Fastest way to iterate through a file

2007-06-26 Thread Kent Johnson
Robert Hicks wrote:
> This is the loop code:
> 
> for line in f2:
>  for id in idList:
>  if id in line:
>  print "%s: %s" % (id, f2.next())
>  found = "%s: %s" % (id, f2.next())
>  f3.write(found)
> 
> 
> I have an list, idList[], that contains a list of id numbers. That code 
> will loop the the f2 file and for lines that have an id on it it will 
> print the "next" line (so I can see what it is doing) and write it to a 
> file. I will turn off that screen print after I get it going the way I 
> want it to.

I don't see any particular reason this should be slow unless idList is 
large. Perhaps the output is being buffered somewhere and not appearing 
until the process is done? How are you running the program?

BTW the line that you print and the line that you save to the file are 
not the same; every time you call f2.next() it will fetch another line 
from the file. If you want them to be the same you could say
  found = "%s: %s" % (id, f2.next())
  print found
  f3.write(found)

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


Re: [Tutor] Bundle help!

2007-06-26 Thread Kent Johnson
Sara Johnson wrote:
> Hi Kent,
>  
> I had a list to sort alphabetically (that included names and 
> percentages), then I had to apend that list to sort by the category 
> represented by the percentages.  I've tried to use something like:
>  
> mylist = [whatever...]
> mylist.append(whatever) and that doesn't seem to work.  Also with this 
> list being sorted by names and by percentages, and there are more than 
> just a few items, I don't think I could just list all of the items out as:
>  
> x = ['apple', 'pear', banana', 'orange'] ..an example from "For Dummies"
>  
> I've also looked at tuples and such.  This is probably something simple, 
> but what I've tried hasn't worked.  There isn't much on bundles in here 
> and I've looked at the python site too.  I got the alphabetical list to 
> work by creating a 'newlist.'  Then I tried 'newlist2' but I'm getting 
> strange results. 

I still don't know enough to help you much. It would help if you showed 
some real code and data. How are you storing the names and percentages? 
If you have a list of pairs of (name, percentage) then you should be 
able to sort it directly with the sort() method of the list. For example:

In [3]: data = [ ('Kent', 50), ('Sara', 80), ('Fred', 20) ]
In [4]: data.sort()
In [5]: data
Out[5]: [('Fred', 20), ('Kent', 50), ('Sara', 80)]

Use append() to add more data, then sort again to get it in order:
In [6]: data.append(('Joe', 90))
In [7]: data.sort()
In [8]: data
Out[8]: [('Fred', 20), ('Joe', 90), ('Kent', 50), ('Sara', 80)]

What is a bundle? I don't know of any Python meaning for that term.

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] Beginner Game: Rock, Paper, Scissors

2007-06-26 Thread Johnny Jelinek

sure, I wouldn't mind looking at your code :D!  Also, the graphical one you
sent me was using gif's, do you know how to render svg's on screen?  The
advantage to vector rather than raster is that you can resize it as big as
you could ever desire and it will never lose quality.  That means I could
make a resolution independent game :D!  Does anyone have any ideas on how to
go about doing that?

On 6/26/07, bhaaluu <[EMAIL PROTECTED]> wrote:


Greetings,

I've attached a graphical Paper-Rock-Scissors game that I found
using the Google Code Search engine. It came as a tarball, but
I've put all the files in a ZIP file:

Archive:  paperock.zip
  Length Date   TimeName
   
12899  11-25-06 03:34   rockpaperscissors16.py
0  06-24-07 11:46   data/
 5974  09-29-06 07:33   data/computerklein.gif
 4553  09-28-06 08:48   data/papierkleinc.gif
 4574  09-28-06 08:48   data/papierklein.gif
 4338  09-28-06 08:48   data/scherekleinc.gif
 4353  09-28-06 08:48   data/schereklein.gif
 4254  09-28-06 08:48   data/steinkleinc.gif
 4245  09-28-06 08:48   data/steinklein.gif
   ---
45190   9 files

Here's the MD5SUM for the zip file:

9602c5b0dcbe38e1d8fffe20683c7902  paperock.zip

It is pretty neat. I hope it helps.
Thanks for posting your source code.
I also wrote a Paper-Rock-Scissors game as a first Python program.
It isn't graphical. If you'd like to take a look at it, let me know.

Happy Programming! =)
--
bhaaluu at gmail dot com

On 6/26/07, Johnny Jelinek <[EMAIL PROTECTED]> wrote:
> Hello, I'm a beginner to python; I wanted to make a fun little game, so
I
> started with something simple: Rock, Paper, Scissors.
>

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


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


Re: [Tutor] Fastest way to iterate through a file

2007-06-26 Thread Robert Hicks
Kent Johnson wrote:
> Robert Hicks wrote:
>> This is the loop code:
>>
>> for line in f2:
>>  for id in idList:
>>  if id in line:
>>  print "%s: %s" % (id, f2.next())
>>  found = "%s: %s" % (id, f2.next())
>>  f3.write(found)
>>
>>
>> I have an list, idList[], that contains a list of id numbers. That code 
>> will loop the the f2 file and for lines that have an id on it it will 
>> print the "next" line (so I can see what it is doing) and write it to a 
>> file. I will turn off that screen print after I get it going the way I 
>> want it to.
> 
> I don't see any particular reason this should be slow unless idList is 
> large. Perhaps the output is being buffered somewhere and not appearing 
> until the process is done? How are you running the program?
> 
> BTW the line that you print and the line that you save to the file are 
> not the same; every time you call f2.next() it will fetch another line 
> from the file. If you want them to be the same you could say
>   found = "%s: %s" % (id, f2.next())
>   print found
>   f3.write(found)
> 

Thanks for that!

idList only has about 129 id numbers in it.

I am running it straight from a Linux console. I thought about buffering 
but I am not sure how Python handles that.

Do you know if Python has a "slower" startup time than Perl? That could 
be part of it though I suspect the buffering thing more.

Robert

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


Re: [Tutor] Fastest way to iterate through a file

2007-06-26 Thread Kent Johnson
Robert Hicks wrote:
> idList only has about 129 id numbers in it.

That is quite a few times to be searching each line of the file. Try 
using a regular expression search instead, like this:

import re
regex = re.compile('|'.join(idList))
for line in f2:
   if regex.search(line):
 # process a hit

A simple test shows that to be about 25 times faster.

Searching for each of 100 id strings in another string:
In [6]: import timeit
In [9]: setup = "import re; import string; ids=[str(i) for i in 
range(1000, 1100)];line=string.letters"
In [10]: timeit.Timer('for i in ids: i in line', setup).timeit()
Out[10]: 15.298269987106323

Build a regular expression to match all the ids and use that to search:
In [11]: setup2=setup + ";regex=re.compile('|'.join(ids))"
In [12]: timeit.Timer('regex.search(line)', setup2).timeit()
Out[12]: 0.58947491645812988

In [15]: _10 / _12
Out[15]: 25.95236804820507

> I am running it straight from a Linux console. I thought about buffering 
> but I am not sure how Python handles that.

I don't think the console should be buffered.

> Do you know if Python has a "slower" startup time than Perl? That could 
> be part of it though I suspect the buffering thing more.

I don't know if it is slower than Perl but it doesn't take a few seconds 
on my computer. How long does it take you to get to the interpreter 
prompt if you just start Python? You could put a simple print at the 
start of your program to see when it starts executing.

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


Re: [Tutor] Fastest way to iterate through a file

2007-06-26 Thread Dave Kuhlman
On Tue, Jun 26, 2007 at 10:04:07AM -0400, Kent Johnson wrote:
> Robert Hicks wrote:
> > This is the loop code:
> > 
> > for line in f2:
> >  for id in idList:
> >  if id in line:
> >  print "%s: %s" % (id, f2.next())
> >  found = "%s: %s" % (id, f2.next())
> >  f3.write(found)
> > 
> > 
> > I have an list, idList[], that contains a list of id numbers. That code 
> > will loop the the f2 file and for lines that have an id on it it will 
> > print the "next" line (so I can see what it is doing) and write it to a 
> > file. I will turn off that screen print after I get it going the way I 
> > want it to.
> 
> I don't see any particular reason this should be slow unless idList is 
> large. Perhaps the output is being buffered somewhere and not appearing 
> until the process is done? How are you running the program?
> 

I think Kent is right.  You probably have a solution that is good
enough.  Ask yourself whether it really is going to save any time
if you are able to optimize it.

But, if speed is important, then you might try a solution that uses
a regular expression in place of

 for id in idList:
 if id in line:

A regular expression of the form "(id1|id2|id2)", which you could
construct programmatically, might work for your problem:

import re
s1 = '|'.join(idList)
s2 = '(%s)' % s1
pat = re.compile(s2)

Then use something like:

for mo in pat.finditer(line):

Dave


-- 
Dave Kuhlman
http://www.rexx.com/~dkuhlman
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Fastest way to iterate through a file

2007-06-26 Thread Jason Massey

Also since you're writing your found results to a file there's no need to
print the results to the screen.  That should shave off some time,
especially if you have a lot of hits.

On 6/26/07, Kent Johnson <[EMAIL PROTECTED]> wrote:


Robert Hicks wrote:
> idList only has about 129 id numbers in it.

That is quite a few times to be searching each line of the file. Try
using a regular expression search instead, like this:

import re
regex = re.compile('|'.join(idList))
for line in f2:
   if regex.search(line):
 # process a hit

A simple test shows that to be about 25 times faster.

Searching for each of 100 id strings in another string:
In [6]: import timeit
In [9]: setup = "import re; import string; ids=[str(i) for i in
range(1000, 1100)];line=string.letters"
In [10]: timeit.Timer('for i in ids: i in line', setup).timeit()
Out[10]: 15.298269987106323

Build a regular expression to match all the ids and use that to search:
In [11]: setup2=setup + ";regex=re.compile('|'.join(ids))"
In [12]: timeit.Timer('regex.search(line)', setup2).timeit()
Out[12]: 0.58947491645812988

In [15]: _10 / _12
Out[15]: 25.95236804820507

> I am running it straight from a Linux console. I thought about buffering
> but I am not sure how Python handles that.

I don't think the console should be buffered.

> Do you know if Python has a "slower" startup time than Perl? That could
> be part of it though I suspect the buffering thing more.

I don't know if it is slower than Perl but it doesn't take a few seconds
on my computer. How long does it take you to get to the interpreter
prompt if you just start Python? You could put a simple print at the
start of your program to see when it starts executing.

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

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


Re: [Tutor] Fastest way to iterate through a file

2007-06-26 Thread Robert Hicks
Kent Johnson wrote:
> Robert Hicks wrote:
>> idList only has about 129 id numbers in it.
> 
> That is quite a few times to be searching each line of the file. Try 
> using a regular expression search instead, like this:
> 
> import re
> regex = re.compile('|'.join(idList))
> for line in f2:
>if regex.search(line):
>  # process a hit
> 

Since I am printing to a file like so:

[id]: [line]

I don't see how I can get the id back out of the regex.search like I 
could in my code.

Robert

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


[Tutor] Importing and creation on the fly

2007-06-26 Thread Tino Dai

Hi there,

I've been banging my head on this for about two weeks, and I can't
figure out a solution to this. I'm wondering if you could assist me on this
pesky problem.

I'm reading in an xml file that has the name of class, location, and
the filename into a dictionary. I want to import these classes and create
instances of them.  The code in question is as follows:

36   for xmlKey in self.dictXML.keys():
37 if not self.dictXML[xmlKey]['location'] in sys.path and \
38 not self.dictXML[xmlKey]['location'] == os.getcwd():
39 sys.path.append(self.dictXML[xmlKey]['location'])
40 try:
41 if os.stat(self.dictXML[xmlKey]['location'] + \
42 self.dictXML[xmlKey]['filename']):
43 eval('import ' + self.dictXML[xmlKey]["class"])
<-- syntax error here
44 actionStmt=self.dictXML[xmlKey]["class"] + '.' +
self.dictXML[xmlKey]["class"] + '()' 45
45  self.objList.append(eval(actionStmt))
46 except:
47 pass


I have also tried: __import__(self.dictXML[xmlKey]["class"]), which gave me
an error when I did the eval(actionStmt). Could anybody shed some light on
this? Thanks in advance.

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


Re: [Tutor] Fastest way to iterate through a file

2007-06-26 Thread Kent Johnson
Robert Hicks wrote:
> Kent Johnson wrote:
>> Robert Hicks wrote:
>>> idList only has about 129 id numbers in it.
>> That is quite a few times to be searching each line of the file. Try 
>> using a regular expression search instead, like this:
>>
>> import re
>> regex = re.compile('|'.join(idList))
>> for line in f2:
>>if regex.search(line):
>>  # process a hit
>>
> 
> Since I am printing to a file like so:
> 
> [id]: [line]
> 
> I don't see how I can get the id back out of the regex.search like I 
> could in my code.

match = regex.search(line)
if match:
   idMatch = match.group()
   print idMatch, line

Note this will only find the first id match on a line. If you need to 
find multiple matches per line use findall() or finditer() as Dave suggests.

re docs are here:
http://docs.python.org/lib/module-re.html

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


Re: [Tutor] using shelve

2007-06-26 Thread Kent Johnson
Reed O'Brien wrote:
> Thanks for the heads up, Kent.  I didn't realize that was a copy of  
> Lutz's book.  I sent notice to [EMAIL PROTECTED]
> It did seem like an awfully rich comparison of python persistence  
> options.
> 
> If they follow up with me I will follow up with the list.

I also emailed O'Reilly, just got a thank-you email back from them 
saying they will "investigate and take appropriate action".

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


Re: [Tutor] Fastest way to iterate through a file

2007-06-26 Thread Robert Hicks
Kent Johnson wrote:
> Robert Hicks wrote:
>> Kent Johnson wrote:
>>> Robert Hicks wrote:
 idList only has about 129 id numbers in it.
>>> That is quite a few times to be searching each line of the file. Try 
>>> using a regular expression search instead, like this:
>>>
>>> import re
>>> regex = re.compile('|'.join(idList))
>>> for line in f2:
>>>if regex.search(line):
>>>  # process a hit
>>>
>> Since I am printing to a file like so:
>>
>> [id]: [line]
>>
>> I don't see how I can get the id back out of the regex.search like I 
>> could in my code.
> 
> match = regex.search(line)
> if match:
>idMatch = match.group()
>print idMatch, line
> 
> Note this will only find the first id match on a line. If you need to 
> find multiple matches per line use findall() or finditer() as Dave suggests.
> 
> re docs are here:
> http://docs.python.org/lib/module-re.html
> 

I was just reading that.  : )

That worked...and while the first one was probably "good" enough, I have 
learned something new today thanks to you and the others.

TYVM

Robert

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


Re: [Tutor] Importing and creation on the fly

2007-06-26 Thread Kent Johnson
Tino Dai wrote:
> Hi there,
> 
>  I've been banging my head on this for about two weeks, and I can't 
> figure out a solution to this. I'm wondering if you could assist me on 
> this pesky problem.
> 
>  I'm reading in an xml file that has the name of class, location, 
> and the filename into a dictionary. I want to import these classes and 
> create instances of them.  The code in question is as follows:
> 
>  36   for xmlKey in self.dictXML.keys():
>  37 if not self.dictXML[xmlKey]['location'] in sys.path and \
>  38 not self.dictXML[xmlKey]['location'] == os.getcwd():
>  39 sys.path.append(self.dictXML[xmlKey]['location'])
>  40 try:
>  41 if os.stat(self.dictXML[xmlKey]['location'] + \
>  42 self.dictXML[xmlKey]['filename']):
>  43 eval('import ' + 
> self.dictXML[xmlKey]["class"])   <-- syntax error here
>  44 actionStmt=self.dictXML[xmlKey]["class"] + '.' + 
> self.dictXML [xmlKey]["class"] + '()' 45
> 45  self.objList.append(eval(actionStmt))
> 46 except:
>  47 pass
> 
> 
> I have also tried: __import__( self.dictXML[xmlKey]["class"]), which 
> gave me an error when I did the eval(actionStmt). Could anybody shed 
> some light on this? Thanks in advance.

eval() won't work because it evaluates expressions; import is a 
statement, not an expression. You might have better luck with exec for 
the import.

__import__() doesn't put the imported module into the global namespace 
the way an import statement does; it returns a reference to the module 
which you assign to a name. So I think you would need something like
module = __import__(self.dictXML[xmlKey]["class"])
obj = getattr(module, self.dictXML[xmlKey]["class"])()

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


Re: [Tutor] Importing and creation on the fly

2007-06-26 Thread Dave Kuhlman
On Tue, Jun 26, 2007 at 12:20:18PM -0400, Tino Dai wrote:
> Hi there,
> 
> I've been banging my head on this for about two weeks, and I can't
> figure out a solution to this. I'm wondering if you could assist me on this
> pesky problem.
> 
> I'm reading in an xml file that has the name of class, location, and
> the filename into a dictionary. I want to import these classes and create
> instances of them.  The code in question is as follows:
> 
> 36   for xmlKey in self.dictXML.keys():
> 37 if not self.dictXML[xmlKey]['location'] in sys.path and \
> 38 not self.dictXML[xmlKey]['location'] == os.getcwd():
> 39 sys.path.append(self.dictXML[xmlKey]['location'])
> 40 try:
> 41 if os.stat(self.dictXML[xmlKey]['location'] + \
> 42 self.dictXML[xmlKey]['filename']):
> 43 eval('import ' + self.dictXML[xmlKey]["class"])
> <-- syntax error here
> 44 actionStmt=self.dictXML[xmlKey]["class"] + '.' +
> self.dictXML[xmlKey]["class"] + '()' 45
> 45  self.objList.append(eval(actionStmt))
> 46 except:
> 47 pass
> 
> 
> I have also tried: __import__(self.dictXML[xmlKey]["class"]), which gave me
> an error when I did the eval(actionStmt). Could anybody shed some light on
> this? Thanks in advance.

For the task of importing, look at the "imp" module:

http://docs.python.org/lib/module-imp.html

Also, the "inspect" module may be of help:

http://docs.python.org/lib/module-inspect.html

In particular, look at the the inspect.getmembers() and
inspect.isclass() methods.

Dave


-- 
Dave Kuhlman
http://www.rexx.com/~dkuhlman
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Importing and creation on the fly

2007-06-26 Thread Tino Dai

On 6/26/07, Dave Kuhlman <[EMAIL PROTECTED]> wrote:


On Tue, Jun 26, 2007 at 12:20:18PM -0400, Tino Dai wrote:
> Hi there,
>
> I've been banging my head on this for about two weeks, and I can't
> figure out a solution to this. I'm wondering if you could assist me on
this
> pesky problem.
>
> I'm reading in an xml file that has the name of class, location, and
> the filename into a dictionary. I want to import these classes and
create
> instances of them.  The code in question is as follows:
>
> 36   for xmlKey in self.dictXML.keys():
> 37 if not self.dictXML[xmlKey]['location'] in sys.path and \
> 38 not self.dictXML[xmlKey]['location'] == os.getcwd():
> 39 sys.path.append(self.dictXML[xmlKey]['location'])
> 40 try:
> 41 if os.stat(self.dictXML[xmlKey]['location'] + \
> 42 self.dictXML[xmlKey]['filename']):
> 43 eval('import ' + self.dictXML[xmlKey]["class"])
> <-- syntax error here
> 44 actionStmt=self.dictXML[xmlKey]["class"] + '.' +
> self.dictXML[xmlKey]["class"] + '()' 45
> 45  self.objList.append(eval(actionStmt))
> 46 except:
> 47 pass
>
>
> I have also tried: __import__(self.dictXML[xmlKey]["class"]), which gave
me
> an error when I did the eval(actionStmt). Could anybody shed some light
on
> this? Thanks in advance.

For the task of importing, look at the "imp" module:

http://docs.python.org/lib/module-imp.html

Also, the "inspect" module may be of help:

http://docs.python.org/lib/module-inspect.html

In particular, look at the the inspect.getmembers() and
inspect.isclass() methods.

Dave



Thanks guys. I will look into that this afternoon

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


Re: [Tutor] Beginner Game: Rock, Paper, Scissors

2007-06-26 Thread Luke Paireepinart
Johnny Jelinek wrote:
> sure, I wouldn't mind looking at your code :D!  Also, the graphical 
> one you sent me was using gif's, do you know how to render svg's on 
> screen?  The advantage to vector rather than raster is that you can 
> resize it as big as you could ever desire and it will never lose 
> quality.  That means I could make a resolution independent game :D!  
> Does anyone have any ideas on how to go about doing that?
Render it to a file, display the file in Pygame.
IF you're using Pygame already, you'll know the resolution of the screen 
that the user is using, so you'll be able to render them in the highest 
quality possible for that specific resolution.

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


Re: [Tutor] Beginner Game: Rock, Paper, Scissors

2007-06-26 Thread Johnny Jelinek

how would I go about rendering an .svg to another file before showing it on
screen?  Could you point me to some resources or examples?  Thanks!

On 6/26/07, Luke Paireepinart <[EMAIL PROTECTED]> wrote:


Johnny Jelinek wrote:
> sure, I wouldn't mind looking at your code :D!  Also, the graphical
> one you sent me was using gif's, do you know how to render svg's on
> screen?  The advantage to vector rather than raster is that you can
> resize it as big as you could ever desire and it will never lose
> quality.  That means I could make a resolution independent game :D!
> Does anyone have any ideas on how to go about doing that?
Render it to a file, display the file in Pygame.
IF you're using Pygame already, you'll know the resolution of the screen
that the user is using, so you'll be able to render them in the highest
quality possible for that specific resolution.


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


Re: [Tutor] Beginner Game: Rock, Paper, Scissors

2007-06-26 Thread Luke Paireepinart
Johnny Jelinek wrote:
> how would I go about rendering an .svg to another file before showing 
> it on screen?  Could you point me to some resources or examples?  Thanks!
You just mentioned that Cairo renders svgs to a file, didn't you?
So can you just use pyCairo?
Google would tell you of other python modules that can render svg files.
I don't have any resources for doing this, I just assumed since you 
mentioned Cairo could do it that you knew
how to use Cairo to do it.
-Luke
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor