python - class calls require me to send self, SpeechEngine.test_choice(SpeechEngine) - python

I'm having some issues where whenever I make a call to one of my classes methods it's requiring me to specifically send the containing class with the call, where I would expect it to already know about it self. I'm sure this is user error but can not track it down.
I've referenced python - self - required positional argument but i think i've got that covered.
class SpeechEngine():
def __init__(self):
self.conn = sqlite3.connect('../twbot.db')
self.c = self.conn.cursor()
#staticmethod
def choose(choice):
num_choices = len(choice)
selection = random.randrange(0, num_choices)
return selection
def initial_contact_msg(self, userId, screenName):
hello = self.c.execute("SELECT text, id FROM speechConstructs WHERE type='salutation'").fetchall()
tagline = self.c.execute("SELECT text, id FROM speechConstructs WHERE type='tagline'").fetchall()
c1 = self.choose(hello)
c2 = self.choose(tagline)
msg_string = str(hello[c1][0]) + ' #' + screenName + ' ' + tagline[c2][0]
# print(msg_string) # For Testing Only
# print(hello[c1][1]) # For Testing Only
return msg_string
And then I would expect to call
SpeechEngine.initial_contact_msg(0, 'somename')
But that returns the following
missing 1 required positional argument: 'self'
Where as if i do it implicitly
SpeechEngine.initial_contact_msg(SpeechEngine, 0, 'somename')
It returns the expected results no questions asked.
I should also point out the same happens when i would assign it as follows.
test = SpeechEngine
test.initial_contact_msg(0, 'somename')

Since initial_contact_msg is a method, you need to call it from an instance, not the Type. Your last attempt is almost right. To instantiate it, you need to do the following:
test = SpeechEngine()
test.initial_contact_msg(0, 'sometime')
"SpeechEngine" is the type class. When you create a new instance you need to call it like a function. This is similar to using the "new" keyword in other languages.
When you have a static method, this can be called directly from the Type object:
SpeechEngine.choose()
You can read more in the Python Documentation.

Related

How to update variables passed between classes?

I'm trying to pass variables between different classes. In order to accompish this task, I have created an info class (here called 'declaration') so that the code reads:
class declaration():
def __init__(self):
self.info1 = 999
self.info2 = 'something_else'
print ('At declaration ')
class controller():
def __init__(self):
instance = declaration()
print ('Initial number ',instance.info1, instance.info2)
modifier(declaration)
print ('MIDDLE ',instance.info1,declaration.info1)
instance = declaration()
print ('Final number ',instance.info1)
class modifier():
def __init__(self,aux):
print ('MODIFIER')
self.info=aux
self.info.info1=55555
controller()
The output is:
At declaration
Initial number 999
something else
MODIFIER
MIDDLE 999 55555
At declaration
Final number 999
However, I'm not really sure about some of the inners of the code. I have one major question and a minor one. My main question is that when the class 'modifier' is modified according to:
class modifier():
def __init__(self,aux):
self.info=aux
print ('MODIFIER',self.info.info1)
self.info.info1=55555
it produces the error AttributeError: type object 'declaration' has no attribute 'info1' [Flipping the last 2 lines fixes the error]. It's confusing (at least to me) whether the class attributes are not passed or they have to be reinitialized.
The second question is how to update instance once its class has been updated. The second call to instance = declaration() seems to accomplish nothing.
Quick side note: Yes I do realise, I just want to say please try to follow the PEP8 python guide as it makes your code look cooler (and easier to read) and all the cool kids use it.
There are a few things wrong with your code, calling modifier(declaration) actually makes the aux parameter an uninitilized class, you want to call modifier(instance) as the init function has already been ran.
Also it would be easier to drop the self.info = aux as you can just call aux.info1 and it looks cleaner and is actually faster (Because you are calling one less Fast Store command in bytecode).
Lastly at print ('MIDDLE ',instance.info1,declaration.info1) you again parse declaration uninitilized therefore you get the error AttributeError: type object 'declaration' has no attribute 'info1', to fix this simply put declaration().info1 as that calls the init function (it is the same as saying declaration.__init__().info1).
So finally you get:
class declaration():
def __init__(self):
self.info1 = 999
self.info2 = 'something_else'
print ('At declaration ')
class controller():
def __init__(self):
instance = declaration()
print ('Initial number ', instance.info1, instance.info2)
modifier(instance)
print ('MIDDLE ', instance.info1, declaration().info1)
instance = declaration()
print ('Final number ',instance.info1)
class modifier():
def __init__(self, aux):
print ('MODIFIER')
aux.info1 = 55555
controller()
Hope this helped.

