Skip to main content

Reverse evaluate values to Python code using constraint solving

Project description

PyPI version

Python code synthesis

This project is used to interpret Python code and generate Python code based on given values.

This project is heavily influenced by Barliman, which implemented this exact idea for Scheme. While Barliman works wonders, I don't think Scheme is a programming language that is widely used in production environments. This is the reason I created this project, so that the principle of Barliman could be applied to Python.

Examples

This project can do both interpreting from Python code to values and vica versa. For example, AST values will be converted to the correct interpreted values:

> from evalo import *
> x = var()
> run(1, x, eval_expro(ast.BinOp(left=ast.Num(n=1), op=ast.Add(), right=ast.Num(n=1)), [], x))

(2,)

Interpreting from values to Python code will generate AST objects, which can be directly translated to Python code using external libraries. For example, if we want 5 different versions of Python code that are interpreted to the value 2, we can do the following:

> from evalo import *
> x = var()
> run(5, x, eval_expro(x, [], 2))

(<_ast.Num at 0x7fdf44aabe48>,
 <_ast.BinOp at 0x7fdf44a87fd0>,
 <_ast.BinOp at 0x7fdf44a1d320>,
 <_ast.BinOp at 0x7fdf44a444e0>,
 <_ast.BinOp at 0x7fdf46ee36a0>)

To translate this to human-readable values, we can use ast_dump_if_possible:

> [ast_dump_if_possible(y) for y in run(5, x, eval_expro(x, [], 2, maxdepth=3))]

['Num(n=2)',
 'BinOp(left=Num(n=0), op=Add(), right=Num(n=2))',
 'BinOp(left=Num(n=0), op=Sub(), right=BinOp(left=Num(n=0), op=Sub(), right=Num(n=2)))',
 'BinOp(left=Num(n=1), op=Mult(), right=Num(n=2))',
 'BinOp(left=BinOp(left=Num(n=0), op=Add(), right=Num(n=0)), op=Add(), right=Num(n=2))']

Using the astunparse library we can directly translate this output to Python source code:

> [astunparse.unparse(y).strip() for y in run(10, x, eval_expro(x, [], 2, maxdepth=3))]

['2',
 '(0 + 2)',
 '(0 - (0 - 2))',
 '(1 * 2)',
 '((0 + 0) + 2)',
 '((0 + 0) - (0 - 2))',
 '((1 + 0) * 2)',
 '((0 - 0) + 2)',
 '((0 - 0) - (0 - 2))',
 '((1 - 0) * 2)']

Development

If you want to help develop on this project, you can install it like so using Python 3.7:

mkvirtualenv -a $(pwd) $(basename $(pwd)) -p python3
pip install -r requirements/development.txt
pre-commit install

This project uses tox with pytest for testing, so just run tox to run all tests.

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

evalo-0.2.0.tar.gz (6.4 kB view details)

Uploaded Source

Built Distribution

evalo-0.2.0-py3-none-any.whl (8.4 kB view details)

Uploaded Python 3

File details

Details for the file evalo-0.2.0.tar.gz.

File metadata

  • Download URL: evalo-0.2.0.tar.gz
  • Upload date:
  • Size: 6.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/51.3.3 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.7.9

File hashes

Hashes for evalo-0.2.0.tar.gz
Algorithm Hash digest
SHA256 df0fd4746dc3b33963c0f59f6ab8537049d1ccbec35dc8a98a9d61065a9c9ee7
MD5 ceb75ded444214d890cc8b3d1141c9c5
BLAKE2b-256 1fe892d9225f751602221b052dbf68ee762b27bc95598682e29545aeb8b9de96

See more details on using hashes here.

File details

Details for the file evalo-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: evalo-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 8.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/51.3.3 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.7.9

File hashes

Hashes for evalo-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 75cb0733aa04c01a419ce63c501efcb90c5a9c34bec4ea389bfce54f51a07ce0
MD5 2e4a07769101dd51bb62e262a79d1dbd
BLAKE2b-256 68d2517528c4107dfa9f88211f559a9e82ac4b96aff06ee740775b4597cdae55

See more details on using hashes here.

Supported by

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