Skip to main content

the stupid postfix evaluator

Project description

postfixcalc

Simple, stupid but easy and powerful infix expression evaluator using postfix notation.

User Guide

How to use?

from postfixcalc import Calc
expr = "(-1) ^ 2 + 3 - 4 * 8 - 2 ^ 3"
calc = Calc(expr)
print(calc.answer)
print(type(calc.answer))
-36
<class 'int'>

Explanation

expression: (-1) ^ 2 + 3 - 4 * 8 - 2 ^ 3

which with the math operator precedence is:

expression: ((-1) ^ 2) + 3 - (4 * 8) - (2 ^ 3)
            = (1) + 3 - (32) - (8)
            = 4 - 32 - 8
            = -28 - 8
            = -36

Initialization

calc = Calc(
    '(2 ^ 32) ^ (2 ^ 15) + -1',
    calc_timeout=1,         # timeout for the calculation (in seconds)
    str_repr_timeout=1.5    # timeout to generate the string representation (in seconds)
)
print(f"'(2 ^ 32) ^ (2 ^ 15)'s answer has '{len(calc.stranswer)}' digits")
'(2 ^ 32) ^ (2 ^ 15)'s answer has '315653' digits
print(f'answer is: {calc.stranswer[:5]}...{calc.stranswer[-5:]}')
answer is: 67411...79135

Other Attributes

from rich.pretty import Pretty
from rich import print as rprint
rprint(calc.parsed)
<ast.BinOp object at 0x7f93380b1330>
rprint(calc.extracted)
[
    (
        [
            (
                [([2], <ast.Pow object at 0x7f933cc8d150>, [32])],
                <ast.Pow object at 0x7f933cc8d150>,
                [([2], <ast.Pow object at 0x7f933cc8d150>, [15])]
            )
        ],
        <ast.Add object at 0x7f933cc8cf10>,
        [(<ast.USub object at 0x7f933cc8d570>, [1])]
    )
]
rprint(calc.flattened)
(
    (
        (([2], <ast.Pow object at 0x7f933cc8d150>, [32]),),
        <ast.Pow object at 0x7f933cc8d150>,
        (([2], <ast.Pow object at 0x7f933cc8d150>, [15]),)
    ),
    <ast.Add object at 0x7f933cc8cf10>,
    (<ast.USub object at 0x7f933cc8d570>, [1])
)
rprint(calc.strparenthesized)
(((2 ^ 32)) ^ ((2 ^ 15))) + (-1)
rprint(calc.listparenthesized)
['(', '(', '(', 2, '^', 32, ')', ')', '^', '(', '(', 2, '^', 15, ')', ')', ')', '+', '(', '-1', ')']
rprint(calc.numerized)
['(', '(', '(', 2, '^', 32, ')', ')', '^', '(', '(', 2, '^', 15, ')', ')', ')', '+', '(', -1, ')']
rprint(calc.postfix)
[2, 32, '^', 2, 15, '^', '^', -1, '+']
rprint(f'{calc.stranswer[:5]}...{calc.stranswer[-5:]}')
67411...79135

Calc Documentation

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

postfixcalc-0.8.1.tar.gz (11.1 kB view hashes)

Uploaded Source

Built Distribution

postfixcalc-0.8.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