Re: [Tutor] Converting a numpy matrix to a numpy array

2011-04-04 Thread Peter Otten
David Crisp wrote:

> np.array([np.arange(9)//3, np.arange(9)%3, a.flatten()],
>> dtype=object).transpose()
>> array([[0, 0, x],
>> [0, 1, o],
>> [0, 2, o],
>> [1, 0, o],
>> [1, 1, x],
>> [1, 2, x],
>> [2, 0, o],
>> [2, 1, x],
>> [2, 2, o]], dtype=object)
>>
>> If that's not good enough you may also ask on the numpy mailing list.
> 
> Thanks Peter,
> 
> That appears to do what I want, in a way.How does this work if you
> have a matrix which is of variable size?   For instance,  some of my
> data will create a 10 by 10 matrix but some will create a 40 by 40
> matrix, Or for that matter any size.I notice your example
> specifically states there will be 9 outputs ( tupples? )   what if I
> want to say "just create as many tuples as you need to use to
> transpose the data"

You can find out the size of the matrix with the shape attribute:

>>> a
array([[ 0,  1,  2,  3],
   [ 4,  5,  6,  7],
   [ 8,  9, 10, 11]])
>>> a.shape
(3, 4)

Use that to calculate the values needed to replace the constants in my 
previous post. Try to make do without the spoiler below!












































>>> def process(a, dtype=object):
... x, y = a.shape
... n = x*y
... return np.array([np.arange(n)//y, np.arange(n)%y, a.flatten()], 
dtype=dtype).transpose()
... 

>>> a = np.arange(12).reshape(3, 4)
>>> a  
array([[ 0,  1,  2,  3],   
   [ 4,  5,  6,  7],   
   [ 8,  9, 10, 11]])
>>> process(a, int)
array([[ 0,  0,  0],
   [ 0,  1,  1],
   [ 0,  2,  2],
   [ 0,  3,  3],
   [ 1,  0,  4],
   [ 1,  1,  5],
   [ 1,  2,  6],
   [ 1,  3,  7],
   [ 2,  0,  8],
   [ 2,  1,  9],
   [ 2,  2, 10],
   [ 2,  3, 11]])
>>> b = np.array(list(
... "xoo"
... "oxx"
... "oxo")).reshape(3, 3)
>>> process(b, object)
array([[0, 0, x],
   [0, 1, o],
   [0, 2, o],
   [1, 0, o],
   [1, 1, x],
   [1, 2, x],
   [2, 0, o],
   [2, 1, x],
   [2, 2, o]], dtype=object)


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


[Tutor] (no subject)

2011-04-04 Thread Greg Richards
http%3A%2F%2Fminilien%2Ecom%2F%3FZuctsogCRp
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Calling another script

2011-04-04 Thread Alan Gauld


"tee chwee liong"  wrote


i want one.py to read a configuration file and
executes two.py and three.py.

if int(operation)== 0:
import two
else:
   print "Default"

two.py:
print "executing script number 2"
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor



importing a module is not intended as a mechanism to execute
code, that should be considered a side-effect or an initialisation 
feature.

importing makes code available for use.

Put the code in the modules into functions, then import all the 
modules at

the start of your program. Then execute the functions within your
if/else logic.

import two,three

operation = read_from_config_file(filename)
if operation == 0:
   two.doTwo()
   three.doThree()
else:
   print "invalid operation"

That wil, be a more reliable and flexible approach.

In fact you could put all the operations in one module...

HTH,


--
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


Re: [Tutor] Calling another script

2011-04-04 Thread Steven D'Aprano
On Mon, Apr 04, 2011 at 05:10:50AM +, tee chwee liong wrote:
> 
> hi,
>  
> i opened a cmd DOS prompt to execute one.py. it works to execute codes from 
> two.py and three.py. yes, you are fight it will not re-execute the module. 
> is there a way to do it? i want after the python script finishes execution 
> will return the control to the DOS prompt instead of leaving as >>>.
> i tried putting sys.exit(). 

Do not confuse importing a script with running a script.

The usual way to run a script is to write it with a main() function, and then 
call that:

# === myscript.py ===

print("Set up code goes here...")
# this only gets executed once, the FIRST time you import the module


def main():
print("Running script now!")

if __name__ == '__main__':
# We've been called from the shell.
main()



In your other script, you do this:

# === Master script that calls myscript ===

if __name__ == '__main__':
# Running as a script ourselves.
import myscript
myscript.main()





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


Re: [Tutor] Passing a Variable

2011-04-04 Thread Andre Engels
On Mon, Apr 4, 2011 at 7:27 AM, Ryan Strunk  wrote:
>> I've read your code. Frankly I don't understand your problem. I also don't
> see any occurrence of "health".
> There isn't a reference to health here. My goal is to have this code act as
> a checker for health, fatigue, time_remaining, or any other sort of
> statistic you'd like to throw into it. My problem is that when I try:
> instance = Statistic(stat=health, sound=spam, low=1, mid=15, high=30)
> health can change elsewhere in the program, but the instance of statistic
> class won't automatically see it.

My proposal would be to wrap the stats in an object:

Class stat:
 __init__(self, name, value)
 self.type = name
 self.value = value

Then in the player object change the initialisation

health = startvalue

to

health = stat("health", startvalue)

and change every other reference to health to a reference to health.value.

Then you can use the current code if you replace self.stat outside the
__init__ by self.stat.value

You could even consider merging the stats and Statistics classes.

==

Another possibility would be to use a getter method and the fact that
methods are objects:

In the player object add:

def get_health(self):
return self.health

change the call to:

instance = Statistic(stat=get_health, sound=spam, low=1, mid=15, high=30)

and replace self.stat by self.stat() everywhere in the Statistics code

-- 
André Engels, andreeng...@gmail.com
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Calling another script

2011-04-04 Thread Alan Gauld


"tee chwee liong"  wrote


i want one.py to read a configuration file and
executes two.py and three.py.

if int(operation)== 0:
import two
else:
   print "Default"

two.py:
print "executing script number 2"
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor



importing a module is not intended as a mechanism to execute
code, that should be considered a side-effect or an initialisation 
feature.

importing makes code available for use.

Put the code in the modules into functions, then import all the 
modules at

the start of your program. Then execute the functions within your
if/else logic.

import two,three

operation = read_from_config_file(filename)
if operation == 0:
   two.doTwo()
   three.doThree()
else:
   print "invalid operation"

That wil, be a more reliable and flexible approach.

In fact you could put all the operations in one module...

HTH,


--
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


Re: [Tutor] Calling another script

2011-04-04 Thread tee chwee liong

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


Re: [Tutor] Passing a Variable

2011-04-04 Thread Lie Ryan
On 04/04/11 11:55, Ryan Strunk wrote:
> Hi list,
> 
> I am in the midst of trying to code a game based entirely on audio cues, and
> I've run into a bit of a snag when trying to monitor certain variables. I'll
> lay out the framework of what I'm going for in the hope that it makes sense
> when written down.
> In a standard video game I could have a health bar go from normal to yellow
> to red as it diminishes. In audio, though, I don't have that luxury. As a
> result, I have conceptualized a system whereby a player hears a sound every
> so often if a particular stat drops into the caution range. If the player
> drops into the danger range, the sound loops continuously. I also wanted to
> make sure that if the player dropped from caution to danger, there wasn't a
> big, awkward pause in the sound loop and that the player would know
> immediately that his stat had dropped (see first and second if checks in the
> check method).
> The problem:
> My existing methods directly update stats. For example: the player class has
> a self.health stat which is directly affected by other methods. This has
> caused no problem up until now. When I pass self.health to the code I will
> paste below, however, the Statistic class does not receive health, but
> rather health's value.
> I understand that python passes variables by value and not by reference, and
> this has not been a problem up until now. Now that I am trying to design a
> class which explicitly checks a specific variable, though, I can't fathom a
> way to do it unless I pass a direct reference, and I'm not sure that can be
> done. I need to figure out a way for the below code to check the value of
> the health variable and act on it. This way, if player's self.health
> changes, the static class will take note of that and respond accordingly.
> It occurred to me to make Statistic a child of int, but I'm told that's more
> trouble than I probably want to deal with.
> Any suggestions/advice anyone has would be greatly appreciated.

Rather than having Statistic polling the Player's health, I suggest that
the Player object should call a method in Statistic class when its
health changes, and then the Statistic class can see if the value change
is relevant or not (e.g. whether to start playing audio, or not).

Since you said that you modified self.health directly, in some other
languages this might cause you problems. But behold, this is python, you
can easily turn your attribute into property:

class Player(object):
def __init__(self):
self.stat = Statistic()
self._health = 100

@property
def health(self):
return self._health
@health.setter
def health(self, value):
self.stat.health_changed(self, value)
self._health = value


class Statistic(object):
def __init__(...): ...
def health_changed(self, player, value):
if value < player.health:
play_once('taking damage')
elif value > player.health:
play_once('getting healed')

if value < self.low:
self.status = 'danger'
play_repeat('danger')
elif value < self.mid:
self.status = 'warning'
play_repeat('warning')
else:
self.status = 'safe'
play_stop()

> Best,
> Ryan
> 
> import sound_lib
> from game_utils import delay
> #this encapsulates threading.Timer's assignment and start method
> 
> class Statistic(object):
> 
> def __init__(self, stat=None, sound=None, low=None, mid=None,
> high=None):
> self.stat = stat
> self.sound = sound
> self.low = low
> self.mid = mid
> self.high = high
> self.status = 'safe'
> self.auto_check_timer = None
> 
> def auto_check(self):
> if self.stat > self.high:
> self.status = 'safe'
> return
> if self.mid <= self.stat <= self.high:
> self.status = 'caution'
> self.sound.play(True)
> self.auto_check_timer =
> delay(self.sound.bytes_to_seconds(len(self.sound))*2, self.auto_check)
> return
> if self.low <= self.stat < self.mid:
> self.status = 'danger'
> self.sound.play(True)
> self.auto_check_timer =
> delay(self.sound.bytes_to_seconds(len(self.sound)), self.auto_check)
> 
> def check(self):
> if self.status = 'caution' and self.low <= self.stat < self.mid:
> #This will set the program to start a constant alarm when the
> stat level has dropped below caution
> self.auto_check_timer.cancel()
> if self.sound.is_playing:
> #to assist in setting up the caution to danger transition
> #a standard playing sound will have a timer running alongside
> it, so skip the next guard and return
> if self.auto_check_timer.is_alive() == False:
> #guard to make sure program doesn't catch every playing
> sound, should prevent repe

[Tutor] Recommendations required

2011-04-04 Thread ANKUR AGGARWAL
Hey
I am reading pygame module and experimenting with it in small codes too . I
want your help. I want you to  recommend the games ,beginner of this module
should try to develop as a practice or so.
Thanks
Ankur Aggarwal
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Recommendations required

2011-04-04 Thread michael scott
Hello Ankur,

Well as a beginner myself I suggest that you check out these games and modify 
them or create new versions of them (with additional functionality). There are 
lots of stock answers, but in general old arcade games from the 70's are great 
places to start. They are simple enough and you should understand enough about 
them to know the various things you will need to implement.

Heavily coded games
http://inventwithpython.com/blog/category/code-comments/

I asked the same question a while back and I'm sure you will get similar 
answers 
to what I got. Although If you have not done most of the tutorials on the 
pygame 
site itself, even these heavily coded source codes may be too complex for you 
too handle.

The pygame tutorials
http://pygame.org/wiki/tutorials

Good luck and I hope you create fun games for others to play.

 
What is it about you... that intrigues me so?





From: ANKUR AGGARWAL 
To: pygame-us...@seul.org; tutor@python.org
Sent: Mon, April 4, 2011 12:12:26 PM
Subject: [Tutor] Recommendations required

Hey
I am reading pygame module and experimenting with it in small codes too . I 
want 
your help. I want you to  recommend the games ,beginner of this module should 
try to develop as a practice or so.
Thanks
Ankur Aggarwal
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Toronto PyCamp 2011

2011-04-04 Thread Chris Calloway
The University of Toronto Department of Physics brings PyCamp to Toronto 
on Monday, June 27 through Thursday, June 30, 2011.


Register today at http://trizpug.org/boot-camp/torpy11/

For beginners, this ultra-low-cost Python Boot Camp makes you productive 
so you can get your work done quickly. PyCamp emphasizes the features 
which make Python a simpler and more efficient language. Following along 
with example Python PushUps™ speeds your learning process. Become a 
self-sufficient Python developer in just four days at PyCamp! PyCamp is 
conducted on the campus of the University of Toronto in a state of the 
art high technology classroom.


--
Sincerely,

Chris Calloway http://nccoos.org/Members/cbc
office: 3313 Venable Hall   phone: (919) 599-3530
mail: Campus Box #3300, UNC-CH, Chapel Hill, NC 27599
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor