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
Release history Release notifications | RSS feed
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)
Built Distribution
Close
Hashes for postfixcalc-0.8.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | b6b6b31af1c29b88c54e24541090b606fe5aa0ea081ad7672f434a2432371ffa |
|
MD5 | 8c4a291394a7f280bdec9a0462b51aff |
|
BLAKE2b-256 | ba5581d4351dbd320d0f9eee5edbe027952c9b09db1d6fd794e943302af3bcc6 |