Skip to main content

the stupid postfix evaluator

Project description

postfixcalc

Simple and stupid infix to postfix converter and evaluator.

How does it work

The algorithm is very simple and straightforward

from postfixcalc.pyeval import evaluate
from postfixcalc.parser import (
    extract_nums_and_ops,
    flatten_nodes,
    infix_to_postfix,
    make_num,
    parse,
    relistexpression,
)

evaluate(
    infix_to_postfix(
        make_num(
            relistexpression(
                flatten_nodes(
                    extract_nums_and_ops(
                        parse('(-1) ^ 2')
                    ),
                ),
            ),
        ),
    ),
)

We should trace from bottom to top:

  1. parse the expression using ast.parse function. This function will parse the expression based on Python grammar and math op precedence.
  2. extract numbers, and operators outta parsed expression
  3. the extracted list contains many nested lists and tuples, so we flatten most of them
  4. we generate a better demonstration outta the flattened list
  5. we make possible strings to numbers, '-1' will be -1 and ...
  6. we generate the postfix notation outta the numbers and operators
  7. evaluate the result

But all this pain is done easily thorough Calc type in the library

from postfixcalc import Calc

calc = Calc('(-1) ^ 2')
print(calc.answer)

This is easy but Calc type provide other cached_propertied which are just the results of the upper functions

from postfixcalc import Calc

c = Calc("2 * -1")
print(c.parsed)
print(c.extracted)
print(c.flattened)
print(c.strparenthesized)
print(c.listparenthesized)
print(c.numerized)
print(c.postfix)
print(c.answer)
print(c.stranswer)

# <ast.BinOp object at 0x7fcd313ecbe0>
# [([2], <ast.Mult object at 0x7fcd32002a70>, [(<ast.USub object at 0x7fcd32003010>, [1])])]
# ([2], <ast.Mult object at 0x7fcd32002a70>, (<ast.USub object at 0x7fcd32003010>, [1]))
# 2 * (-1)
# [2, '*', '(', '-1', ')']
# [2, '*', '(', -1, ')']
# [2, -1, '*']
# -2
# -2

Important notes

  1. For safety reasons, calculations are done with a timeout which is absolutely easy to change:
c = Calc('...', timeout=10)

timeout is in seconds.

  1. answer property returns the actual object of the answer, whether it is int or float, BUT if you want to print the answer, you should consider the obj to str conversion time, it may be quite long or short depending on that obj; because of this Calc implements a new propery called stranswer which calculates the str repr with a timeout and raises exceptions if it would take long Always use stranswer if you want the str repr of the answer

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.7.2.tar.gz (9.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

postfixcalc-0.7.2-py3-none-any.whl (10.1 kB view details)

Uploaded Python 3

File details

Details for the file postfixcalc-0.7.2.tar.gz.

File metadata

  • Download URL: postfixcalc-0.7.2.tar.gz
  • Upload date:
  • Size: 9.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.3.1 CPython/3.10.4 Linux/6.0.6-76060006-generic

File hashes

Hashes for postfixcalc-0.7.2.tar.gz
Algorithm Hash digest
SHA256 3bfc83020d74d0e973112371130dda356c20fc3cd65f88101b9a4e0ecb54fec2
MD5 0c2924e3dedcb76f180bb54956b4ba23
BLAKE2b-256 687aa2297b913f3994df07e931e0184651d5594e0f3e12769f2492c9897fabff

See more details on using hashes here.

File details

Details for the file postfixcalc-0.7.2-py3-none-any.whl.

File metadata

  • Download URL: postfixcalc-0.7.2-py3-none-any.whl
  • Upload date:
  • Size: 10.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.3.1 CPython/3.10.4 Linux/6.0.6-76060006-generic

File hashes

Hashes for postfixcalc-0.7.2-py3-none-any.whl
Algorithm Hash digest
SHA256 814973bc22bf1f026824b4cd91fee34ed6124508f2984f3c17c78d5b4a08eae6
MD5 12afcbcb7ff8049cb647b0cd1003577b
BLAKE2b-256 f7fcf692ba6cf78c3489d1f4f63e9a2010ddc991972c6e7f1d0be8264559418b

See more details on using hashes here.

Supported by

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