How to create and keep a set of variables in python? - python

I'm developing an application that reads a message input from telegram with a set of variables, and then starts a game with the user. So I created a class that represents an instance of the game, making one game per chat possible:
class Battle:
def __init__(self, mainchat):
self.mainchat = mainchat
print('Instance of battle started on chat %s' % self.mainchat)
pcount = 0
team1 = []
team2 = []
p1 = ()
p2 = ()
p1score = 0
p2score = 0
battlechoicep1 = -1
battlechoicep2 = -1
so, as soon as I get a message, I start an instance of a battle based on user inputes, e.g.
battle = Battle(chat_id)
battle.p1 = 'Paul'
battle.battlechoicep1 = 4
...
this way has been working fine right now, but every time I want to reset the battle, I go through a function that does this:
battle.pcount = 0
battle.team1 = []
battle.team2 = []
battle.p1 = ()
battle.p2 = ()
battle.p1score = 0
battle.p2score = 0
battle.battlechoicep1 = -1
battle.battlechoicep2 = -1
save() # outside function that saves the scores into a pickle file
return
So, I would like to make it so this is a function inside my class, so everytime I call battle.reset it would call something like this
def reset():
battle.pcount = 0
battle.team1 = []
battle.team2 = []
battle.p1 = ()
battle.p2 = ()
battle.p1score = 0
battle.p2score = 0
battle.battlechoicep1 = -1
battle.battlechoicep2 = -1
save() # outside function that saves the scores into a pickle file
return
I don't know how is the right approach to this problem, I don't even know if what I've been doing up to now is 'correct' (it is working at least).
Creating the function inside the class (like def reset(self):) seems to have no effect.

You're on the right track with def reset(self). You just need to change the instances of battle to self in the method itself. NOTE: This needs to be a method of the Battle class.
def reset(self):
self.pcount = 0
... # etc
save() # outside function that saves the scores into a pickle file
When you pass in self as the first parameter of a class method, it allows the method to work on the instance of the class that you've called it on. If you just do def reset(self) without changing the battle to self, it will try to modify a variable in the current scope called battle, which in this case probably doesn't exist.
The other thing you could do if you just want reset to create a completely new object without preserving any of the attributes, you can just do:
def reset(self):
return Battle()

You're almost there!
class Battle:
def __init__(self, mainchat):
self.mainchat = mainchat
print('Instance of battle started on chat %s' % self.mainchat)
self.reset()
def reset(self):
self.team1, self.team2 = [], []
self.p1 = self.p2 = () #New tuples will be assigned and overwritten
self.pcount = self.p1score = self.p2score = 0
self.battlechoicep1 = self.battlechoicep2 = -1
save() # outside function that saves the scores into a pickle file
So when you need to reset, just call battle.reset()! Maybe the save function can also be a class method as well, just follow the same format.

Related

Timer Threading - AttributeError '...' has no attribute '...'

As I'm new in python maybe I'm missing the obvious.
I've wrote the updateRates method for the class which counts fps in this RDP program.
When I try to access some attributes from inside the call from the timer method there is this weird Error:
"AttributeError: type object 'RDCToGUI' has no attribute '_cnt_framerate'"
it is really obvious this class inherits this attribute, but in the Timer thread it doesn't seem to be there anymore
So the problem is: I can call the inherited methods of the super (e.g. self.rates) but inside some attributes aren't present in the method
class RDCToGUI(clientProtocol.rdc):
def __init__(self):
clientProtocol.rdc.__init__(self)
self.num = 0
self.count = 0
self.framerate_before = 0
def updateRates(self, framerate_label, datarate_label):
divisor = 1
datarate_text = "b/s"
self.rates(self)
framerate = self.framerate
datarate = self.datarate
if self.logged_in == 1 and self.framerate_before == 0 == framerate:
self.framebufferUpdateRequest(
width=800, height=800)
if datarate > 1000000:
divisor = 1000000
rateText = "Mb/s"
elif datarate > 1000:
divisor = 1000
rateText = "Kb/s"
self.framerate_before = framerate
framerate_label.setText(f"Framerate: {framerate}")
datarate_label.setText(
f"Datarate: {round(datarate / divisor, 2)} {datarate_text}")
threading.Timer(1, self.updateRates, args=(self,
framerate_label, datarate_label)).start()
class rdc(Protocol):
def __init__(self):
self._packet = ""
self._expected_len = 0
self._cnt_framerate = 0
self._cnt_datarate = 0
self.framerate = 0
self.datarate = 0
self.logged_in = 0
def rates(self):
self.setRates(self)
self.resetCounter(self)
def setRates(self):
self.framerate = self._cnt_framerate
self.datarate = self._cnt_datarate
def resetCounter(self):
self._cnt_datarate = 0
self._cnt_framerate = 0
def incFramerate(self):
self._cnt_framerate += 1
def addDataSize(self, size):
self._cnt_datarate += size
The classes have more methods and members than what I showed, but wanted to cut it to the most important stuff.
Tried to put the method in different classes and played around with the names of the attributes
class rdc is in another file which is imported as clientProtocol.
This seems like an inheritance issue. Maybe try replacing
clientProtocol.rdc.__init__(self)
with
super(RDCToGUI, self).__init__()
I'm not an expert on inheritance either and haven't really used it, but there is a ton to read on the super() function and you could maybe start at Understanding Python super() with __init__() methods and just follow it down the rabbit hole :)

How to create a new instance using a class method

I am trying to write a method for a class which will create a new instance of an already existing instance of a class. The problem is that I cannot access the new instance in the console when I try new_handname.
This is for creating a blackjack game in python. The idea of the code is when the hand is split a new instance will be created to create a new hand
import random
class Card(object):
def __init__(self, value, suit,nvalue):
self.value = value
self.suit = suit
self.nvalue = nvalue
suit = ['Hearts','Spades','Clubs','Diamonds']
value = ['2','3','4','5','6','7','8','9','10','J','Q','K','A']
nvalue = [2,3,4,5,6,7,8,9,10,10,10,10,11]
class Hand(object):
def __init__(self,current_hand):
self.current_hand = current_hand
def hand_total(self):
current_sum = 0
for i in range(0,len(self.current_hand)):
current_sum += self.current_hand[i].nvalue
return current_sum
def hand_type(self):
if self.current_hand[0].value == self.current_hand[1].value:
return('pair')
elif self.current_hand[0].value == 'A' or self.current_hand[1].value == 'A':
return('soft')
else:
return('hard')
def append(self,current_hand,some_card):
self.current_hand = self.current_hand + some_card
def hit(self):
self.current_hand.append(deck[0])
deck.pop(0)
def double(self,new_handname):
new_handname = Hand(self)
def deal_start_hand():
player_hand.append(deck[0])
deck.pop(0)
dealer_hand.append(deck[0])
deck.pop(0)
player_hand.append(deck[0]) #### player gets two cards ### assuming europe no hole card rules
deck.pop(0)
def gen_deck():
for v,n in zip(value,nvalue):
for s in suit:
deck.append(Card(v,s,n))
### variable initiation ###
deck = []
player_hand = []
dealer_hand = []
##program start ##
gen_deck()
random.shuffle(deck)
deal_start_hand()
p1 = Hand(player_hand)
p1.double('p2')
p2 ### I expect p2 to return an instance but does not
>>> p1
<__main__.Hand object at 0x00000006A80F0898>
>>> p2
Traceback (most recent call last):
File "<pyshell#182>", line 1, in <module>
p2
NameError: name 'p2' is not defined
Note: current_hand is a list of the cards objects.
I expect p2 to return an instance of the class but instead the variable p2 is not defined
Your split routine could look like the following, in which a new instance of the class is returned:
class Hand(object):
def __init__(self, current_hand):
self.current_hand = current_hand
def split(self):
return Hand(self.current_hand)
Simply create an instance, then split it later:
# You need to define "some_default" somewhere
myhand = Hand(some_default)
second_hand = myhand.split()
However, your split routine needs to take into account what cards have already been played, and what cards are still in the deck(s), none of which your code considers. I might advise mapping out the "states" of the game (think of it as a state machine), draw that out on paper, then consider how to code up each state and transition. A card game like this is more complicated to code than it might seem at first glance.

Access class variables with same function