Issues with 'self' argument and python requests module

Having an issue calling function dynamic_exit from class dynamic, called from an imported python file within the project file. Only including enough code to depict an efficient example of my issue.
Call example below:
from lib.core import dynamic
import ...
if requests.get(url).status_code != 200:
clear()
print(" xxxxx \n\n\n")
print("[ !! | Invalid URL ] Status code: {0}".format(
str(requests.get(url).status_code)))
time.sleep(1)
print("\n\n Please enter a valid URL.\nExiting...")
dynamic.dynamic_exit(self=dynamic())
time.sleep(3)
exit()
Lib.core contains:
class dynamic:
def __init__(self):
self.loadSwitch = False
self.analyzeSwitch = False
self.exitSwitch = False
def dynamic_load(self, loadSwitch=True):
self.loadSwitch = loadSwitch
done = False
for c in itertools.cycle(['[ | ]', '[ / ]', '[ - ]', '[ \\ ]']):
if done:
break
sys.stdout.write('\rLoading ' + c)
sys.stdout.flush()
time.sleep(0.1)
# Further along...
if dynamic.dynamic_analyze(): # Separate function -- Irrelevant
t = threading.Thread(target=dynamic_analyze())
t.start()
elif dynamic_exit(): # Separate function -- Irrelevant
t2 = threading.Thread(target=dynamic_exit())
t2.start()
else: # dynamic_load -- Example function
t3 = threading.Thread(target=dynamic_load())
t3.start()
sys.stdout.write('\r[ ✓ ] Process Complete ')
time.sleep(4.5)
done = True
loadSwitch = False
exitSwitch = False
analyzeSwitch = False
Lord, I know it's a mess. First time actually working with classes like this.
Error is as follows:
File "~/test-delete.py", line 11, in <module>
from lib.core import dynamic
File "~/lib/core.py", line 55, in <module>
if dynamic.dynamic_analyze():
TypeError: dynamic_analyze() missing 1 required positional argument: 'self'
The IDE is wanting more than a simple self parameter, it is recommending self=. So unsure of how to handle this.
Basically, need help under the context of the __init__ function and using the self parameter. Trying to call the function setting either exitSwitch, analyzeSwitch, or loadSwitch = True, ifswitch == True, perform either function dynamic_load, dynamic_exit, or dynamic_analyze. Post-completion, set all switches back to False.
The problem is that you are calling instance methods as if they are static methods. In other words, you call the methods as dynamic.dynamic_analyse() where dynamic is a reference to the class, not to an instance of that class.
So proceed as follows:
Name your class with PascalCase -- a common practice to distinguish classes from other things. So yours should be named Dynamic.
Create an instance and assign it to a variable. This one could actually get the name dynamic with lower case initial letter.
Don't pass an instance as argument when calling methods on the instance. Because in the notation a.b(), b will be called with the value for self set to a.
So define the class as:
class Dynamic:
def __init__(self):
self.loadSwitch = False
self.analyzeSwitch = False
self.exitSwitch = False
# ...etc.
Import the class and create an instance like this:
from lib.core import Dynamic
# ...
dynamic = Dynamic() # Create instance
# ...
if dynamic.dynamic_analyze(): # Call method on the instance
# ..etc
Your exit code should have:
dynamic.dynamic_exit() # No argument.
I cannot comment on the rest of your code, as it is hard to tell what it is doing. For instance, I do wonder why you call dynamic_analyse() twice... but this at least will solve the problem with the error you got.

Python unit testing on class methods with no input arguments

Given a class with class methods that contain only self input:
class ABC():
def __init__(self, input_dict)
self.variable_0 = input_dict['variable_0']
self.variable_1 = input_dict['variable_1']
self.variable_2 = input_dict['variable_2']
self.variable_3 = input_dict['variable_3']
def some_operation_0(self):
return self.variable_0 + self.variable_1
def some_operation_1(self):
return self.variable_2 + self.variable_3
First question: Is this very bad practice? Should I just refactor some_operation_0(self) to explicitly take the necessary inputs, some_operation_0(self, variable_0, variable_1)? If so, the testing is very straightforward.
Second question: What is the correct way to setup my unit test on the method some_operation_0(self)?
Should I setup a fixture in which I initialize input_dict, and then instantiate the class with a mock object?
#pytest.fixture
def generator_inputs():
f = open('inputs.txt', 'r')
input_dict = eval(f.read())
f.close()
mock_obj = ABC(input_dict)
def test_some_operation_0():
assert mock_obj.some_operation_0() == some_value
(I am new to both python and general unit testing...)
Those methods do take an argument: self. There is no need to mock anything. Instead, you can simply create an instance, and verify that the methods return the expected value when invoked.
For your example:
def test_abc():
a = ABC({'variable_0':0, 'variable_1':1, 'variable_2':2, 'variable_3':3))
assert a.some_operation_0() == 1
assert a.some_operation_1() == 5
If constructing an instance is very difficult, you might want to change your code so that the class can be instantiated from standard in-memory data structures (e.g. a dictionary). In that case, you could create a separate function that reads/parses data from a file and uses the "data-structure-based" __init__ method, e.g. make_abc() or a class method.
If this approach does not generalize to your real problem, you could imagine providing programmatic access to the key names or other metadata that ABC recognizes or cares about. Then, you could programmatically construct a "defaulted" instance, e.g. an instance where every value in the input dict is a default-constructed value (such as 0 for int):
class ABC():
PROPERTY_NAMES = ['variable_0', 'variable_1', 'variable_2', 'variable_3']
def __init__(self, input_dict):
# implementation omitted for brevity
pass
def some_operation_0(self):
return self.variable_0 + self.variable_1
def some_operation_1(self):
return self.variable_2 + self.variable_3
def test_abc():
a = ABC({name: 0 for name in ABC.PROPERTY_NAMES})
assert a.some_operation_0() == 0
assert a.some_operation_1() == 0

NameError: name 'getTempo' is not defined

i'm getting an error defining function "getTempo" and i don't know why... Thanks for the help.
example:
L=[Musica("aerossol",4.9),Musica("lua",5.3),Musica("monte",3.2),Musica("rita",4.7)];getTempo("lua",L)
should give:
lua:5.3
5.3
class Musica:
def __init__(self,nome,tempo):
self.nome=nome
self.tempo=tempo
def __repr__(self):
return self.nome+":"+str(self.tempo)
def getTempo(nomeMusica,ListaMusicas):
if ListaMusicas==[]:
print ("Inexistente")
else:
meio=len(ListaMusicas)//2
print (ListaMusicas[meio])
A = [i[0] for i in ListaMusicas]
B = [i[1] for i in ListaMusicas]
if nomeMusica==A[meio]:
print (B[meio])
elif nomeMusica<A[meio]:
return getTempo(nomeMusica,ListaMusicas[:meio])
else:
return getTempo(nomeMusica,ListaMusicas[(meio+1):])
In python, unlike languages like Java or C++, instance attributes and methods must be accessed on the instance, so you must write self.getTempo in order for getTempo to resolve.
EDIT - Selective Reading Failure
You also need to make sure that all method definitions include an argument for the class instance itself, which will be the first argument passed. By convention, this is the self argument, but it can be any name you choose. Here is the modified function definition:
def getTempo(self, nomeMusica,ListaMusicas): # Changed
if ListaMusicas==[]:
print ("Inexistente")
else:
meio=len(ListaMusicas)//2
print (ListaMusicas[meio])
A = [i[0] for i in ListaMusicas]
B = [i[1] for i in ListaMusicas]
if nomeMusica==A[meio]:
print (B[meio])
elif nomeMusica<A[meio]:
return self.getTempo(nomeMusica,ListaMusicas[:meio]) # Changed
else:
return self.getTempo(nomeMusica,ListaMusicas[(meio+1):]) # Changed

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

Categories

Resources