Skip to main content

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/)

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

opyum-0.2.tar.gz (20.5 kB view hashes)

Uploaded Source

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