Skip to content

Instantly share code, notes, and snippets.

@pohmelie
Created April 27, 2019 10:51
Show Gist options
  • Save pohmelie/f14aea0ddec4493f0ce2da7f3ddc28ac to your computer and use it in GitHub Desktop.
Save pohmelie/f14aea0ddec4493f0ce2da7f3ddc28ac to your computer and use it in GitHub Desktop.
pysnooper gen coro example
Starting var:.. x = 1
Starting var:.. y = 2
13:50:40.798043 call 29 def fib(n):
13:50:40.798187 line 11 z = x + y
New var:....... z = 3
13:50:40.798215 line 12 a = x - y
New var:....... a = -1
13:50:40.798250 line 13 b = a + z
New var:....... b = 2
13:50:40.798271 line 14 result = await the_42()
13:50:40.798287 call 4 async def the_42():
13:50:40.798304 line 5 await asyncio.sleep(0.5)
13:50:40.798389 return 5 await asyncio.sleep(0.5)
Return value:.. <Future pending>
13:50:40.798437 return 14 result = await the_42()
Return value:.. <Future pending>
13:50:41.300428 call 14 result = await the_42()
13:50:41.300597 call 5 await asyncio.sleep(0.5)
13:50:41.300832 exception 5 await asyncio.sleep(0.5)
13:50:41.300918 line 6 return 42
13:50:41.300983 return 6 return 42
Return value:.. 42
13:50:41.301095 exception 14 result = await the_42()
New var:....... result = 42
13:50:41.301229 line 15 return b // 2
13:50:41.301318 return 15 return b // 2
Return value:.. 1
Starting var:.. n = 5
13:50:41.808357 call 38 def foo(n):
13:50:41.808499 line 39 r = yield from(fib(n))
Starting var:.. n = 5
13:50:41.808664 call 29 def fib(n):
13:50:41.808746 line 30 a, b = 0, 1
New var:....... a = 0
New var:....... b = 1
13:50:41.808885 line 31 while n:
13:50:41.808959 line 32 yield a
13:50:41.809059 return 32 yield a
Return value:.. 0
13:50:41.809182 return 39 r = yield from(fib(n))
Return value:.. 0
0
13:50:41.809351 call 39 r = yield from(fib(n))
13:50:41.809438 call 32 yield a
13:50:41.809516 line 33 a, b, n = b, a + b, n - 1
Modified var:.. a = 1
Modified var:.. n = 4
13:50:41.809643 line 31 while n:
13:50:41.809717 line 32 yield a
13:50:41.809789 return 32 yield a
Return value:.. 1
13:50:41.809881 return 39 r = yield from(fib(n))
Return value:.. 1
1
13:50:41.810056 call 39 r = yield from(fib(n))
13:50:41.810142 call 32 yield a
13:50:41.810219 line 33 a, b, n = b, a + b, n - 1
Modified var:.. b = 2
Modified var:.. n = 3
13:50:41.810350 line 31 while n:
13:50:41.810422 line 32 yield a
13:50:41.810495 return 32 yield a
Return value:.. 1
13:50:41.810585 return 39 r = yield from(fib(n))
Return value:.. 1
1
13:50:41.810728 call 39 r = yield from(fib(n))
13:50:41.810806 call 32 yield a
13:50:41.810879 line 33 a, b, n = b, a + b, n - 1
Modified var:.. a = 2
Modified var:.. b = 3
Modified var:.. n = 2
13:50:41.811022 line 31 while n:
13:50:41.811091 line 32 yield a
13:50:41.811162 return 32 yield a
Return value:.. 2
13:50:41.811248 return 39 r = yield from(fib(n))
Return value:.. 2
2
13:50:41.811389 call 39 r = yield from(fib(n))
13:50:41.811465 call 32 yield a
13:50:41.811538 line 33 a, b, n = b, a + b, n - 1
Modified var:.. a = 3
Modified var:.. b = 5
Modified var:.. n = 1
13:50:41.811680 line 31 while n:
13:50:41.811749 line 32 yield a
13:50:41.811819 return 32 yield a
Return value:.. 3
13:50:41.811904 return 39 r = yield from(fib(n))
Return value:.. 3
3
13:50:41.812042 call 39 r = yield from(fib(n))
13:50:41.812118 call 32 yield a
13:50:41.812191 line 33 a, b, n = b, a + b, n - 1
Modified var:.. a = 5
Modified var:.. b = 8
Modified var:.. n = 0
13:50:41.812336 line 31 while n:
13:50:41.812405 line 34 return "BOO"
13:50:41.812478 return 34 return "BOO"
Return value:.. 'BOO'
13:50:41.812571 exception 39 r = yield from(fib(n))
New var:....... r = 'BOO'
13:50:41.812667 line 40 print("->", r)
-> BOO
13:50:41.812748 return 40 print("->", r)
Return value:.. None
from pysnooper import snoop
async def the_42():
await asyncio.sleep(0.5)
return 42
@snoop(depth=2)
async def foo(x, y):
z = x + y
a = x - y
b = a + z
result = await the_42()
return b // 2
async def bar():
for i in range(10):
x = i
await asyncio.sleep(0.1)
import asyncio
loop = asyncio.get_event_loop()
loop.run_until_complete(asyncio.gather(foo(1, 2), bar()))
def fib(n):
a, b = 0, 1
while n:
yield a
a, b, n = b, a + b, n - 1
return "BOO"
@snoop(depth=2)
def foo(n):
r = yield from(fib(n))
print("->", r)
for f in foo(5):
print(f)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment