Skip to content

FatExpression is python class used for calculating text-presented expressions

License

Notifications You must be signed in to change notification settings

allan-lima/fatexpression

Repository files navigation

FatExpression

https://badge.fury.io/py/fatexpression.png https://travis-ci.org/allan-lima/fatexpression.png?branch=master https://pypip.in/d/fatexpression/badge.png https://coveralls.io/repos/allan-lima/fatexpression/badge.png

FatExpression is python class used for calculating text-presented expressions.

The class FatExpression (Python) is based in TFatExpression (Delphi).

The class TFatExpression by Gasper Kozak (gasper.kozak@email.si)

version: 1.0 beta, June 2001.

version: 1.3, Dezembre 2002. bug fix e new features by Allan Lima

FatExpression and TFatExpression is component used for calculating text-presented expressions.

FatExpression and TFatExpression is open-source and is free for all use.

Features

  • keys, brackets, parenthesis: {}, [], ()

basic operations:

- addition:         +      3 + 2 = 5
- substration:      -      3 - 2 = 1
- multiplication:   *      3 * 2 = 6
- division:         /      3 / 2 = 1.5
- exponentiation:   ^      3 ^ 2 = 9
- factorial:       !      3! = 6
- module:           %      3 % 2 = 1
- unary minus:      -      (-x) = x * (-1)

relational operations:

- minor: < -> 3 < 2 = 0
- major: > -> 3 > 2 = 1
- major or equal: >=  -> 3 >= 2 = 1
- minor or egual: <=  -> 3 <= 2 = 0
- different: <>  -> 3 <> 2 = 1
- egual: =  -> 3 = 2 = 0

logic operations:

- and: & -> 1 & 1 = 1, 1 & 0 = 0
- or: | -> 1 | 0 = 1, 0 | 0 = 0
- exclusive or: ? -> 1 ? 0 = 1, 1 ? 1 = 0
- negation: ~ -> ~1 = 0, ~0 = 1
  • mathematical functions: abs, frac, max, min, mod, round, sign, sqrt, exp, trunc, sum

Example:

python
>> variables = ['a=1.1','b=2.2','c=3.3']
>> exp = FatExpression()
>> exp.addVariables(variables)
>> exp.text = 'trunc(max(a, b, c))'
>> print(exp.value)
>> 3.0
>> exp.text = 'round(sum(a, b, c))'
>> print(exp.value)
>> 7.0
  • geometric functions: sin, cos, tan, atan, log, exp

Example:

python
>> exp = FatExpression()
>> exp.text = 'log(1)'
>> print(exp.value)
>> 0
  • various functions: and, or, if, random

Example:

python
>> variables = ['a=1','b=2','c=3']
>> exp = FatExpression()
>> exp.addVariables(variables)
>> exp.text = 'if(a=1, b*10, c*10)'
>> print(exp.value)
>> 20.0
  • variables:

Example:

python

>> variables = ['a=1','b=2','c=3'] # variables = {'a':1,'b':2,'c':3} or variables = 'a=1;b=2;c=3'
>> exp = FatExpression()
>> exp.addVariables(variables)
>> exp.text = 'a+b+c'
>> print(exp.value)
>> 6.0
  • user-defined functions (udf): format function_name [ (argument_name [, argument_name ... ]] = expression

Example:

python
>> functions = ['x(a,b)=a*b', 't1(a)=a+10'] # functions = 'x(a,b)=a*b;t1(a)=a+10'
>> exp = FatExpression()
>> exp.addFunctions(functions)
>> exp.text = 'x(1,3)+t1(2)'
>> print(exp.value)
>> 15.0
  • evaluate: words are processed by unresolved events "evaluates" recorded addEvaluate().

Example:

python
>> def test(text, args, argCount):
>>     if text == 'y':
>>         return 3
>> exp = FatExpression()
>> exp.addEvaluate(test)
>> exp.text = 'y*2'
>> print(exp.value)
>> 6.0
  • multiples lines of text: undercore is value previous.

Example:

python
>> exp = FatExpression()
>> exp.text = ['y*2', '_+3*2']
>> print(exp.value)
>> 12.0
>> exp.text = ['a:y*2', 'a+3*2']
>> print(exp.value)
>> 12.0

About

FatExpression is python class used for calculating text-presented expressions

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published