File tree Expand file tree Collapse file tree 3 files changed +35
-9
lines changed Expand file tree Collapse file tree 3 files changed +35
-9
lines changed Original file line number Diff line number Diff line change @@ -32,14 +32,19 @@ Enjoy!
32
32
...: await asyncio.sleep(2)
33
33
...:
34
34
35
- In [5]: %%asyncio
35
+ In [5]: %%async_
36
36
...: await foo()
37
37
...:
38
38
time = 1478985421.307329
39
39
time = 1478985423.309606
40
40
time = 1478985425.31514
41
41
42
- In [6]:
42
+ In [6]: %await_ foo()
43
+ time = 1487097377.700184
44
+ time = 1487097379.705614
45
+ time = 1487097381.707186
46
+
47
+ In [7]:
43
48
44
49
## Testing
45
50
Original file line number Diff line number Diff line change 1
1
# -*- coding: utf-8 -*-
2
2
3
3
import ast
4
- from ast import Call , Attribute , Name , Load
4
+ from ast import (
5
+ Call ,
6
+ Attribute ,
7
+ Name ,
8
+ Load ,
9
+ )
5
10
import asyncio as _asyncio
6
11
7
- from IPython .core .magic import Magics , magics_class , cell_magic
12
+ from IPython .core .magic import (
13
+ Magics ,
14
+ magics_class ,
15
+ cell_magic ,
16
+ line_magic ,
17
+ )
8
18
from IPython .utils .text import indent
9
19
10
20
@@ -44,13 +54,23 @@ def visit_Return(self, node):
44
54
45
55
@magics_class
46
56
class AsyncIOMagics (Magics ):
57
+ @line_magic
58
+ def await_ (self , line ):
59
+ expr = 'async def __f(): await {line}' .format (line = indent (line ))
60
+
61
+ self ._exec (expr )
62
+
47
63
@cell_magic
48
- def asyncio (self , line , cell ):
49
- coro_wrapper = 'async def __f():\n {cell}' .format (cell = indent (cell ))
50
- coro_wrapper = ast .parse (coro_wrapper )
51
- coro_wrapper = coro_wrapper .body [0 ].body
64
+ def async_ (self , line , cell ):
65
+ expr = 'async def __f():\n {cell}' .format (cell = indent (cell ))
66
+
67
+ self ._exec (expr )
68
+
69
+ def _exec (self , expr ):
70
+ expr = ast .parse (expr )
71
+ expr = expr .body [0 ].body
52
72
53
- nodes = [RewriteAwait ().visit (node ) for node in coro_wrapper ]
73
+ nodes = [RewriteAwait ().visit (node ) for node in expr ]
54
74
module = ast .Module (nodes )
55
75
ast .fix_missing_locations (module )
56
76
Original file line number Diff line number Diff line change 26
26
'Programming Language :: Python' ,
27
27
'Programming Language :: Python :: 3' ,
28
28
'Programming Language :: Python :: 3.5' ,
29
+ 'Programming Language :: Python :: 3.6' ,
29
30
'Topic :: Software Development :: Libraries :: Python Modules' ,
30
31
]
31
32
)
You can’t perform that action at this time.
0 commit comments