Skip to content

Instantly share code, notes, and snippets.

@zerko
Created October 25, 2010 13:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zerko/644971 to your computer and use it in GitHub Desktop.
Save zerko/644971 to your computer and use it in GitHub Desktop.
sample for habr.ru
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