I want to create a function within a class that can access two different members with the same function. For example in the code below, I want both of the lines below to use the 'apply' function on different variables in the class
print(state.apply(rate))
print(state.apply(wage))
I had thought if I put in a dummy variable in the function definition (called exposure), it would replace it with the variables passed to the function (rate and wage in the example below). What is the correct way of doing this in python 3?
class State():
def __init__(self):
self.rate = 0
self.wage = 0
def apply(self, exposure):
self.exposure = self.exposure - 1
return self.exposure
state = State()
rate = State.rate
wage = State.wage
print(state.apply(rate))
print(state.apply(wage))
EDIT: I had made a typo where I had State instead of state in each print statement. I have now corrected this
This would be the only way:
class State:
def __init__ (self):
self.rate = 0
self.wage = 0
def apply (self, exposure):
setattr(self, exposure, getattr(self, exposure) - 1)
return getattr(self, exposure)
>>> state = State()
>>> print(state.apply('rate'))
-1
>>> print(state.apply('wage'))
-1
>>> print(state.apply('wage'))
-2
Note that those are instance variables, so you cannot access them using the type, State, but only using the object, state.
However, I would say, that whatever you are trying, you’re doing it wrong. If you describe your actual problem, we may be able to suggest a way better solution for it instead.

Get the return value from a function in a class in Python

I am trying to simply get the value out of my class using a simple function with a return value, I'm sure its a trivial error, but im pretty new to python
I have a simply class set up like this:
class score():
#initialize the score info
def __init__(self):
self.score = 0
self.num_enemies = 5
self.num_lives = 3
# Score Info
def setScore(num):
self.score = num
# Enemy Info
def getEnemies():
return self.num_enemies
# Lives Info
def getLives():
return self.getLives
etc.....
Than I create an instance of the class as such:
scoreObj = score()
for enemies in range(0, scoreObj.getEnemies):
enemy_sprite.add(enemy())
I get the error saying that an integer is expected, but it got an instancemethod
What is the correct way to get this information?
Thanks!
scoreObj.getEnemies is a reference to the method. If you want to call it you need parentheses: scoreObj.getEnemies().
You should think about why you are using a method for this instead of just reading self.num_enemies directly. There is no need for trivial getter/setter methods like this in Python.
The first parameter for a member function in python is a reference back to the Object.
Traditionally you call it "self", but no matter what you call the first parameter, it refers back to the "self" object:
Anytime I get weird errors about the type of a parameter in python, I check to see if I forgot the self param. Been bit by this bug a few times.
class score():
#initialize the score info
def __init__(self):
self.score = 0
self.num_enemies = 5
self.num_lives = 3
# Score Info
def setScore(self, num):
self.score = num
# Enemy Info
def getEnemies(self):
return self.num_enemies
# Lives Info
def getLives(foo): #foo is still the same object as self!!
return foo.num_lives
#Works but don't do this because it is confusing
This code works:
class score():
def __init__(self):
self.score = 0
self.num_enemies = 5
self.num_lives = 3
def setScore(self, num):
self.score = num
def getEnemies(self):
return self.num_enemies
def getLives(self):
return self.getLives
scoreObj = score()
for enemy_num in range(0, scoreObj.getEnemies()):
print enemy_num
# I don't know what enemy_sprite is, but
# I commented it out and just print the enemy_num result.
# enemy_sprite.add(enemy())
Lesson Learned:
Class functions must always take one parameter, self.
That's because when you call a function within the class, you always call it with the class name as the calling object, such as:
scoreObj = score()
scoreObj.getEnemies()
Where x is the class object, which will be passed to getEnemies() as the root object, meaning the first parameter sent to the class.
Secondly, when calling functions within a class (or at all), always end with () since that's the definition of calling something in Python.
Then, ask yourself, "Why am I not fetching 'scoreObj.num_lives' just like so instead? Am I saving processing power?" Do as you choose, but it would go faster if you get the values directly from the class object, unless you want to calculate stuff at the same time. Then your logic makes perfect sense!
You made a simple mistake:
scoreObj.getEnemies()
getEnemies is a function, so call it like any other function scoreObj.getEnemies()

Class does not seem to be Global in python

