[Tutor] How to iterate and update subseqent items in list

2007-12-06 Thread ted b
Can you suggest a good way to iterate through the
remainder of list and update them?

Ex:
Thing1.value = 0
Thing2.value = 1
Thing3.value = 0
Thing4.value = 0

Things = [Thing1, Thing2, Thing3, Thing4]

I want to iterate through 'Things' and if
'Thing.value' > 0, then I want to set all values for
that and the subsequent 'Things' in the list to 2

So that i'd end up with

Thing1.value = 0
Thing2.value = 2
Thing3.value = 2
Thing4.value = 2


  

Be a better friend, newshound, and 
know-it-all with Yahoo! Mobile.  Try it now.  
http://mobile.yahoo.com/;_ylt=Ahu06i62sR8HDtDypao8Wcj9tAcJ 

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


[Tutor] affecting all classes if one class is affected by an event - pygame

2007-11-16 Thread ted b
I am trying to figure out how to make a class instance
respond the same way as another class instance if the
other is affected by some event. I have been playing
around with inheritance, and have tried other stuff,
but i am somewhat of a newbie and I have been having
difficulty (but you guys, particularly Kent, have
really been helping me a lot :

For example, in the pygame code below, i have set it
up so that the boxes will stop if they are above the
barrier and hit it. Well, that's what i want to
happen, but if one of the boxes hits the barrier, the
other box keeps going. Is there a way i can have both
boxes stop if either of them hit the barrier. I was
hoping there was a way that i could have the info from
one class get passed to the other classes so i could
stop the other box (or stop all  or only some other
boxes if i add lots more). Should i make another
class? Another method? Globals?

Here's the code:

#/usr/bin/env python

import pygame

from pygame.locals import *

pygame.init()

class testBox1(pygame.sprite.Sprite):
   def __init__(self):
  pygame.sprite.Sprite.__init__(self)
  self.image=pygame.Surface((25,25))
  self.image.fill((255,0,0))
  self.rect=self.image.get_rect()
  self.rect.center = (30,90)

   def update(self):
  # check for user input and move left, right, up
or down
  keys = pygame.key.get_pressed()
  if keys[pygame.K_w]:
 self.rect.center = (self.rect.centerx,
self.rect.centery-4)
  if keys[pygame.K_s]:
 self.rect.center = (self.rect.centerx,
self.rect.centery+4)
  if keys[pygame.K_a]:
 self.rect.center = (self.rect.centerx-4,
self.rect.centery)
  if keys[pygame.K_d]:
 self.rect.center = (self.rect.centerx+4,
self.rect.centery)
  # see if the rect hit the barrier
  self.checkPos()

   def checkPos(self):
  # if box rect moves below barrier's rect, halt
box's position
  if ((self.rect.bottom > testBarrier().rect.top)
and (self.rect.top < testBarrier().rect.top)):
 if ((testBarrier().rect.right >
self.rect.right > testBarrier().rect.left) or
(testBarrier().rect.right > self.rect.left >
testBarrier().rect.left)):
self.rect.bottom = testBarrier().rect.top
 
class testBox2(testBox1):
   def __init__(self):
  pygame.sprite.Sprite.__init__(self)
  self.image=pygame.Surface((25,25))
  self.image.fill((0,0,255))
  self.rect=self.image.get_rect()
  self.rect.center = (80,50)

class testBarrier(pygame.sprite.Sprite):
   def __init__(self):
  pygame.sprite.Sprite.__init__(self)
  self.image=pygame.Surface((30,4))
  self.image.fill((0,0,0))
  self.rect=self.image.get_rect()
  self.rect.center = (50,150)

def main():
   screen = pygame.display.set_mode((100,300))

   pygame.display.set_caption("testing")

   background=pygame.Surface(screen.get_size())
   background=background.convert()
   background.fill((255,255,255))
   screen.blit(background, (0,0))

   box1=testBox1()
   box2=testBox2()
   barrier=testBarrier()

   allSprites=pygame.sprite.Group(box1, box2, barrier)
   
   clock=pygame.time.Clock()
   keepGoing=True
   while keepGoing:
  clock.tick(30)
  for event in pygame.event.get():
 if event.type==pygame.QUIT:
keepGoing=False

  allSprites.clear(screen, background)
  allSprites = pygame.sprite.OrderedUpdates
(barrier, box1, box2)
  allSprites.update()
  allSprites.draw(screen)
  
  pygame.display.flip()

if __name__ == "__main__":
   main()

Thanks for all your help!!! :)
-ted


  

Be a better sports nut!  Let your teams follow you 
with Yahoo Mobile. Try it now.  
http://mobile.yahoo.com/sports;_ylt=At9_qDKvtAbMuh1G1SQtBI7ntAcJ
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] selecting elements from a list that do not meet selection criteria

2007-11-15 Thread ted b
Is there a way i can select all elements from a list
that do not meet selection criteria. I want to be able
to select elements that have values of, say, < 1 but
only if at least one of the elements has a value of >
0.

What i mean is, for example, in the code below, if one
of the elements of "list 'a'" has a value greater than
1, then i want to print all the other elements in the
list (i.e., class One and class Three) and to do
otherStuff associated with those classes. Right now,
it prints those elements that *do* have values of > 0,
(i.e. class Two). But in situations where all of the
classes have values set to 0, then i don't want
anything selected. 

I don't want to just use something like "if x.value()
!= 0" or "if x.value() < 1" since those would give
results if all elements were less than 1, and i only
want to select elements of the list that are less than
1 if at least one of the elements is > 1.

Here's the sample code:

class One:
   def value(self):
  return 0
  
class Two:
   def value(self):
  return 1

class Three:
   def value(self):
  return 0

a = [One(), Two(), Three()]

for x in a:
   if x.value() > 0:
  print x
  x.otherStuff()

Thanks in advance!!! :)))


  

Be a better pen pal. 
Text or chat with friends inside Yahoo! Mail. See how.  
http://overview.mail.yahoo.com/
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] class accessing another's updated property

2007-11-14 Thread ted b
I want class One to be able to access access class
Two's value property after its been updated. Everytime
I try (by running, say testTwo().value) I get the
__init__ value. The update method fro class Two is
called elsewhere in the program, and I want class
One's "getTwo" method to access class Two's updated
value and give me '9' but i get '1'

Here's the code:

class One:
   def __init__(self):
  self.value = 3
   def getTwo(self):
  print "testTwo's updated value", Two().value

class Two:
   def __init__(self):
  self.value = 1
   def update(self):
  self.value = 9

Thanks in advance!


  

Be a better sports nut!  Let your teams follow you 
with Yahoo Mobile. Try it now.  
http://mobile.yahoo.com/sports;_ylt=At9_qDKvtAbMuh1G1SQtBI7ntAcJ
___
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


[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


[Tutor] sorting variables

2007-10-31 Thread ted b
I am using pygame and i have three variables that i
want to sort (var1, var2 and var3) and be able to
refer to them later in a statement that will use them
in sorted order:

the statement i am using is:
   objSprites = pygame.sprite.OrderedUpdates(var1,
var2, var3)

and i want var1, var2 and var3 to show up in the
statement in order of their value

so if var1 = 9, var2 = 3 and var3 = 5, i want my
statement to be equivalent to

   objSprites = pygame.sprite.OrderedUpdates(var2,
var3, var1)

i was thinking of doing something like

   objSprites = pygame.sprite.OrderedUpdates((var1,
var2, var3).sort)

but i don't think thats gunna work

any help much appreciated

__
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