So I am trying to setup a unittest for a threaded class in python
The class looks like this:
#Singleton
class EventManager(threading.Thread):
def __init__(self):
self.__eventDict = {}
self.__eventDictLock = threading.Lock()
self.__eventQueue = Queue()
self.__subscriberList = []
threading.Thread.__init__(self)
def run(self):
while(True):
if self.__eventQueue.qsize() > 0:
self.__runEvent()
else:
time.sleep(1)
My unittest looks like this:
eventManager = EventManager.Instance()
eventManager.start()
class EventManagerTest(unittest.TestCase):
#pre-test initialization
def setUp(self):
pass
#post-test destruction
def tearDown(self):
pass
def testRegisterEvent(self):
global eventManager
logger.debug("Entering testRegisterEvent()")
eventManager.registerEvent("MyEvent")
logger.debug("testRegisterEvent() appears to have successfully registered")
self.assertIn("MyEvent", eventManager.__subscriberList)
self.assertFalse( eventManager.__eventDict["MyEvent"])
And I'm getting an error like this:
ERROR: testRegisterEvent (__main__.EventManagerTest)
Traceback (most recent call last):
File "EventManager_Test.py", line 59, in testRegisterEvent
self.assertIn("MyEvent", eventManager.__eventDict)
AttributeError: 'EventManager' object has no attribute '\_EventManagerTest\__eventDict'
Where is the _EventManagerTest__eventDict attribute coming from? That is not the attribute I'm calling and it's preventing me from running a unit test.
As __eventDict starts with two underscore, it is a private attribute, therefore its name is “mangled”, that's why the name changed.
The problem is not related to unittest, it is just that you are trying to access a private attribute.
To solve your problem, remove one (or two) underscore(s) at the beginning of the name of __eventDict to make it protected (or public).
Related
I wanted to define a class as follows:
class ConfigManager:
#classmethod
def load_config(cls):
# do some complex stuff to load something
return config
__CONFIG = ConfigManager.load_config()
#classmethod
def get_config(cls):
return cls.__CONFIG
And then when I run the following code, it reports a NameError:
x = ConfigManager.get_config()
Traceback (most recent call last):
File "test.py", line 1, in <module>
class ConfigManager:
File "test.py", line 7, in ConfigManager
__CONFIG = ConfigManager.load_config()
NameError: name 'ConfigManager' is not defined
Why does this error occur? Is it because Python code is executed by interpretation and when it goes to line 7, class ConfigManager is not yet finished to be defined?
A class object only exists by its name after the class body and all class decorators are evaluated. What that means is that you cannot use the name ConfigManager inside the ConfigManager body itself. This also includes any function or method you call before the class is completed.
While building the class body, you can refer to names previously defined in the class body and names previously defined outside of the class body.
external_name = 'External'
class Demo:
internal_name = 'Internal'
print('Body see', external_name, internal_name)
# throws an error
print('Body does not see', late_internal_name, late_external_name)
late_internal_name = 'LateInternal'
late_external_name = 'LateExternal'
This means you can define a function to load your configuration as long as it does not need the class object. Note that even when defined inside the class, this is not a (class-) method by the time you access it.
class ConfigManager:
# not a method - does not receive cls/self
def load_config():
# do some complex stuff to load something
return {}
# call helper as a regular function
__CONFIG = load_config()
# clean up helper since it is not a proper method
del load_config
#classmethod
def get_config(cls):
return cls.__CONFIG
Alternatively, you can lazily load the config if needed.
class ConfigManager:
_CONFIG = None
#classmethod
def _load_config(cls):
# do some complex stuff to load something
return {}
#classmethod
def get_config(cls):
if cls._CONFIG is None:
cls._CONFIG = cls._load_config()
return cls._CONFIG
I wonder if it is possible to set variables of a class by a different class using cls?
The story behind it:
I'm writing tests for different purposes but see that one part of the setup is the same as in an already existing class. So I would do the setUp by the already existing one:
The original code:
class TestBase(unittest.TestCase):
def setUp(self):
self.handler = txt.getHandler(hcu.handler3())
self.curves = self.handler.curves()
self.arguments = (getInit())
self.ac = self.connect2DB(self.arguments)
self.au = AutoUtils()
This has worked well so far.
Now in my TestClient I'd like to make use of that:
from .testsDB import TestBase as tb
class TestClient(unittest.TestCase):
def setUp(self):
tb.setUp()
And modified in the TestBase the setUp to the following:
#classmethod
def setUp(cls):
cls.handler = txt.getHandler(hcu.handler3())
cls.graph = cls.handler.curves()
cls.argv = (getInit())
cls.ac = cls.connect2DB(cls.arguments)
cls.au = AutoUtils()
But I'm getting an error as soon as I use one of the values defined in the variables of the TestClient-class:
def test_Duplicates(self):
self.testDB = self.ac.connect(self.ac.client, self.arguments[4])
With the error:
In test_Duplicate (Curves.tests_client.TestClient) :
Traceback (most recent call last):
File "/home/qohelet/Curves/tests_client.py", line 49, in test_Duplicate
self.testDB = self.ac.connect(self.ac.client, self.arguments[4])
AttributeError: 'TestClient' object has no attribute 'ac'
Is it actually possible what I'm trying?
EDIT:
After writing this and seeing the answers I did another review. Yes indeed there is a circular issue I'm having.
TestBase has the function connect2DB which will be executed on setUp.
If it refers to itself (as in the original) it's fine.
If I replace the self with cls it will try to execute TestClient.connect2DB in the setUp - which does not exist. So it would require self again as putting connect2DB into TestClient is not an option.
How to solve that?
Surely your new class should just inherit the setup()?
from .testsDB import TestBase as tb
class TestClient(tb):
def test_Duplicates(self):
self.testDB = self.ac.connect(self.ac.client, self.arguments[4])
The whole point of inheritance is that you don't modify what you inherit from. Your new class should just make use of what is supplied. That is why inheritance is sometimes called programming by difference.
In writing a Python (2.5) program, I tried to create a class and, in its __init__ function, automatically create an instance of another class with its name as an argument to the __init__ function, something like this:
class Class1:
def __init__(self,attribute):
self.attribute1=attribute
class Class2:
def __init__(self,instanceName):
#any of Class2's attributes
exec instanceName + '=Class1('attribute1')'
# this should produce an instance of Class1 whose name is instanceName
But when I make an instance of Class2, instance=Class2('instance2'), and try to get attribute1 of instance2 (which should have been created from Class2's __init__ function) I get an error message:
Traceback (most recent call last):
File "<pyshell#29>", line 1, in <module>
print instance2.attribute1
NameError: name 'instance2' is not defined
I don't know what the problem is, since name='instance3' and
exec name+'=Class1('attribute1') does work, though this is probably because I don't have much experience with Python. How would I be able to do something like this automatically when an instance is created?
I have to run, so hopefully, someone else can fix any mistakes in this post:
class Class1:
def __init__(self, attribute):
self.attribute1 = attribute
class Class2:
def __init__(self, instanceName):
setattr(self, instanceName, Class1(...)) # replace ... with whatever parameters you want
I can't really understand what I'm doing wrong, since when I try it in "small scale" and it is working there.
I have a class named Play()
I goes like this:
class Play():
def __init__(self):
file = open("/home/trufa/Desktop/test", "r")
self.word = random.choice(file.readlines()).rstrip()
self.errAllowed = 7
self.errMade = 0
self.errList = []
self.cheatsAllowed = 2##chetas not incrementing
self.cheatsMade =0
self.wordList = ["*"]*len(self.word) ##this one is the one I want to have available in another class
...
Then I have another class called Score()
class Score(Play):
def __init__(self):
self.initialScore = 0
def letterGuess(self):
self.initialScore += 1
return self.errList
...
I instantiated both:
game = Play()
points = Score()
And if I do:
print points.letterGuess()
It gives me an error:
Traceback (most recent call last):
File "/home/trufa/workspace/hangpy/src/v2.py", line 188, in <module>
startGame()
File "/home/trufa/workspace/hangpy/src/v2.py", line 134, in startGame
print points.letterGuess()
File "/home/trufa/workspace/hangpy/src/v2.py", line 79, in letterGuess
return self.errList
AttributeError: Score instance has no attribute 'errList'
I don't understand why since I can do this without any trouble:
class One():
def __init__(self):
self.list= [1,2]
class Two(One):
def meth(self):
return self.list
uan = One()
tu = Two()
print uan.list
print tu.meth() ## Both output [1,2]
I'm very new to OOP so I could be doing all kinds of silly mistakes but I can't figure out where!
I think I have posted all the relevant code, but I you think the error might be elsewhere, I can provide it.
As I said I'm very new, so this might have nothing to do with inheritance I just think it called that when you get "something" from within another class (you must be shouting at the screen by now)
You overwrite the original __init__, which is then never called and doesn't initialize the members. You must call the parent's __init__ separately, usually with this snippet:
def __init__(self):
super(Score, self).__init__()
See the docs for super() for details. However, super() only works for so-called new-style classes. You must therefore either change the definition of Play to inherit from object:
class Play(object)
or you call the parent's method directly:
def __init__(self):
Play.__init__(self)
When you inherit from the class Play, you automatically get the attributes that you've created in the definition of Play, but you don't get the attributes that you've created in Play.__init__. You have to explicitly call it like so:
class Score(Play):
def __init__(self):
Play.__init__(self)
self.initialScore = 0
See Boldewyn's suggestion for using super to do this; but IMO you should probably get used to the basic way inheritance works before fiddling with super.
To further clarify, if you don't override __init__ as you have in this case, then it's inherited and called automatically.
You forgot to initialize the superclass.
class Score(Play):
def __init__(self):
super(Score, self).__init__()
self.initialScore = 0
If I have a python class, how can I alias that class-name into another class-name and retain all it's methods and class members and instance members? Is this possible without using inheritance?
e.g. I have a class like:
class MyReallyBigClassNameWhichIHateToType:
def __init__(self):
<blah>
[...]
I'm creating an interactive console session where I don't want my users' fingers to fall off while instantiating the class in the interactive sessions, so I want to alias that really long class name to something tiny like 'C'. Is there an easy way to do this without inheritance?
C = MyReallyBigClassNameWhichIHateToType
Also, if you're importing the name from another module...
from modulename import ReallyLongNameWhichIHateToType as FriendlyName
You can simply do:
ShortName = MyReallyBigClassNameWhichIHateToType
A class in Python is just an object like any other, and can have more than one name.
Refactor the name, no reason it should have a name that long.
Otherwise whateverName = VeryLongClassName should do the trick.
Simple name assignment approach works but has one disadvantage that might be important in some cases: the alias name will be the same as the name of the "base" class because of the __name__ property.
class C(object):
pass
D = C
print(C.__name__) # 'C'
print(D.__name__) # 'C' again
For example, if you create custom exception and then another one that assigning the first one you will get the name of this "parent" exception every time no matter which one of them you raise and this should probably confuse a user:
class CustomBaseException(Exception):
def __init__(self, operation):
super(CustomBaseException, self).__init__()
self.operation = operation
def __str__(self):
return f"Invalid operation '{self.operation}'"
OperationException = CustomBaseException
raise OperationException('readd')
output:
Traceback (most recent call last):
File "<input>", line 12, in <module>
CustomBaseException: Invalid operation 'readd'
So a solution would be to actually subclass the class:
class CustomBaseException(Exception):
def __init__(self, operation):
super(CustomBaseException, self).__init__()
self.operation = operation
def __str__(self):
return f"Invalid operation '{self.operation}'"
class OperationException(CustomBaseException):
pass
raise OperationException('delite')
output:
Traceback (most recent call last):
File "<input>", line 14, in <module>
OperationException: Invalid operation 'delite'