Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/mhconradt/ast-magic
IPython extension that allows viewing the AST of IPython cells using %magic
https://github.com/mhconradt/ast-magic
ipython python
Last synced: 13 days ago
JSON representation
IPython extension that allows viewing the AST of IPython cells using %magic
- Host: GitHub
- URL: https://github.com/mhconradt/ast-magic
- Owner: mhconradt
- Created: 2021-12-03T18:28:54.000Z (about 3 years ago)
- Default Branch: master
- Last Pushed: 2021-12-03T18:48:35.000Z (about 3 years ago)
- Last Synced: 2025-01-14T23:46:33.159Z (18 days ago)
- Topics: ipython, python
- Language: Python
- Homepage:
- Size: 3.91 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
## Installation
pip3 install ast-magic
To manually load, run the following in your IPython prompt:
%load_ext ast_magicTo automatically load, add the following to your [IPython configuration file](https://ipython.org/ipython-doc/3/config/intro.html):
c = get_config()
c.InteractiveShellApp.extensions.append('ast_magic')
## UsageInvestigating operator precedence:
In [39]: %ast print(True or False and True)
Module(
body=[
Expr(
value=Call(
func=Name(id='print', ctx=Load()),
args=[
BoolOp(
op=Or(),
values=[
Constant(value=True),
BoolOp(
op=And(),
values=[
Constant(value=False),
Constant(value=True)])])],
keywords=[]))],
type_ignores=[])
True
You can use it in a cell too:In [1]: %%ast
...:
...: def fibonacci(n: int) -> int:
...: if n <= 1: return 1
...: return fibonacci(n - 2) + fibonacci(n - 1)
...:
Module(
body=[
FunctionDef(
name='fibonacci',
args=arguments(
posonlyargs=[],
args=[
arg(
arg='n',
annotation=Name(id='int', ctx=Load()))],
kwonlyargs=[],
kw_defaults=[],
defaults=[]),
body=[
If(
test=Compare(
left=Name(id='n', ctx=Load()),
ops=[
LtE()],
comparators=[
Constant(value=1)]),
body=[
Return(
value=Constant(value=1))],
orelse=[]),
Return(
value=BinOp(
left=Call(
func=Name(id='fibonacci', ctx=Load()),
args=[
BinOp(
left=Name(id='n', ctx=Load()),
op=Sub(),
right=Constant(value=2))],
keywords=[]),
op=Add(),
right=Call(
func=Name(id='fibonacci', ctx=Load()),
args=[
BinOp(
left=Name(id='n', ctx=Load()),
op=Sub(),
right=Constant(value=1))],
keywords=[])))],
decorator_list=[],
returns=Name(id='int', ctx=Load()))],
type_ignores=[])