Advertisement
Guest User

Untitled

a guest
Mar 18th, 2014
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import operator, re
  2.  
  3. def _match(pattern, _string):
  4.     return True if re.match(pattern, _string) else False
  5.  
  6. numeric_types_operators = { '<' : operator.lt, '<=': operator.le, '==': operator.eq, '!=': operator.ne, '>=': operator.ge,'>' : operator.gt }
  7.  
  8. allowed_operators = {
  9.     boolean : {
  10.         '==': operator.eq,
  11.         '!=': operator.ne,
  12.     }, int : numeric_types_operators,
  13.     float : numeric_types_operators,
  14.     string : {
  15.         'match' : _match
  16.     }
  17. }
  18.  
  19. def evaluate(value, eval_string):
  20.     """ eval string example: operator value_to_compare """
  21.     operation, value_to_compare = eval_string.split(" ")
  22.     return allower_operators[type(value)][operation](value, value_to_compare)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement