Created
October 25, 2010 13:46
-
-
Save zerko/644971 to your computer and use it in GitHub Desktop.
sample for habr.ru
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import unittest | |
import logging | |
logging.basicConfig(level = logging.DEBUG) | |
log = logging.getLogger("messaged.expression") | |
class MessagedExpression(object): | |
def __init__(self, val, msg): | |
log.debug("%s evaluated" % val) | |
self.msg = msg | |
self.val = val | |
def __nonzero__(self): | |
return self.__bool__() | |
def __bool__(self): | |
return bool(self.val) | |
_me = MessagedExpression | |
def calculate_me(): | |
log.debug("hardcore calculations") | |
class Test(unittest.TestCase): | |
def test(self): | |
result = _me("123","first") and _me(False, "second") and _me(calculate_me(),"calculated") | |
self.assertEqual(result.msg,"second") | |
result = _me("123","first") and _me(False, "second") or _me(calculate_me(),"calculated") | |
self.assertEqual(result.msg,"calculated") | |
if __name__=="__main__": | |
unittest.main() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment