python ast / Николай Карелин / vpi development center [python meetup 27.03.15]

26
PYTHON AST MODULE Между исходным текстом и байткодом Николай Карелин

Upload: python-meetup

Post on 15-Jul-2015

257 views

Category:

Software


3 download

TRANSCRIPT

Page 1: Python AST / Николай Карелин / VPI Development Center [Python Meetup 27.03.15]

PYTHON AST

MODULE

Между исходным текстом и байткодом

Николай Карелин

Page 2: Python AST / Николай Карелин / VPI Development Center [Python Meetup 27.03.15]

Что это?

• Abstract Syntax Tree:

• Представление абстрактной структуры кода языка

программирования в виде дерева объектов

• В отличие от дерева разбора (parse tree) остаются только

элементы синтаксиса языка

Page 3: Python AST / Николай Карелин / VPI Development Center [Python Meetup 27.03.15]

Компиляция кода

• Python 2.5 и позже

• By Armin Ronacher

.py

Parse

Tree

CFG*

bytecode

AST

*CFG = Control Flow Graph

Page 4: Python AST / Николай Карелин / VPI Development Center [Python Meetup 27.03.15]

Пример 1

Page 5: Python AST / Николай Карелин / VPI Development Center [Python Meetup 27.03.15]

Module

FunctionDef

Name

Expr Pass

body

arguments

body

Module(

body=[

FunctionDef(name='func', args=arguments(

args=[Name(id='x', ctx=Param())],

vararg=None, kwarg=None,

defaults=[]),

body=[Expr(value=Str(s='My function')),

Pass()],

decorator_list=[])])

Str

valueParam

ctx

Page 6: Python AST / Николай Карелин / VPI Development Center [Python Meetup 27.03.15]

Пример 2

https://github.com/titusjan/astviewer

Attributes

Fields

Page 7: Python AST / Николай Карелин / VPI Development Center [Python Meetup 27.03.15]

Структура модуля• from _ast import *

• Часть С-кода

• Определение всех объектов

• Helper functions

• Получение

• Обработка

• Манипуляции

• Visitor/Transformer class prototypes

Page 8: Python AST / Николай Карелин / VPI Development Center [Python Meetup 27.03.15]

Иерархия объектовAST

comprehension

boolop cmpop

arguments excepthandler

alias expr_context

exprmod

operator

keyword

unaryopstmt slice

103 classes in total

Page 9: Python AST / Николай Карелин / VPI Development Center [Python Meetup 27.03.15]

Иерархия объектов

• Attribute, BinOp, BoolOp,

• Call, Compare, Dict,

• DictComp, GeneratorExp,

• IfExp, Lambda, List,

• ListComp, Name, Num,

• Repr, Set, SetComp,

• Str, Subscript, Tuple,

• UnaryOp, Yield

• Assert, Assign,

• AugAssign, Break,

• ClassDef, Continue, Delete,

• Exec, Expr, For,

• FunctionDef, Global,

• If, Import, ImportFrom,

• Pass, Print, Raise,

• Return, TryExcept,

• TryFinally, While, With

expr stmt

Page 10: Python AST / Николай Карелин / VPI Development Center [Python Meetup 27.03.15]

Синтаксис Python

Page 11: Python AST / Николай Карелин / VPI Development Center [Python Meetup 27.03.15]

Синтаксис Python

ASDL (Zephyr Abstract Syntax Definition Language) format

Использует Python скрипт для генерации C-кода ;)

Опционально0 или более

Page 12: Python AST / Николай Карелин / VPI Development Center [Python Meetup 27.03.15]

Примеры

• blaze, cffi, enaml, Ipython

• numpy (SafeEval), pandas, scipy.weave, sympy

• pep8, pip, pycparser pylint pyflakes rope

Page 13: Python AST / Николай Карелин / VPI Development Center [Python Meetup 27.03.15]

Применение: компиляция

• Numba, http://numba.pydata.org/

• HOPE, http://hope.phys.ethz.ch

• PyJS (Pyjamas), http://pyjs.org,

https://github.com/pyjs/pyjs

.py

Parse

Tree

CFG

bytecode

AST

LLVM

Page 14: Python AST / Николай Карелин / VPI Development Center [Python Meetup 27.03.15]

Visitor

GreenTreeSnakes

Page 15: Python AST / Николай Карелин / VPI Development Center [Python Meetup 27.03.15]

Свой язык в Python

• Hy ;)

.py

Parse

Tree

CFG

bytecode

AST

Page 16: Python AST / Николай Карелин / VPI Development Center [Python Meetup 27.03.15]

Создание AST

Page 17: Python AST / Николай Карелин / VPI Development Center [Python Meetup 27.03.15]

Hy: HyASTCompiler.compile()

Page 18: Python AST / Николай Карелин / VPI Development Center [Python Meetup 27.03.15]

Сode instrumenting

• Pyflakes, …

• Macros (http://www.pocoo.org/projects/karnickel/)

.py

Parse

Tree

CFG

bytecode

AST

?

Page 19: Python AST / Николай Карелин / VPI Development Center [Python Meetup 27.03.15]

numpy.utils.safe_eval()

См. также ast.literal_eval()

Page 20: Python AST / Николай Карелин / VPI Development Center [Python Meetup 27.03.15]

Осторожно!

There Might be Dragons!

Page 21: Python AST / Николай Карелин / VPI Development Center [Python Meetup 27.03.15]

AST - Предосторожности

• Странные ошибки (segfaults)

• Нет гарантии стабильности API

• Разная структура для

• 2.x <-> 3.x

• Разные версии ветки 3.x

• Cpython, PyPy, Jython, …

Page 22: Python AST / Николай Карелин / VPI Development Center [Python Meetup 27.03.15]

Что дальше

• Hacker’s Guide to Python

• PEP 339 / Python Dev. Guide

• Green Tree Snakes documentation,

https://greentreesnakes.readthedocs.org/en/latest/

• ast module documentation

• http://pyvideo.org/video/419/pycon-2011--what-would-you-

do-with-an-ast

• http://www.dalkescientific.com/writings/diary/archive/2010/

02/22/instrumenting_the_ast.html

Page 23: Python AST / Николай Карелин / VPI Development Center [Python Meetup 27.03.15]

Hacker’s Guide to Python

Table of contents

1. Starting your project

2. Modules and libraries

3. Documentation

4. Distribution

5. Virtual environments

6. Unit testing

7. Methods and decorators

8. Functional programming

9. The AST

10. Performances and optimizations

11. Scaling and architecture

12. RDBMS and ORM

13. Python 3 support strategies

14. Write less, code more

https://julien.danjou.info/books/the-hacker-guide-to-python

Page 24: Python AST / Николай Карелин / VPI Development Center [Python Meetup 27.03.15]

THE END

Page 25: Python AST / Николай Карелин / VPI Development Center [Python Meetup 27.03.15]

На будущее

• Sphinx и ReST для документирования проектов

• Python для численных расчетов (Numba, GPU, …)

• ctypesgen – обертки над внешними библиотеками

???

Page 26: Python AST / Николай Карелин / VPI Development Center [Python Meetup 27.03.15]

Ищется помощь по белорусификации

http://pymorphy2.readthedocs.org/en/latest/

https://github.com/kmike/pymorphy2