I setup a class and it accepts and prints out the variables fine in one if statement.
class npc: #class for creating mooks
def __init__(self, name):
self.name = name
def npc_iq (self,iq):
self.iq = []
def npc_pp (self,pp):
self.pp = []
def npc_melee (self, melee):
self.melee = []
def npc_ct (self, ct):
self.ct = []
It works fine in this if statement
if menu_option == 1:
print "Choose melees for npc"
init_bonus = random.randint(0,2)
char_PP = random.randint(7,15)
char_iq = random.randint(7,15)
npc_Melees = int(raw_input(prompt))
combat_time = math.floor((round_attacks - init_bonus - math.floor(char_PP/2) - math.floor(char_iq/2)) / npc_Melees)
#function for calculating sequence number
print "combat time is"
print combat_time
mook = "mook%s" % counter # adds different mook names to program
mook = npc(mook)
mook.iq = (char_iq)
mook.pp = (char_PP)
mook.melee = (npc_Melees)
mook.ct = (combat_time)
counter += 1
But on this statement it will print out the name in the class but not ct.
elif menu_option ==4:
print "Printing out all mooks"
print
printcount = counter -1
while printcount != 0:
mookprint = "mook%s" % printcount
mookprint = npc(mookprint)
print mookprint.name
print mookprint.ct
print
printcount -= 1
Why would a mookprint have any idea what value ct should be? The constructor for npc initialises a new instance of npc, with the name given as a parameter, but ct is left empty.
When you create an NPC in menu option 1, you do not create a global instance of npc. If you want to refer to a previously created instance of npc, you will need to find some way of storing them. Dictionaries may be a good solution for you. A dictionary is an object that holds mappings between keys and values. If you know the key, then you can find the assosicated value. In this case you would make name the key and the value the npc instances.
eg.
npcsDict = dict()
if menu_option == 1:
# code for intialising a new instance of npc
...
# most, if not all of the initialisation code should be moved to the
# __init__ method for npc
# now store the newly created mook
npcsDict[mook.name] = mook
elif menu_option == 4:
print "Printing out all mooks"
print
for mookName in npcsDict:
print npcsDict[mookName].name
print npcsDict[mookName].ct
print
i dont really understand your problem.
your working example:
mook = npc(mook)
mook.iq = (char_iq)
mook.pp = (char_PP)
mook.melee = (npc_Melees)
mook.ct = (combat_time)
mook.ct is value of (combat_time)
your failing example:
mookprint = npc(mookprint)
print mookprint.name
print mookprint.ct
mookprint.ct's value is nothing because it is never set.
The elif will only be executed if the if has not, so if the elif block runs, ct was never set
I don't think you're understanding how four lines work:
mookprint = "mook%s" % printcount
mookprint = npc(mookprint)
print mookprint.name
print mookprint.ct
Every time this block of code is run, the following things are happending:
You're assigning a string of the form "mook" to the variable mookprint
You're creating a new instance of the npc class. You should note that all of the instances you're creating will be separate from eachother. This new instance will have an attribute with the name that was previously held in the variable mookprint and this instance of npc will be assigned to mookprint.
You're printing the name attribute of the instance of the npc class that you created in the previous step. This works because when this instance was created, the __init__ method of your class was called with the argument name being set to "mook1" or whatever was stored in mookprint at the time.
You're printing the ct attribute of the instance of the npc class that you just created. Since you never set the ct attribute to anything, this will not work how you expected.
If you want to count the number of instances of your npc class, you'll need to create a class attribute. This is a variable whose value is common across all instances of a class. To do so, you'll need to modify your class definition to add an item to this attribute every time you make a new instance of the class. It will look something like this:
class npc: #class for creating mooks
ct = []
def __init__(self, name):
self.name = name
self.ct.append(name)
def get_ct(self):
return len(self.ct)
With the above, the variable ct will be a list that is common to all instances of npc and will grow every time a new npc is created. Then the method get_ct will count how long this list is.
Then you'll need to modify the four lines I mentioned to look like:
mookprint = "mook%s" % printcount
mookprint = npc(mookprint)
print mookprint.name
print mookprint.get_ct()
I think the code above shows how to change your code to work more how you expected it to work. However, it should be noted that you rarely want to create classes where each instance depends on information about the other instances. It is usually a better design to do something like Dunes suggested, storing the instances in a dictionary, or some other data structure, and keeping track of them that way.

Categories

Resources