Navigation Menu

Skip to content

DanielSolomon/seval

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Build Status

SEVAL

Safe evalutaion package which allows you to evaluate expressions safely (not only literals).

What's safe?

Currently seval evaluates:

  • arithmetic expressions
  • literals
  • any combination of the above

Installing

Install and update using pip:

$ pip install seval

Usage

>>> import ast
>>> import seval
>>>
>>> # Safe expression!
>>> safe_expression = '2 ** 3'
>>> ast.literal_eval(safe_expression)
ValueError: malformed node or string
>>> seval.safe_eval(safe_expression)
8
>>>
>>> # Function call - unsafe!
>>> seval.safe_eval('pow(2, 3)')
ValueError: no handler for node: call (Call(func=Name(id='pow', ctx=Load()), args=[Num(n=2), Num(n=3)], keywords=[]))
>>>
>>> # Another safe expression (which is combination of literals and arithmetic operations)!
>>> another_safe_expression = '["a", 5 * 5]'
>>> ast.literal_eval(another_safe_expression)
ValueError: malformed node or string
>>> seval.safe_eval(another_safe_expression)
['a', 25]