Update text in real time by calling two functions Pygame - python

I have program that takes input from the user and displays multiple variations of the input using the Population() function. The store_fit function adds these different variations to a list then deletes them so that the list is only populated with one variation at a time.
I want to be able to get the variation from the list and use it to update my text. However, my program only updates the text after the Population function is completed. How could I run the Population function and update my text simultaneously?
code:
fit = []
...
def store_fit(fittest): # fittest is each variation from Population
clear.fit()
fit.append(fittest)
...
pg.init()
...
done = False
while not done:
...
if event.key == pg.K_RETURN:
print(text)
target = text
Population(1000) #1000 variations
store_fit(value)
# I want this to run at the same time as Population
fittest = fit[0]
...
top_sentence = font.render(("test: " + fittest), 1, pg.Color('lightskyblue3'))
screen.blit(top_sentence, (400, 400))

I recommend to make Population a generator function. See The Python yield keyword explained:
def Populate(text, c):
for i in range(c):
# compute variation
# [...]
yield variation
Create an iterator and use next() to retrieve the next variation in the loop, so you can print every single variation:
populate_iter = Populate(text, 1000)
final_variation = None
while not done:
next_variation = next(populate_iter, None)
if next_variation :
final_variation = next_variation
# print current variation
# [...]
else:
done = True
Edit according to the comment:
In order to keep my question simple, I didn't mention that Population, was a class [...]
Of course Populate can be a class, too. I this case you've to implement the object.__iter__(self) method. e.g.:
class Populate:
def __init__(self, text, c):
self.text = text
self.c = c
def __iter__(self):
for i in range(self.c):
# compute variation
# [...]
yield variation
Create an iterator by iter(). e.g.:
populate_iter = iter(Populate(text, 1000))
final_variation = None
while not done:
next_variation = next(populate_iter, None)
if next_variation :
final_variation = next_variation
# print current variation
# [...]
else:
done = True

Related

Efficient Way Doing Same function for different variables

