Skip to content

Commit 5267e91

Browse files
committed
added %await_ line magic; renamed %%asycnio cell magic to %%async_
1 parent bb32de5 commit 5267e91

File tree

3 files changed

+35
-9
lines changed

3 files changed

+35
-9
lines changed

README.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,19 @@ Enjoy!
3232
...: await asyncio.sleep(2)
3333
...:
3434

35-
In [5]: %%asyncio
35+
In [5]: %%async_
3636
...: await foo()
3737
...:
3838
time = 1478985421.307329
3939
time = 1478985423.309606
4040
time = 1478985425.31514
4141

42-
In [6]:
42+
In [6]: %await_ foo()
43+
time = 1487097377.700184
44+
time = 1487097379.705614
45+
time = 1487097381.707186
46+
47+
In [7]:
4348

4449
## Testing
4550

asynciomagic.py

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,20 @@
11
# -*- coding: utf-8 -*-
22

33
import ast
4-
from ast import Call, Attribute, Name, Load
4+
from ast import (
5+
Call,
6+
Attribute,
7+
Name,
8+
Load,
9+
)
510
import asyncio as _asyncio
611

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+
)
818
from IPython.utils.text import indent
919

1020

@@ -44,13 +54,23 @@ def visit_Return(self, node):
4454

4555
@magics_class
4656
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+
4763
@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
5272

53-
nodes = [RewriteAwait().visit(node) for node in coro_wrapper]
73+
nodes = [RewriteAwait().visit(node) for node in expr]
5474
module = ast.Module(nodes)
5575
ast.fix_missing_locations(module)
5676

setup.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
'Programming Language :: Python',
2727
'Programming Language :: Python :: 3',
2828
'Programming Language :: Python :: 3.5',
29+
'Programming Language :: Python :: 3.6',
2930
'Topic :: Software Development :: Libraries :: Python Modules',
3031
]
3132
)

0 commit comments

Comments
 (0)