Skip to main content

Tokeniztion, posfixing and postfix calculation

Project description

EZmachining

image

String Expression Calculation

The project can translate infix expressions to postfix(str to tuple with members of different types). And can calculate if needed.

Used algorithm from Алгоритм сортировочной станции , also known as Shunting yard algorithm. I used less detailed russian wiki and had no choice but improove it a little - degree('^') handler added. Unary minus was created too - better fix negatives in postfixing than in calculating.

You can see project here https://github.com/wolkolak/postfixcalc

Initially I tried to find suitable module on PYPI but failed unfortunately:

Firstly expression can contain functions: sin, pow, mod, neg, etc. Most projects can't work with that.

Just why? There is no way for real expressions to not contain any functions. You need sin and cos at least. Is trigonometry only for sinners? I don't think so.

Secondly module should handle logic operators too: '<' , '==' , 'AND' , 'OR' , etc. And variables, too.

The third point - i need to create postfix expressions tens of times, and calculate it thousands of times. Because of that postfix expressions had to be turned into tuples. Entire module is a part of my CNC interpreter.

Fourth point - I don't want to see "divided by zero error", so calculator should return None in that case.


To calculate infix expression You need next functions:

  1. tokenize
    a = tokenize('-2^ -(sin(5)3)')
    print(a)
    ->>['-', 2.0, '^', '(', '-', '(', 'sin', '(', 5.0, ')', '
    ', 3.0, ')', ')']'

  2. tokensPostfixing
    b = tokensPostfixing(a)
    print(b)
    -->(2.0, 5.0, 'sin', 3.0, '*', '', '^', '')'

  3. postfixTokenCalc
    print(postfixTokenCalc(b, DICT_VARS=None))
    -0.8342390624578879
    dict_A = {'x': 5}
    print(postfixTokenCalc(('x', 1.0, '+', 6, '=='), DICT_VARS=dict_A))
    True

    print('complex?: ', postfixTokenCalc((5, 3j, '-',)))
    complex?: (5-3j)

As you can see, even complex numeric type didn't break the program. For now. I do not require complex in CNC, so I run only one test with it.
And '=' is not a mathematical or logical operator - your expression for calculation should not contain assignments in the middle(You can start from "=" though). Best time to handle assignments would be after tokenization, I believe.

If You need different operators and functions (or sin in radians for example), look at the followed dicts and modify:

  • OPERATORs_DICT_math:                     +, -, /, *, ^
  • OPERATORs_DICT_logic_L                    AND, NOT, OR', GT...
  • OPERATORs_DICT_func_1                   sqrt, pow1', abc, neg, mod, sin ...       # functions with 1 arg
  • OPERATORs_DICT_func_2                   pow2                                                   # functions with 2 args
  • OPERATORs_DICT_compare1               <, =, > (= should only be used as a part of '<=', '>=', '==' as explained above)
  • OPERATORs_DICT_compare2               <=, ==, <>, >=
  • OPERATORs_PRECEDENCE_DICT       functions and operators precedence, obviously

Also, You can use DICT_VARS to look for your variables. You can throw away some operators like 'GT' or 'EQ' - this is the same as '>' and '==' in some CNC and other languages, and not required for You, probably.

PS: This is my first attempt to share code with others, tell me if something wrong.

Project details


Release history Release notifications | RSS feed

This version

0.1

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

postfixcalc_advanced-0.1.tar.gz (8.9 kB view details)

Uploaded Source

Built Distribution

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

postfixcalc_advanced-0.1-py3-none-any.whl (13.4 kB view details)

Uploaded Python 3

File details

Details for the file postfixcalc_advanced-0.1.tar.gz.

File metadata

  • Download URL: postfixcalc_advanced-0.1.tar.gz
  • Upload date:
  • Size: 8.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.9.7

File hashes

Hashes for postfixcalc_advanced-0.1.tar.gz
Algorithm Hash digest
SHA256 a3e1b9eb28e65424d2c5887e81dfc180ab394f7ea72f76a6408d7bca1fc2c0a4
MD5 8a629d34257e02dcbf9c93ad80f86d34
BLAKE2b-256 cd8b21c49339b2c6099558597bb998c25ef86f5ac0792a5084a84aff0431392d

See more details on using hashes here.

File details

Details for the file postfixcalc_advanced-0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for postfixcalc_advanced-0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 f2bacc72c1c657c30eacd920bb8d26496e5e1c414145b40d9ffd8ea308a0c5de
MD5 e98d6329165a6a47a4cfa1beb65ca618
BLAKE2b-256 da4ee4e4ad5b41ef9bb1961bc74dc478da5a9a08068477ea137ed8cad54e9471

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