I'm trying to make my code more efficient. Currently, I do have two functions doing basically the same inside a while loop. Only the subject (a and b) is different. These two subjects are taking turns with every loop.
This is my framework so far:
#run engine
engine_running = True
#set first subject
a = True
b = False
# while engine is running rotate between a and b
while engine_running == True:
if (a == True):
Function_a()
a = False
b = True
elif (b == True):
Function_b()
a = True
b = False
else:
print('Error')
This is the framework of both functions. It's noteworthy that each function reads the same set of Data, which has Data for a and b.
def Function_a():
import Data
import random
# Get Data and the weights
List = [Data.a_person1, Data.a_person2, Data.a_person3]
Weight = [List[0]['attribute'],List[1]['attribute'], List[2]['attribute']
# Choosing a random person based on its attribute
Selection = random.choices(List,Weight)
print(Selection[0]['name'], 'has been chosen')
def Function_b():
import Data
import random
# Get Data and the weights
List = [Data.b_person1, Data.b_person2, Data.b_person3]
Weight = [List[0]['attribute'],List[1]['attribute'], List[2]['attribute']
# Choosing a random person based on its attribute
Selection = random.choices(List,Weight)
print(Selection[0]['name'], 'has been chosen')
I'm new to python, so I understand this may look ugly and there is probably a nicer and more efficient way of doing this. Currently, it works for me. But maybe you have some input for me?
You could simply pass the lists that you wish to work on to the function
def Function(data):
import random
# Get Data and the weights
Weight = [data[0]['attribute'], data[1]['attribute'], data[2]['attribute']
# Choosing a random person based on its attribute
Selection = random.choices(data,Weight)
print(Selection[0]['name'], 'has been chosen')
Function([Data.a_person1, Data.a_person2, Data.a_person3])
Function([Data.b_person1, Data.b_person2, Data.b_person3])
def a():
print("a")
def b():
print("b")
switch = True
while True:
if switch:
a()
switch = False
elif not swith:
b()
switch = True
else:
print('Error')

Python Classes: Appending a list & Incorporating a Method

I just started learning about Python classes today and I had a quick question. I'm pretty amazed how much more succinct it has made my code, but I'm trying to figure out if the following is possible for a chess problem I'm working on.
(1) Can I append a list somehow from within a class method? I'm trying to figure out if there's a way to accumulate the pieces in a list each time capture is called.
(2) How can I call a method from within the class to be used in another method? I would like to be able to check if a move is valid before even proceeding if the piece should try to capture another or move.
class Piece(Board):
def __init__(self, piece, r, c):
self.piece = piece
self.r = r
self.c = c
This is the function I would like to incorporate into the functions below to avoid the redundancy (Question 2)
def valid_move(self,r,c,r_offset,c_offset):
#r, c are integers for the coordinates on the board
#r_offset,c_offset are the cells the piece might move to
self.tgt_r, self.tgt_c = r+r_offset, c+c_offset
if self.tgt_r <= 7 or self.tgt_c >= 0:
return True
return False
These functions are the same for now. I'm trying to see how I can use the capture function to accumulate a list of pieces once they're taken. (Question 1)
def capture(self,r,c, r_offset, c_offset):
piece = self.piece
self.tgt_r, self.tgt_c = r+r_offset, c+c_offset
if self.tgt_r > 7 or self.tgt_c < 0:
return None
else:
nb = Board(curr).copy_board() #this board is just 8x8 np.array
nb[self.tgt_r,self.tgt_c], nb[r,c] = piece,'-'
return nb
def move(self,r,c, r_offset, c_offset):
piece = self.piece
self.tgt_r, self.tgt_c = r+r_offset, c+c_offset
if self.tgt_r > 7 or self.tgt_c < 0:
return None
else:
nb = Board(curr).copy_board()
nb[self.tgt_r,self.tgt_c], nb[r,c] = piece,'-'
return nb
Thanks as always.
1. Can I append a list somehow from within a class method?
create a list -piecesList in your class for storing the pieces:
class Piece(Board):
def __init__(self, piece, r, c):
self.piece = piece
self.r = r
self.c = c
self.piecesList = [] #or init using some argument if you want to use some list from outside of the class
and whenever your capture method is called, simply append the piece in the piecesList :
def capture(self,r,c, r_offset, c_offset):
self.piecesList.append(self.piece)
piece = self.piece
2. How can I call a method from within the class to be used in another method?
you can simply call it using self.method(arg1, arg2...) :
def capture(self,r,c, r_offset, c_offset):
piece = self.piece
if self.valid_move(r,c,r_offset,c_offset) == False:
return None
else:
nb = Board(curr).copy_board() #this board is just 8x8 np.array
nb[self.tgt_r,self.tgt_c], nb[r,c] = piece,'-'
return nb

Python function replacing part of variable

I am writing a code for a project in particle physics (using pyroot).
In my first draft, I use the following line
for i in MyTree:
pion.SetXYZM(K_plus_PX, K_plus_PY, K_plus_PZ,K_plus_MM)
This basically assigns to the pion the values of variables in the parenthesis, ie momenta and inv. mass of the kaon.
Physics aside, I would like to write a function "of the form":
def myfunc(particle):
return %s_PX % particle
I know this is wrong. What I would like to achieve is to write a function that allows, for a given particle, to set particle_PX, particle_PY etc to be the arguments of SetXYZM.
Thank you for your help,
B
To access class attributes from string variables you can use python's getattr:
import ROOT
inputfile = ROOT.TFile.Open("somefile.root","read")
inputtree = inputfile.Get("NameOfTTree")
inputtree.Print()
# observe that there are branches
# K_plus_PX
# K_plus_PY
# K_plus_PZ
# K_plus_MM
# K_minus_PX
# K_minus_PY
# K_minus_PZ
# K_minus_MM
# pi_minus_PX
# pi_minus_PY
# pi_minus_PZ
# pi_minus_MM
def getx(ttree,particlename):
return getattr(ttree,particlename+"_PX")
def gety(ttree,particlename):
return getattr(ttree,particlename+"_PY")
def getz(ttree,particlename):
return getattr(ttree,particlename+"_PZ")
def getm(ttree,particlename):
return getattr(ttree,particlename+"_MM")
def getallfour(ttree,particlename):
x = getattr(ttree,particlename+"_PX")
y = getattr(ttree,particlename+"_PY")
z = getattr(ttree,particlename+"_PZ")
m = getattr(ttree,particlename+"_MM")
return x,y,z,m
for entry in xrange(inputtree.GetEntries()):
inputtree.GetEntry(entry)
pion1 = ROOT.TLorentzVector()
x = getx(inputtree,"K_plus")
y = gety(inputtree,"K_plus")
z = getz(inputtree,"K_plus")
m = getm(inputtree,"K_plus")
pion2.SetXYZM(x,y,z,m)
x,y,z,m = getallfour(inputtree,"pi_minus")
pion2 = ROOT.TLorentzVector()
pion2.SetXYZM(x,y,z,m)
As linked by Josh Caswell, you can similarly access variable names:
def getx(particlename):
x = globals()[partilcename+"_PX"]
though that might get nasty quickly as of whether your variables are global or local and for local, in which context.

Extract value from a list in another function in Python

I am programming a robot and I want to use an Xbox Controller using pygame. So far this is what I got (original code credits to Daniel J. Gonzalez):
"""
Gamepad Module
Daniel J. Gonzalez
dgonz#mit.edu
Based off code from: http://robots.dacloughb.com/project-1/logitech-game-pad/
"""
import pygame
"""
Returns a vector of the following form:
[LThumbstickX, LThumbstickY, Unknown Coupled Axis???,
RThumbstickX, RThumbstickY,
Button 1/X, Button 2/A, Button 3/B, Button 4/Y,
Left Bumper, Right Bumper, Left Trigger, Right Triller,
Select, Start, Left Thumb Press, Right Thumb Press]
Note:
No D-Pad.
Triggers are switches, not variable.
Your controller may be different
"""
def get():
out = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
it = 0 #iterator
pygame.event.pump()
#Read input from the two joysticks
for i in range(0, j.get_numaxes()):
out[it] = j.get_axis(i)
it+=1
#Read input from buttons
for i in range(0, j.get_numbuttons()):
out[it] = j.get_button(i)
it+=1
first = out[1]
second = out[2]
third = out[3]
fourth = out[4]
return first, second, third, fourth
def test():
while True:
first, second, third, fourth = get()
pygame.init()
j = pygame.joystick.Joystick(0)
j.init()
print 'Initialized Joystick : %s' % j.get_name()
test()
Do you see the list called "out"? Each element in it is a button on the Xbox Controller. I want to extract those elements and put them on variables, one variable to each element/button so I can control my robot.
How could I do it?
I've tried to use global variables but then everything turned down to a mess.
Please note that I am a beginner in Python.
If you want to have out in your program then just return it from your function get:
def get():
# rest of the code ...
return out
Also change your function test:
def test():
while True:
out = get()
LThumbstickX = out[0]
LThumbstickY = out[1]
# and so on
Then run your program as before. What the function test does is constantly (while True) read the keypad. You could for example do:
def test():
while True:
out = get()
LThumbstickX = out[0]
if LThumbstickX != 0:
print 'Left button has been pressed'
# and so on
You can just return the list and use python's unpacking feature:
def get():
out = [1,2,3,4]
return out
first, second, third, fourth = get()

Why is this recursive statement wrong?

This is a bank simulation that takes into account 20 different serving lines with a single queue, customers arrive following an exponential rate and they are served during a time that follows a normal probability distribution with mean 40 and standard deviation 20.
Things were working just fine till I decided to exclude the negative values given by the normal distribution using this method:
def getNormal(self):
normal = normalvariate(40,20)
if (normal>=1):
return normal
else:
getNormal(self)
Am I screwing up the recursive call? I don't get why it wouldn't work. I have changed the getNormal() method to:
def getNormal(self):
normal = normalvariate(40,20)
while (normal <=1):
normal = normalvariate (40,20)
return normal
But I'm curious on why the previous recursive statement gets busted.
This is the complete source code, in case you're interested.
""" bank21: One counter with impatient customers """
from SimPy.SimulationTrace import *
from random import *
## Model components ------------------------
class Source(Process):
""" Source generates customers randomly """
def generate(self,number):
for i in range(number):
c = Customer(name = "Customer%02d"%(i,))
activate(c,c.visit(tiempoDeUso=15.0))
validateTime=now()
if validateTime<=600:
interval = getLambda(self)
t = expovariate(interval)
yield hold,self,t #esta es la rata de generaciĆ³n
else:
detenerGeneracion=999
yield hold,self,detenerGeneracion
class Customer(Process):
""" Customer arrives, is served and leaves """
def visit(self,tiempoDeUso=0):
arrive = now() # arrival time
print "%8.3f %s: Here I am "%(now(),self.name)
yield (request,self,counter),(hold,self,maxWaitTime)
wait = now()-arrive # waiting time
if self.acquired(counter):
print "%8.3f %s: Waited %6.3f"%(now(),self.name,wait)
tiempoDeUso=getNormal(self)
yield hold,self,tiempoDeUso
yield release,self,counter
print "%8.3f %s: Completed"%(now(),self.name)
else:
print "%8.3f %s: Waited %6.3f. I am off"%(now(),self.name,wait)
## Experiment data -------------------------
maxTime = 60*10.5 # minutes
maxWaitTime = 12.0 # minutes. maximum time to wait
## Model ----------------------------------
def model():
global counter
#seed(98989)
counter = Resource(name="Las maquinas",capacity=20)
initialize()
source = Source('Source')
firstArrival= expovariate(20.0/60.0) #chequear el expovariate
activate(source,
source.generate(number=99999),at=firstArrival)
simulate(until=maxTime)
def getNormal(self):
normal = normalvariate(40,20)
if (normal>=1):
return normal
else:
getNormal(self)
def getLambda (self):
actualTime=now()
if (actualTime <=60):
return 20.0/60.0
if (actualTime>60)and (actualTime<=120):
return 25.0/60.0
if (actualTime>120)and (actualTime<=180):
return 40.0/60.0
if (actualTime>180)and (actualTime<=240):
return 30.0/60.0
if (actualTime>240)and (actualTime<=300):
return 35.0/60.0
if (actualTime>300)and (actualTime<=360):
return 42.0/60.0
if (actualTime>360)and (actualTime<=420):
return 50.0/60.0
if (actualTime>420)and (actualTime<=480):
return 55.0/60.0
if (actualTime>480)and (actualTime<=540):
return 45.0/60.0
if (actualTime>540)and (actualTime<=600):
return 10.0/60.0
## Experiment ----------------------------------
model()
I think you want
return getnormal(self)
instead of
getnormal(self)
If the function exits without hitting a return statement, then it returns the special value None, which is a NoneType object - that's why Python complains about a 'NoneType.' The abs() function wants a number, and it doesn't know what to do with a None.
Also, you could avoid recursion (and the cost of creating a new stack frame) by using
def getNormal(self):
normal = 0
while normal < 1:
normal = normalvariate(40,20)
return normal
I am not entirely sure, but I think you need to change your method to the following:
def getNormal(self):
normal = normalvariate(40,20)
if (normal>=1):
return normal
else:
return getNormal(self)
You need to have:
return getNormal(self)
instead of
getNormal(self)
Really though, there's no need for recursion:
def getNormal(self):
normal = 0
while normal < 1:
normal = normalvariate(40,20)
return normal

Categories

Resources