Skip to main content

This allows you to visualize the AST of a cell in IPython upon running it.

Installation

pip3 install ast-magic

To manually load, run the following in your IPython prompt:

%load_ext ast_magic

To automatically load, add the following to your IPython configuration file:

c = get_config()
c.InteractiveShellApp.extensions.append('ast_magic')

Usage

Verifying Python follows PEMDAS:

In [1]: %ast (1 + 1) ** 2 * 5 - 4
Module(
    body=[
        Expr(
            value=BinOp(
                left=BinOp(
                    left=BinOp(
                        left=BinOp(
                            left=Constant(value=1),
                            op=Add(),
                            right=Constant(value=1)),
                        op=Pow(),
                        right=Constant(value=2)),
                    op=Mult(),
                    right=Constant(value=5)),
                op=Sub(),
                right=Constant(value=4)))],
    type_ignores=[])

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=[])

Release files for ast-magic 1.0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for ast-magic 1.0.1
File Size Uploaded
ast-magic-1.0.1.tar.gz 2.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for ast-magic 1.0.1
File Interpreter ABI Platform
ast_magic-1.0.1-py3-none-any.whl Python 3 none any Details

Total release size: 4.8 kB

Release files / ast-magic-1.0.1.tar.gz

Download URL ast-magic-1.0.1.tar.gz
Size 2.4 kB
Tags Source
SHA-256 checksum
How to use checksums
19bd0a1dd5a211873435af12b7cdd1124400f70ceb8ad2727ddb51eafce5a222
BLAKE2b-256 checksum
How to use checksums
48c91bfa215c7bc6d5b34159a7f8989e670e68c367c3d15544e97175dc57dc1c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.1 pkginfo/1.5.0.1 requests/2.25.1 setuptools/46.1.3 requests-toolbelt/0.9.1 tqdm/4.45.0 CPython/3.8.0

Release files / ast_magic-1.0.1-py3-none-any.whl

Download URL ast_magic-1.0.1-py3-none-any.whl
Size 2.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
810c40883c9cff971ea865a35ca54d0a9cd71282de256137bd51d4a286a5cd62
BLAKE2b-256 checksum
How to use checksums
f62c2b35c097acb76d9e6077b70a1d0996434178f8d1e6c2b60649e5a11a502b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.1 pkginfo/1.5.0.1 requests/2.25.1 setuptools/46.1.3 requests-toolbelt/0.9.1 tqdm/4.45.0 CPython/3.8.0

Release history Release notifications | RSS feed

This release

1.0.1 This release

2 release files

1.0.0

2 release files

0.0.4

2 release files

0.0.3

2 release files

0.0.2

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page