No project description provided
Project description
Fun with ASTs
Provides developers with a programmatic tool to change our own code. This repository contains a library to analyze and manipulate python code using Abstract Systax Tress manipulation.
Using the library
See the test-fun-with-ast project for examples of using the fun-with-ast library.
Why Fun with ASTs
- The intellectual problem:
- it is fun
- it is a great learning experience
- enables smart and complex manipulations
How it works
AST Parse and Unparse Examples
example #1: losing comments
import ast
code = """
a=7 # A is 7
"""
print(ast.unparse(ast.parse(code)))
Output:
a = 7
example #2: Losing parentheses
import ast
code ="""
if (a<7) or (b>9):
pass
"""
print(ast.unparse(ast.parse(code)))
Output:
if a < 7 or b > 9:
pass
Example 3: Losing elif
, losing indentation
import ast
code = """
if True:
a=2
else:
if d==8:
c=7
"""
print(ast.unparse(ast.parse(code)))
output:
if True:
a = 2
elif d == 8:
c = 7
import ast
print(ast.unparse(ast.parse("""if True:\n a=2\nelse:\n if d==8:\n c=7\n else:\n c =8""")))
if True:
a = 2
elif d == 8:
c = 7
else:
c = 8
example #1
import ast
code = """
if True:
a=2
else:
if d==8:
c = 7
elif d==9:
c=8"""
print(ast.unparse(ast.parse(code)))
``python if True: a = 2 elif d == 8: c = 7 elif d == 9: c = 8
## examples
### Fun #1: Keep source to source transformations
### Fun #2: add log
### Fun #3: switch else / if
### Fun #4: mutation testing switch `<` into `<=`
### Fun #5: for to while
### Fun #6: for loop into tail recursion
### Fun #7: Add node AND comment
### Fun #8: Sort methods public to private
# Get started
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
fun_with_ast-0.1.68.tar.gz
(80.9 kB
view hashes)
Built Distribution
Close
Hashes for fun_with_ast-0.1.68-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6a797809450fc28d4363f5994453b1162719f93c93f72c2bc4e10cf907e5aa01 |
|
MD5 | 20894b8eb3a2ded318a1ebdc6dda54f8 |
|
BLAKE2b-256 | e72f4e90dd4dc1cb1e14cb21ee8f7261da5395d4dac5837f3b2d53d8d5ff57ae |