HTTPS SSH

Boolexp

Boolexp provides "safe" evaluation of boolean expressions for use cases where eval might be dangerous. Boolexp expressions are context-free and mimic Python sytax. Only basic math opearations and data structures are provided.

Instalation

To install Boolexp, change to the directory where setup.py is located and type:

python setup.py install

Requirements:

  • Python 2.7
  • pyparsing 1.5.0 or later

Expression Syntax Overview

Basic math operators *,/,+,- have highest priority

>>> from boolexp import Expression
>>> Expression("4 + 4 / 2").evaluate()
>>> 6
>>> Expression("( 4 + 4 ) / 2 ").evaluate()
>>> 4

Comparsion operators !=, ==, <, >, <=, >=, in are next in priority. All comparsion operators have return type of boolean, which can be used as input for boolean operators and, or, not with lowest priority.

>>> from boolexp import Expression
>>> Expression(2 == 1).evaluate()
>>> False
>>> Expression("2 * - 2 == - 4").evaluate()
>>> True
>>> Expression("not 2 == 3").evaluate()
>>> True
>>> Expression("3 <= 8 and 2 == 5").evaluate()
>>> False

Expresions with with variables

This all would not be much of a fun without posibility to use variables. Allowed types for variables are bool,int,float,str,list,dict

>>> from boolexp import Expression
>>> Expression("1 in v").evaluate(dict(v=[1,2]))
>>> True
>>> Expression("one[1].part == 2").evaluate(dict(one=[1, dict(part=2)], two=1))
>>> True

Disclaimer

This project is now in a state of proof of concept only, thus far from suitable for production enviroment.

License

Copyright (C) 2013, Peter Facka, Lukas Lamper

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.