Skip to main content

Full Syntax Tree for python to make writing refactoring code a realist task

Project description

Introduction

Baron is a Full Syntax Tree (FST) library for Python. By opposition to an AST which drops some syntax information in the process of its creation (like empty lines, comments, formatting), a FST keeps everything and guarantees the operation fst_to_code(code_to_fst(source_code)) == source_code.

Installation

pip install baron

Basic Usage

from baron import parse, dumps

fst = parse(source_code_string)
source_code_string == dumps(fst)

There is a good chance that you’ll want to use RedBaron instead of using Baron directly. Think of Baron as the “bytecode of python source code” and RedBaron as some sort of usable layer on top of it.

If you don’t know what Baron is or don’t understand yet why it might be useful for you, read the « Why is this important? » section.

Documentation

Baron documentation is available on Read The Docs.

Why is this important?

The usage of a FST might not be obvious at first sight so let’s consider a series of problems to illustrate it. Let’s say that you want to write a program that will:

  • rename a variable in a source file… without clashing with things that are not a variable (example: stuff inside a string)

  • inline a function/method

  • extract a function/method from a series of line of code

  • split a class into several classes

  • split a file into several modules

  • convert your whole code base from one ORM to another

  • do custom refactoring operation not implemented by IDE/rope

  • implement the class browser of smalltalk for python (the whole one where you can edit the code of the methods, not just showing code)

It is very likely that you will end up with the awkward feeling of writing clumpsy weak code that is very likely to break because you didn’t thought about all the annoying special cases and the formatting keeps bothering you. You may end up playing with ast.py until you realize that it removes too much information to be suitable for those situations. You will probably ditch this task as simple too complicated and really not worth the effort. You are missing a good abstraction that will take care of all of the code structure and formatting for you so you can concentrate on your task.

The FST tries to be this abstraction. With it you can now work on a tree which represents your code with its formatting. Moreover, since it is the exact representation of your code, modifying it and converting it back to a string will give you back your code only modified where you have modified the tree.

Said in another way, what I’m trying to achieve with Baron is a paradigm change in which writing code that will modify code is now a realist task that is worth the price (I’m not saying a simple task, but a realistic one: it’s still a complex task).

Other

Having a FST (or at least a good abstraction build on it) also makes it easier to do code generation and code analysis while those two operations are already quite feasible (using ast.py and a templating engine for example).

Some technical details

Baron produces a FST in the form of JSON (and by JSON I mean Python lists and dicts that can be dumped into JSON) for maximum interoperability.

Baron FST is quite similar to Python AST with some modifications to be more intuitive to humans, since Python AST has been made for CPython interpreter.

Since playing directly with JSON is a bit raw I’m going to build an abstraction on top of it that will looks like BeautifulSoup/jQuery.

State of the project

Currently, Baron has been tested on the top 100 projects and the FST converts back exactly into the original source code. So, it can be considered quite stable, but it is far away from having been battle tested.

Since the project is very young and no one is already using it except my project, I’m open to changes of the FST nodes but I will quickly become conservative once it gets some adoption and will probably accept to modify it only once or twice in the future with clear indications on how to migrate.

Baron is targeting python 2.[67]. It has not been tested on python3 but should be working for most parts (except the new grammar like yield from, obviously).

Tests

Run either py.test tests/ or nosetests in the baron directory.

Community

You can reach us on irc.freenode.net#baron.

Misc

Old blog post announcing the project. Not that much up to date.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

baron-0.2.tar.gz (31.1 kB view details)

Uploaded Source

Built Distributions

baron-0.2-py2.py3-none-any.whl (37.8 kB view details)

Uploaded Python 2 Python 3

baron-0.2-py2.7.egg (33.7 kB view details)

Uploaded Source

File details

Details for the file baron-0.2.tar.gz.

File metadata

  • Download URL: baron-0.2.tar.gz
  • Upload date:
  • Size: 31.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for baron-0.2.tar.gz
Algorithm Hash digest
SHA256 623418872ea56f76f4a5c24e4160db460d56c5aefd80479ad1b8411fa140e371
MD5 d92e5b117b214425f64b17d1562a5061
BLAKE2b-256 74379da9165c80f8a423ac1e1f4ed40f93847e7bf637799b8d4f5579dbbbe1f6

See more details on using hashes here.

File details

Details for the file baron-0.2-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for baron-0.2-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 0aae0ccc7326e49d223d5c440c6b8003102d087d3bf421653c29faded7502aab
MD5 276a03ab8c1a2985621b3a5e18752ca4
BLAKE2b-256 8942f0dcab7b68ad3d594716d4337fa2d7787c95afe2b2a72addd0520d7074d0

See more details on using hashes here.

File details

Details for the file baron-0.2-py2.7.egg.

File metadata

  • Download URL: baron-0.2-py2.7.egg
  • Upload date:
  • Size: 33.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for baron-0.2-py2.7.egg
Algorithm Hash digest
SHA256 0c3135951b85244f0905bc1eaaddffad9bb21df01ab918ac39cab95a6479c4bb
MD5 ee8670ab294a5c481eea55f098859e93
BLAKE2b-256 90961c66946be188cb4865a5e5216f585c431bd370bf94dddd4d5a19e66fe9e6

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page