Skip to main content

arithmeticParsing

Arithmetic parsing is a simple python library designed for parsing arithmetic expressions.
It is designed to be easy to use and to maintain, and does not focus on optimization as much as it does ease of use.

This library is designed to use minimal dependancies, and the only non-standard-library module this uses is treelib

Installation

To install, it is as easy as using pip on this git repository
I recommend using python -m pip instead of just pip, as this confirms that you are installing on the right python version

python3.9 -m pip install arithmetic-parsing

You can also install by cloning and then running setup.py

git clone https://github.com/wireboy5/arithmeticParsing
cd arithmeticParsing
python3.9 setup.py install

Usage

arithmetic-parsing was designed to be as easy as possible to use
A basic example:

import arithmetic_parsing

parseString = "(testVar1 + 2 * 6) + (testVar2 + 2 * 6)"

parser = arithmetic_parsing.Parser()
parsed = parser.parse(parseString)

print(parsed)

This will output this:

base
└── +
    ├── +
    │   ├── *
    │   │   ├── 2
    │   │   └── 6
    │   └── testVar1
    └── +
        ├── *
        │   ├── 2
        │   └── 6
        └── testVar2

Also, because this uses treelib, you can get a json result:

print(parsed.as_json())
{
    "base": {
        "children": [
            {
                "+": {
                    "children": [
                        {
                            "+": {
                                "children": [
                                    {
                                        "*": {
                                            "children": [
                                                "2",
                                                "6"
                                            ]
                                        }
                                    },
                                    "testVar1"
                                ]
                            }
                        },
                        {
                            "+": {
                                "children": [
                                    {
                                        "*": {
                                            "children": [
                                                "2",
                                                "6"
                                            ]
                                        }
                                    },
                                    "testVar2"
                                ]
                            }
                        }
                    ]
                }
            }
        ]
    }
}

Another output form is the list output

print(parsed.as_list())
[
    ['dyn', 'base_0', '+', 'testVar2', '12'], 
    ['dyn', 'base_1', '+', 'testVar1', '12'], 
    ['dyn', 'base_2', '+', 'base_1', 'base_0']
]

Lets take a look at the first item:

['dyn', 'base_0', '+', 'testVar2', '12']

And lets break it down

  • Dyn
    • This specifies that it is a dynamic value, and not a constant
  • base_0
    • This is the variable name, If you were to convert this into something like python.
  • +
    • This is the operator
  • testVar2
    • This is the first operand
  • 12
    • This is the second operand

Notice how it has 12 instead of 2 * 6?
That is because it is optimizing the result.
We can disable this optimization by setting optimize to false:

parser = arithmetic_parsing.Parser(
    optimized = False
)

Also notice that the parsed list has the values in a different order than you would expect?
The parser automatically sorts them to be as close as possible to their first reference, from the top down.
This to can be disabled:

parser = arithmetic_parsing.Parser(
    sort = False
)

We can use this to convert the value to assembly:

from arithmetic_parsing.examples import assembly

asm = assembly.listToAssembly(parsed.as_list(),parseString)

asm = "\n".join(asm)

print(asm)

This will output NASM code:

mov rbx, [testVar2]
add rbx, 12
mov rbx, [testVar1]
add rbx, 12
mov rax, rbx
add rax, rbx'

NOTE: Do not use this function for converting to assembly in an actual program.
This function is for demonstration purposes and has not been tested thouroughly

Metadata

Release files for arithmetic-parsing 0.0.1.post3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for arithmetic-parsing 0.0.1.post3
File Size Uploaded Provenance
arithmetic-parsing-0.0.1.post3.tar.gz 10.4 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for arithmetic-parsing 0.0.1.post3
File Interpreter ABI Platform
arithmetic_parsing-0.0.1.post3-py3-none-any.whl Python 3 none any Details

Total release size: 19.9 kB

Release files / arithmetic-parsing-0.0.1.post3.tar.gz

Download URL arithmetic-parsing-0.0.1.post3.tar.gz
Size 10.4 kB
Tags Source
SHA-256 checksum
How to use checksums
a9f087036d0081a8302e2da2d4f17668048277b47e15a37edad7d15b09b57d41
BLAKE2b-256 checksum
How to use checksums
799a2dc1b8451ac20b50b8d042d09718e2ff2acca51bc7f16166d1713b30a09b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.54.0 CPython/3.9.0rc1

Release files / arithmetic_parsing-0.0.1.post3-py3-none-any.whl

Download URL arithmetic_parsing-0.0.1.post3-py3-none-any.whl
Size 9.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
b129e8497c0c82ffa1b8fd2e482ef7f05d1340508dc124284d7c5a26519a63ee
BLAKE2b-256 checksum
How to use checksums
ff230b8d80b2a3db5f029b1e000128f4c2b1d816a15aa42e43d2f1a1c216328f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.54.0 CPython/3.9.0rc1

Security for arithmetic-parsing 0.0.1.post3

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page