Cant add variables to a class? - python

Ok, so i'm really starting to get annoyed at this. I can't seem to figure out what it is? Some are saying it is indentation but i've tried that.
Here is my code:
class player(object):
def __init__(self):
self.nation_name = None
self.race = None
self.name = None
self.special_unit = None
self.special_unitsize = 0
self.regular_army = 50
self.army_size = special_unitsize +regular_army
self.army_power = 250
self.IceColdCoins = 0
self.gold = 100
self.taxincome = 45
self.mineincome = 0
self.recruitrate = 30
self.totalincome = taxincome + mineincome
self.num_of_mines = 1
self.num_of_towns = 0
self.num_of_cities = 1
self.num_of_fort = 0
self.wait_till_upgrade = 0
And every time I try to add any type of variable it doesn't allow it saying:
attribute error (example) is not defined.
I did a test and well same error. Copy code and add a variable and make an if statement about the variable it won't work.

You forget to use self:
self.taxincome = 45
self.mineincome = 0
self.totalincome = taxincome + mineincome # there is no 'taxincome', etc.
The first two lines do not make taxincome and mineincome variables in the local (or global) namespace, but attributes of self:
self.taxincome = 45
self.mineincome = 0
self.totalincome = self.taxincome + self.mineincome # note the 'self'
The same goes for special_unitsize and regular_army.

Related

How do I print all of the instances from a set of variables that are undefined from the beginning?

I have a program that I want to be able to print all of the instances of each variable using my method that I created. Problem is I can't figure out a way to print them since each are listed under a different variable that aren't configured from hardcoding them in and I need a way to automatically recall them in my code.
class fit:
def __init__(self,day,did,workout='Not Recorded',time='An unknown amount of',calories='An unknown amount of'):
self.day = day
self.did = did
if did.lower()=='no':
self.workout = 'Not Recorded'
self.time = "An unknown amount of Minutes"
self.calories = "An unknown amount of Calories"
else:
self.workout = workout
self.time = "{} Minutes".format(time)
self.calories = "{} Calories".format(calories)
def formate(self):
self.formate = "{}:\n\nDid you work out: {}\nWorkout focus: {}\nYou worked out for: {}\nYou burned: {}\n\n----------------------------------------------------------".format(self.day,self.did,self.workout,self.time,self.calories)
return self.formate
def reader(day,index):
file = open('readme.txt')
file = file.read()
stripped = file.rsplit("\n")
for i in range(len(stripped)):
stripped[i] = stripped[i].rsplit(" ")
del stripped[-1]
if int(index) >= len(stripped[day-1]):
return "none"
else:
return stripped[day-1][index]
x = 0
def create_new_instance(class_name,instance_name):
globals()[instance_name] = class_name(reader(x,0),reader(x,1),reader(x,2),reader(x,3),reader(x,4))
print('Class instance {} created'.format(instance_name))
while True:
try:
x+=1
ins = 'day_' + str(x)
create_new_instance(fit,ins)
except:
break
break
def printer(instance):
print(.formate())
while True:
x+=1
inst = 'day_' + str(x)
printer(inst)
An example of this might be that I have 8 lines of data from a text document and I have a system that creates instances of day_1, day_2, day_3 ect until day_8 and then I want to print each of those instances out, but again I don't have those instances directly hardcoded into my code so I don't know how I'd do it. I've tried looking into maybe a while loop and increasing a variable by 1 and concatenating it with day and trying to make a variable out of that but the my limited experience with python isn't helping.
A very unpythonic and ugly way would be to use exec, for example:
day_3=5
x = 'day_'+'3'
exec("print("+x+")")
I would recommend another way to store your variables though.

Pythonic way to update multiple class variables in a loop?

I would like to update class variables with similar names in a loop:
I have the following code:
class Table:
def __init__(self):
pass
acc_counter = 0
acc0 = 0
acc1 = 0
acc2 = 0
acc3 = 0
acc4 = 0
I could update each value manually:
Table.acc0 = 0
Table.acc1 = 1
Table.acc2 = 2
Table.acc3 = 3
Table.acc4 = 4
However, I'm wondering if I could do it in a loop, something like that:
for i in range(5):
print(getattr(Table, f"acc{i}"))
#getattr(Table, f"acc{i}") = i
If the last line of the code is uncommented it returns: "SyntaxError: can't assign to function call"
You can use the setattr function:
for i in range(5):
setattr(Table, f"acc{i}", i)
Use setattr to set the object's attribute value
for i in range(5):
print(getattr(Table, f"acc{i}"))
setattr(Table, f"acc{i}", i)

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

Python how to reference variable

I'm getting a problem when referencing variables on a python file. Here is the code:
FG_E = 9
FG_R = 8
START = 7
READY = 9
MC = 3
BRAKE = 5
ERROR = 6
a = 2
b = 3
position = 0
def build_message(signal):
message = position
message = message | (0b1<<signal)
s = bin(message)
s = s[2:len(s)]
s = (16-len(s))*'0' + s
s0 = s[0:len(s)/2]
s1 = s[len(s)/2:len(s)]
s0 = s0[::-1]
s1 = s1[::-1]
s_final = int(s0 + s1, 2)
position = s_final
print bin(s_final)
return s_final
build_message(FG_R)
The error I get is:
UnboundLocalError: local variable 'position' referenced berofe assigment
The problematic line is actually position = s_final in the function build_message.
If it wasn't there then message = position would work because the Python interpreter would know to which position variable you are referring.
But in this case it is ambiguous because you're are later reassigning to position (position = s_final).
You should either re think the design of the code, or add global position as the first line in build_message. Keep in mind that as it says, it would make position a global variable and build_message will change the value of position every where throughout your code.
EDIT A quick demo:
global_var = 0
def foo1():
print(global_var)
def foo2():
print(global_var)
global_var = 1
def foo3():
global global_var
print(global_var)
global_var = 1
print(global_var)
foo1()
>> 0
foo2()
>> UnboundLocalError: local variable 'global_var' referenced before assignment
foo3()
>> 0
1
You need to use global keyword to access global variable.
def build_message(signal):
global position
message = position
If you are using an outside variable into a function maybe you should consider passing it as an argument, like:
def build_message(signal,position):
pass

How to create and keep a set of variables in 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.

Categories

Resources