Created
April 19, 2013 03:08
-
-
Save fgmacedo/5417833 to your computer and use it in GitHub Desktop.
Teste de funcionamento de "coroutines", num estilo simplista do que acontece com o @gen.coroutine do Tornado.
This file contains hidden or 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 | |
def co_operador(f): | |
def wrapper(): | |
gen = f() | |
res = gen.send(None) | |
while 1: | |
try: | |
calculado = res[0](res[1:]) | |
res = gen.send(calculado) | |
except StopIteration: | |
break | |
return calculado | |
return wrapper | |
@co_operador | |
def decorada(): | |
res = yield (sum, 2, 3) | |
assert res == 5, 'res deve ser 5' | |
res = yield (min, 35, 30, 12, 15) | |
assert res == 12, 'res deve ser 12' | |
class TesteDeCorrotinas(unittest.TestCase): | |
def teste_decorador_futuro(self): | |
res = decorada() | |
self.assertEqual(res, 12) | |
if __name__ == '__main__': | |
unittest.main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment