I have simple task: we have class TestingClass and we wanna simply test his methods. And we have class TestHelper for testing purpose.
class TestingClass():
def testing_method_1(self):
return False
def testing_method_2(self):
return True
class TestHelper():
def __init__(self, class_name):
pass
def add(self, func_name, func_result):
pass
def run(self):
pass
helper = TestHelper(TestingClass)
helper.add(“testing_method_1”, False)
helper.add(“testing_method_2”, True)
result = helper.run()
my target implement methods for class TestHelper . I solved it for myself but I think its bad realization. I initialize empty dictionary and test class. Next I add every method and test method result to dictionary. In run method I compare results.
class TestHelper():
def __init__(self, class_name):
self.test_class = class_name()
self.func_list = {}
def add(self, func_name, func_result):
temp = 'self.test_class.' + func_name
self.func_list[temp] = func_result
def run(self):
for f in self.func_list.keys():
if eval(f)() == self.func_list[f]:
print('yes')
else:
print('no')
Can I resolve it in best way? Thanks!
Using eval for this purpose is overkill.
You could instead use the getattr() function to retrieve the function and call it.
Read more: Python Docs
class TestHelper():
def __init__(self, class_name):
self.test_class = class_name()
self.func_list = {}
def add(self, func_name, func_result):
# { <function>:<desired result> }
self.func_list[func_name] = func_result
def run(self):
for func, desired_result in self.func_list.items():
if getattr(self.test_class, func)() is desired_result:
print('yes')
else:
print('no')
This code produces results:
...
>>> helper = TestHelper(TestingClass)
>>> helper.add("testing_method_1", False)
>>> helper.add("testing_method_2", True)
>>> result = helper.run()
yes
yes
Of course you should also test if the class even has an attribute with the given function name. You can use hasattr() for this.
This question already has answers here:
Assign class boolean value in Python
(2 answers)
Defining "boolness" of a class in python
(3 answers)
Closed 2 years ago.
I have defined a class in Python 3 and have a case that I create an "empty" class object. Therefor I want to be able to check if the object is empty or not, like you can write:
test = []
if not test:
print('False')
My code for the class looks like this (note that I will accept an empty value for name):
class myClass:
def __init__(self, name=False):
self.name = name
def __str__(self):
return self.name.replace('\n', ' ')
def __repr__(self):
return self.name.replace('\n', ' ')
def __eq__(self, other):
if not isinstance(other, myClass):
return False
if not self.name and not other.name:
pass
elif not (self.name or other.name):
return False
elif self.name != other.name:
return False
return True
Now I would like to check if I got an empty class:
test = myClass()
if not test:
print('False')
else:
print('True')
The result of this case will always be true. How can I change this behaviour?
Add a __bool__ method to your class:
def __bool__(self):
return bool(self.name)
To cater for the case where self.name == '' and you want to return True:
def __bool__(self):
return self.name is not False
say class instance is printHello for class Hello
Now when I execute below code
print printHello
The output is "HelloPrinted"
Now I want to compare the printHello with a string type and cannot achieve it because printHello is of type instance.
Is there a way to capture the output of print printHello code and use it for comparison or convert the type of printHello to string and I can use it for other string comparisons?
Any help is appreciated.
If you want to specifically compare to strings you could do it in two different ways. First is to define the __str__ method for your class:
class Hello:
def __init__(self, data="HelloWorld"):
self._data = data
def __str__(self):
return self._data
Then you can compare to a string with:
h = Hello()
str(h) == "HelloWorld"
Or you could specifically use the __eq__ special function:
class Hello:
def __init__(self, data="HelloWorld"):
self._data = data
def __str__(self):
return self._data
def __eq__(self, other):
if isinstance(other, str):
return self._data == other
else:
# do some other kind of comparison
then you can do the following:
h = Hello()
h == "HelloWorld"
A special method __repr__ should be defined in your class for this purpose:
class Hello:
def __init__(self, name):
self.name= name
def __repr__(self):
return "printHello"
Either define str or repr in Hello class
More information here - https://docs.python.org/2/reference/datamodel.html#object.str
I think you want:
string_value = printHello.__str__()
if string_value == "some string":
do_whatever()
The __str__() method is used by print to make sense of class objects.
I have this Python 2 code:
class VKHandshakeChecker:
def __getAnswers(self):
return self.__answers
def __getStatus(self):
return self.__status
def __init__(self,vkapi,maxDepth=6):
isinstance(vkapi,VKApi.VKApi)
self.vkapi = vkapi
self.__maxDepth=maxDepth
self.__answers = list()
self.__status = 'IDLE'
self.status = property(VKHandshakeChecker.__getStatus)
self.answers = property(VKHandshakeChecker.__getAnswers)
I want to get answers property. But when I execute this code:
checker = VKHandshakeChecker.VKHandshakeChecker(api)
print(checker.status)
I get <property object at 0x02B55450>, not IDLE. Why?
You can't put descriptors (like a property object) on the instance. You have to use them on the class.
Simply use property as a decorator:
class VKHandshakeChecker:
#property
def answers(self):
return self.__answers
#property
def status(self):
return self.__status
def __init__(self,vkapi,maxDepth=6):
self.vkapi = vkapi
self.__maxDepth=maxDepth
self.__answers = list()
self.__status = 'IDLE'
I removed the isinstance() expression, it doesn't do anything as you are ignoring the return value of the function call.
Is there any way to avoid calling __init__ on a class while initializing it, such as from a class method?
I am trying to create a case and punctuation insensitive string class in Python used for efficient comparison purposes but am having trouble creating a new instance without calling __init__.
>>> class String:
def __init__(self, string):
self.__string = tuple(string.split())
self.__simple = tuple(self.__simple())
def __simple(self):
letter = lambda s: ''.join(filter(lambda s: 'a' <= s <= 'z', s))
return filter(bool, map(letter, map(str.lower, self.__string)))
def __eq__(self, other):
assert isinstance(other, String)
return self.__simple == other.__simple
def __getitem__(self, key):
assert isinstance(key, slice)
string = String()
string.__string = self.__string[key]
string.__simple = self.__simple[key]
return string
def __iter__(self):
return iter(self.__string)
>>> String('Hello, world!')[1:]
Traceback (most recent call last):
File "<pyshell#2>", line 1, in <module>
String('Hello, world!')[1:]
File "<pyshell#1>", line 17, in __getitem__
string = String()
TypeError: __init__() takes exactly 2 positional arguments (1 given)
>>>
What should I replace string = String(); string.__string = self.__string[key]; string.__simple = self.__simple[key] with to initialize the new object with the slices?
EDIT:
As inspired by the answer written below, the initializer has been edited to quickly check for no arguments.
def __init__(self, string=None):
if string is None:
self.__string = self.__simple = ()
else:
self.__string = tuple(string.split())
self.__simple = tuple(self.__simple())
When feasible, letting __init__ get called (and make the call innocuous by suitable arguments) is preferable. However, should that require too much of a contortion, you do have an alternative, as long as you avoid the disastrous choice of using old-style classes (there is no good reason to use old-style classes in new code, and several good reasons not to)...:
class String(object):
...
bare_s = String.__new__(String)
This idiom is generally used in classmethods which are meant to work as "alternative constructors", so you'll usually see it used in ways such as...:
#classmethod
def makeit(cls):
self = cls.__new__(cls)
# etc etc, then
return self
(this way the classmethod will properly be inherited and generate subclass instances when called on a subclass rather than on the base class).
A trick the standard pickle and copy modules use is to create an empty class, instantiate the object using that, and then assign that instance's __class__ to the "real" class. e.g.
>>> class MyClass(object):
... init = False
... def __init__(self):
... print 'init called!'
... self.init = True
... def hello(self):
... print 'hello world!'
...
>>> class Empty(object):
... pass
...
>>> a = MyClass()
init called!
>>> a.hello()
hello world!
>>> print a.init
True
>>> b = Empty()
>>> b.__class__ = MyClass
>>> b.hello()
hello world!
>>> print b.init
False
But note, this approach is very rarely necessary. Bypassing the __init__ can have some unexpected side effects, especially if you're not familiar with the original class, so make sure you know what you're doing.
Using a metaclass provides a nice solution in this example. The metaclass has limited use but works fine.
>>> class MetaInit(type):
def __call__(cls, *args, **kwargs):
if args or kwargs:
return super().__call__(*args, **kwargs)
return cls.__new__(cls)
>>> class String(metaclass=MetaInit):
def __init__(self, string):
self.__string = tuple(string.split())
self.__simple = tuple(self.__simple())
def __simple(self):
letter = lambda s: ''.join(filter(lambda s: 'a' <= s <= 'z', s))
return filter(bool, map(letter, map(str.lower, self.__string)))
def __eq__(self, other):
assert isinstance(other, String)
return self.__simple == other.__simple
def __getitem__(self, key):
assert isinstance(key, slice)
string = String()
string.__string = self.__string[key]
string.__simple = self.__simple[key]
return string
def __iter__(self):
return iter(self.__string)
>>> String('Hello, world!')[1:]
<__main__.String object at 0x02E78830>
>>> _._String__string, _._String__simple
(('world!',), ('world',))
>>>
Addendum:
After six years, my opinion favors Alex Martelli's answer more than my own approach. With meta-classes still on the mind, the following answer shows how the problem can be solved both with and without them:
#! /usr/bin/env python3
METHOD = 'metaclass'
class NoInitMeta(type):
def new(cls):
return cls.__new__(cls)
class String(metaclass=NoInitMeta if METHOD == 'metaclass' else type):
def __init__(self, value):
self.__value = tuple(value.split())
self.__alpha = tuple(filter(None, (
''.join(c for c in word.casefold() if 'a' <= c <= 'z') for word in
self.__value)))
def __str__(self):
return ' '.join(self.__value)
def __eq__(self, other):
if not isinstance(other, type(self)):
return NotImplemented
return self.__alpha == other.__alpha
if METHOD == 'metaclass':
def __getitem__(self, key):
if not isinstance(key, slice):
raise NotImplementedError
instance = type(self).new()
instance.__value = self.__value[key]
instance.__alpha = self.__alpha[key]
return instance
elif METHOD == 'classmethod':
def __getitem__(self, key):
if not isinstance(key, slice):
raise NotImplementedError
instance = self.new()
instance.__value = self.__value[key]
instance.__alpha = self.__alpha[key]
return instance
#classmethod
def new(cls):
return cls.__new__(cls)
elif METHOD == 'inline':
def __getitem__(self, key):
if not isinstance(key, slice):
raise NotImplementedError
cls = type(self)
instance = cls.__new__(cls)
instance.__value = self.__value[key]
instance.__alpha = self.__alpha[key]
return instance
else:
raise ValueError('METHOD did not have an appropriate value')
def __iter__(self):
return iter(self.__value)
def main():
x = String('Hello, world!')
y = x[1:]
print(y)
if __name__ == '__main__':
main()
Pass another argument to the constructor, like so:
def __init__(self, string, simple = None):
if simple is None:
self.__string = tuple(string.split())
self.__simple = tuple(self.__simple())
else:
self.__string = string
self.__simple = simple
You can then call it like this:
def __getitem__(self, key):
assert isinstance(key, slice)
return String(self.__string[key], self.__simple[key])
Also, I'm not sure it's allowed to name both the field and the method __simple. If only for readability, you should change that.