{"id":17361515,"url":"https://github.com/alexandergrooff/python-code-synthesis","last_synced_at":"2025-04-15T00:48:33.821Z","repository":{"id":54816719,"uuid":"141338936","full_name":"AlexanderGrooff/python-code-synthesis","owner":"AlexanderGrooff","description":null,"archived":false,"fork":false,"pushed_at":"2021-01-27T13:02:20.000Z","size":119,"stargazers_count":6,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-15T00:48:21.266Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/AlexanderGrooff.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-07-17T20:15:30.000Z","updated_at":"2023-06-10T11:45:48.000Z","dependencies_parsed_at":"2022-08-14T03:40:45.606Z","dependency_job_id":null,"html_url":"https://github.com/AlexanderGrooff/python-code-synthesis","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AlexanderGrooff%2Fpython-code-synthesis","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AlexanderGrooff%2Fpython-code-synthesis/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AlexanderGrooff%2Fpython-code-synthesis/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AlexanderGrooff%2Fpython-code-synthesis/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/AlexanderGrooff","download_url":"https://codeload.github.com/AlexanderGrooff/python-code-synthesis/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248986278,"owners_count":21194025,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-15T19:33:57.318Z","updated_at":"2025-04-15T00:48:33.803Z","avatar_url":"https://github.com/AlexanderGrooff.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"[![PyPI version](https://badge.fury.io/py/evalo.svg)](https://pypi.org/project/evalo/)\n# Python code synthesis\n\nThis project is used to interpret Python code and generate Python code based on given values.\n\nThis project is heavily influenced by [Barliman](https://github.com/webyrd/Barliman), which implemented\nthis exact idea for Scheme. While Barliman works wonders, I don't think Scheme is a programming\nlanguage that is widely used in production environments. This is the reason I created this\nproject, so that the principle of Barliman could be applied to Python.\n\n\n## Examples\nThis project can do both interpreting from Python code to values and vica versa. For example, AST\nvalues will be converted to the correct interpreted values:\n\n```\n\u003e from evalo import *\n\u003e x = var()\n\u003e run(1, x, eval_expro(ast.BinOp(left=ast.Num(n=1), op=ast.Add(), right=ast.Num(n=1)), [], x))\n\n(2,)\n```\n\nInterpreting from values to Python code will generate AST objects, which can be directly translated\nto Python code using external libraries. For example, if we want 5 different versions of Python code\nthat are interpreted to the value `2`, we can do the following:\n\n```\n\u003e from evalo import *\n\u003e x = var()\n\u003e run(5, x, eval_expro(x, [], 2))\n\n(\u003c_ast.Num at 0x7fdf44aabe48\u003e,\n \u003c_ast.BinOp at 0x7fdf44a87fd0\u003e,\n \u003c_ast.BinOp at 0x7fdf44a1d320\u003e,\n \u003c_ast.BinOp at 0x7fdf44a444e0\u003e,\n \u003c_ast.BinOp at 0x7fdf46ee36a0\u003e)\n```\n\nTo translate this to human-readable values, we can use `ast_dump_if_possible`:\n```\n\u003e [ast_dump_if_possible(y) for y in run(5, x, eval_expro(x, [], 2, maxdepth=3))]\n\n['Num(n=2)',\n 'BinOp(left=Num(n=0), op=Add(), right=Num(n=2))',\n 'BinOp(left=Num(n=0), op=Sub(), right=BinOp(left=Num(n=0), op=Sub(), right=Num(n=2)))',\n 'BinOp(left=Num(n=1), op=Mult(), right=Num(n=2))',\n 'BinOp(left=BinOp(left=Num(n=0), op=Add(), right=Num(n=0)), op=Add(), right=Num(n=2))']\n```\n\nUsing the [astunparse](https://github.com/simonpercivall/astunparse) library we can directly translate this output to Python source code:\n\n```\n\u003e [astunparse.unparse(y).strip() for y in run(10, x, eval_expro(x, [], 2, maxdepth=3))]\n\n['2',\n '(0 + 2)',\n '(0 - (0 - 2))',\n '(1 * 2)',\n '((0 + 0) + 2)',\n '((0 + 0) - (0 - 2))',\n '((1 + 0) * 2)',\n '((0 - 0) + 2)',\n '((0 - 0) - (0 - 2))',\n '((1 - 0) * 2)']\n```\n\n## Development\nIf you want to help develop on this project, you can install it like so using Python 3.7:\n```\nmkvirtualenv -a $(pwd) $(basename $(pwd)) -p python3\npip install -r requirements/development.txt\npre-commit install\n```\n\nThis project uses `tox` with `pytest` for testing, so just run `tox` to run all tests.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falexandergrooff%2Fpython-code-synthesis","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Falexandergrooff%2Fpython-code-synthesis","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falexandergrooff%2Fpython-code-synthesis/lists"}