Optimizing Python applications without mutilation code
Project description
# opyum
[![version](https://img.shields.io/pypi/v/opyum.svg)](http://pypi.python.org/pypi/opyum)
[![downloads](https://img.shields.io/pypi/dw/opyum.svg)](http://pypi.python.org/pypi/opyum)
[![license](https://img.shields.io/pypi/l/opyum.svg)](http://pypi.python.org/pypi/opyum)
[![status](https://img.shields.io/pypi/status/opyum.svg)](http://pypi.python.org/pypi/opyum)
[![pyversions](https://img.shields.io/pypi/pyversions/opyum.svg)](http://pypi.python.org/pypi/opyum)
## Description
**Optimizing Python applications without mutilation code.**
Use the automatic modification of AST for code optimization, which is transparent to the user and requires the addition of only a few lines.
## Usage
**Decorator**:
```python
from opyum import optimize
@optimize
def function_for_optimize():
...
```
**Import-hook**:
```python
import opyum
opyum.activate()
# other imports
```
**"With" syntax:**
```python
import opyum
with opyum.activate:
# other imports
```
**Command-line mode:**
Show optimized source:
$ opyum show myfile.py
Diff between original source and optimized source:
$ opyum diff myfile.py
Console diff (with "-c" or "--console" option):
![console diff example](https://raw.githubusercontent.com/Amper/opyum/master/example/screen1.png)
Custom app diff (with "--app" option):
![app diff example](https://raw.githubusercontent.com/Amper/opyum/master/example/screen2.png)
By default, html diff (without options):
![app diff example](https://raw.githubusercontent.com/Amper/opyum/master/example/screen3.png)
## List of optimizations
####Constant folding
Before:
```python
x = 7 * 24 * 60 * 60
y = [i ** 2 for i in range(10) if i % 2 == 0]
z = sum(range(1000))
```
After:
```python
x = 604800
y = [0, 4, 16, 36, 64]
z = 499500
```
####"'Power' to 'multiplication'" optimization
Before:
```python
x1 = a ** (-2)
x2 = a ** (-1)
x3 = a ** ( 0)
x4 = a ** ( 1)
x5 = a ** ( 2)
```
After:
```python
x1 = 1 / (a * a)
x2 = 1 / a
x3 = 1
x4 = a
x5 = a * a
```
####"'Yield' to 'yield from'" optimization
Before:
```python
for x in some_expression:
yield x
```
After
```python
yield from some_expression
```
####Builtin constant propagation
Before:
```python
from math import pi
def circumference(r):
return 2 * pi * r
```
After:
```python
from math import pi
def circumference(r):
return 2 * 3.141592653589793 * r
```
####Custom constant propagation
Before:
```python
C_PI = 3.141592653589793
def circumference(r):
return 2 * C_PI * r
```
After:
```python
C_PI = 3.141592653589793
def circumference(r):
return 2 * 3.141592653589793 * r
```
####Dead code elimination
Before:
```python
def do_something():
return 1
print('returning 1')
if condition1:
pass
elif condition2:
do_something()
else:
pass
```
After:
```python
def do_something():
return 1
if not condition1 and condition2:
do_something()
```
## Installation
Installation is simple with pip:
$ pip install opyum
or with setuptools:
$ easy_install opyum
## Documentation
[opyum.readthedocs.org](http://opyum.readthedocs.org/)
[opyum.rtfd.org](http://opyum.rtfd.org/)
[![version](https://img.shields.io/pypi/v/opyum.svg)](http://pypi.python.org/pypi/opyum)
[![downloads](https://img.shields.io/pypi/dw/opyum.svg)](http://pypi.python.org/pypi/opyum)
[![license](https://img.shields.io/pypi/l/opyum.svg)](http://pypi.python.org/pypi/opyum)
[![status](https://img.shields.io/pypi/status/opyum.svg)](http://pypi.python.org/pypi/opyum)
[![pyversions](https://img.shields.io/pypi/pyversions/opyum.svg)](http://pypi.python.org/pypi/opyum)
## Description
**Optimizing Python applications without mutilation code.**
Use the automatic modification of AST for code optimization, which is transparent to the user and requires the addition of only a few lines.
## Usage
**Decorator**:
```python
from opyum import optimize
@optimize
def function_for_optimize():
...
```
**Import-hook**:
```python
import opyum
opyum.activate()
# other imports
```
**"With" syntax:**
```python
import opyum
with opyum.activate:
# other imports
```
**Command-line mode:**
Show optimized source:
$ opyum show myfile.py
Diff between original source and optimized source:
$ opyum diff myfile.py
Console diff (with "-c" or "--console" option):
![console diff example](https://raw.githubusercontent.com/Amper/opyum/master/example/screen1.png)
Custom app diff (with "--app" option):
![app diff example](https://raw.githubusercontent.com/Amper/opyum/master/example/screen2.png)
By default, html diff (without options):
![app diff example](https://raw.githubusercontent.com/Amper/opyum/master/example/screen3.png)
## List of optimizations
####Constant folding
Before:
```python
x = 7 * 24 * 60 * 60
y = [i ** 2 for i in range(10) if i % 2 == 0]
z = sum(range(1000))
```
After:
```python
x = 604800
y = [0, 4, 16, 36, 64]
z = 499500
```
####"'Power' to 'multiplication'" optimization
Before:
```python
x1 = a ** (-2)
x2 = a ** (-1)
x3 = a ** ( 0)
x4 = a ** ( 1)
x5 = a ** ( 2)
```
After:
```python
x1 = 1 / (a * a)
x2 = 1 / a
x3 = 1
x4 = a
x5 = a * a
```
####"'Yield' to 'yield from'" optimization
Before:
```python
for x in some_expression:
yield x
```
After
```python
yield from some_expression
```
####Builtin constant propagation
Before:
```python
from math import pi
def circumference(r):
return 2 * pi * r
```
After:
```python
from math import pi
def circumference(r):
return 2 * 3.141592653589793 * r
```
####Custom constant propagation
Before:
```python
C_PI = 3.141592653589793
def circumference(r):
return 2 * C_PI * r
```
After:
```python
C_PI = 3.141592653589793
def circumference(r):
return 2 * 3.141592653589793 * r
```
####Dead code elimination
Before:
```python
def do_something():
return 1
print('returning 1')
if condition1:
pass
elif condition2:
do_something()
else:
pass
```
After:
```python
def do_something():
return 1
if not condition1 and condition2:
do_something()
```
## Installation
Installation is simple with pip:
$ pip install opyum
or with setuptools:
$ easy_install opyum
## Documentation
[opyum.readthedocs.org](http://opyum.readthedocs.org/)
[opyum.rtfd.org](http://opyum.rtfd.org/)
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
opyum-0.2.tar.gz
(20.5 kB
view details)
File details
Details for the file opyum-0.2.tar.gz
.
File metadata
- Download URL: opyum-0.2.tar.gz
- Upload date:
- Size: 20.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 706b3c38f68a6ff275e76f76c1fb7c2c25c132766c49d8216880b6261ed95a0d |
|
MD5 | 20886fb9e9c452852f3e6dba68e0f4d5 |
|
BLAKE2b-256 | 76a1e3efd5bb4d3cc7c0eaff309c62b8a5dc88de68b1fb5de37f305c02a04892 |