PageRenderTime 14ms CodeModel.GetById 10ms app.highlight 2ms RepoModel.GetById 1ms 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"""
 2Expression evaluation support.
 3
 4For the moment this depends on python's eval. In the future it should be
 5replaced with a "safe" parser.
 6"""
 7
 8from UserDict import DictMixin
 9
10class ExpressionContext( object, DictMixin ):
11    def __init__( self, dict, parent=None ):
12        """
13        Create a new expression context that looks for values in the
14        container object 'dict', and falls back to 'parent'
15        """
16        self.dict = dict
17        self.parent = parent
18    def __getitem__( self, key ):
19        if key in self.dict:
20            return self.dict[key]
21        if self.parent is not None and key in self.parent:
22            return self.parent[key]
23        raise KeyError( key )
24    def __setitem__( self, key, value ):
25        self.dict[key] = value
26    def __contains__( self, key ):
27        if key in self.dict:
28            return True
29        if self.parent is not None and key in self.parent:
30            return True
31        return False
32    def __str__( self ):
33        return str( self.dict )
34    def __nonzero__( self ):
35        if not self.dict and not self.parent:
36            return False
37        return True