PageRenderTime 15ms CodeModel.GetById 5ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/galaxy/util/expressions.py

https://bitbucket.org/cistrome/cistrome-harvard/
Python | 37 lines | 4 code | 0 blank | 33 comment | 3 complexity | 84fd38adcb6563a6467538ee780eba94 MD5 | raw file
  1. """
  2. Expression evaluation support.
  3. For the moment this depends on python's eval. In the future it should be
  4. replaced with a "safe" parser.
  5. """
  6. from UserDict import DictMixin
  7. class ExpressionContext( object, DictMixin ):
  8. def __init__( self, dict, parent=None ):
  9. """
  10. Create a new expression context that looks for values in the
  11. container object 'dict', and falls back to 'parent'
  12. """
  13. self.dict = dict
  14. self.parent = parent
  15. def __getitem__( self, key ):
  16. if key in self.dict:
  17. return self.dict[key]
  18. if self.parent is not None and key in self.parent:
  19. return self.parent[key]
  20. raise KeyError( key )
  21. def __setitem__( self, key, value ):
  22. self.dict[key] = value
  23. def __contains__( self, key ):
  24. if key in self.dict:
  25. return True
  26. if self.parent is not None and key in self.parent:
  27. return True
  28. return False
  29. def __str__( self ):
  30. return str( self.dict )
  31. def __nonzero__( self ):
  32. if not self.dict and not self.parent:
  33. return False
  34. return True