Skip to main content

No project description provided

Project description

Expr Simplifier

🍋‍🟩 A tool for simplifying Python expressions that don't contain side effects, mainly for generated code

PyPI - Python Version pypi PyPI - Downloads LICENSE
uv ruff Gitmoji

Usage

Quick start in CLI

We recommend using the tool uv to try it without manually installing the package:

$ uvx expr_simplifier cse "a * 4 + (a * 4)"
(___t_0 := (a * 4)) + ___t_0
$ uvx expr_simplifier constant_folding "(___x := 1 + 1) + ___x" --max-iter=1
(___x := 2) + ___x
$ uvx expr_simplifier constant_folding "(___x := 1 + 1) + ___x" --max-iter=2
4
# uvx expr_simplifier logical_simplification "a and b and a"
a and b

As a library

$ uv add expr-simplifier
from __future__ import annotations

import ast

from expr_simplifier import auto_simplify

code = "a[1 + 1].b + a[3 - 1].b.c + (___x := 2) + (a[2].b and (___x == 2) and a[2].b)"
tree = ast.parse(code, mode="eval")
simplified_tree = auto_simplify(tree)
print(ast.unparse(simplified_tree))
# (___t_1 := a[2].b) + ___t_1.c + 2 + ___t_1

TODOs

  • Add runtime checks in uts

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

expr_simplifier-0.1.1.tar.gz (16.0 kB view hashes)

Uploaded Source

Built Distribution

expr_simplifier-0.1.1-py3-none-any.whl (10.8 kB view hashes)

Uploaded Python 3

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