comparison planemo/lib/python3.7/site-packages/galaxy/util/expressions.py @ 1:56ad4e20f292 draft

"planemo upload commit 6eee67778febed82ddd413c3ca40b3183a3898f1"
author guerler
date Fri, 31 Jul 2020 00:32:28 -0400
parents
children
comparison
equal deleted inserted replaced
0:d30785e31577 1:56ad4e20f292
1 """
2 Expression evaluation support.
3
4 For the moment this depends on python's eval. In the future it should be
5 replaced with a "safe" parser.
6 """
7
8 from collections import MutableMapping
9 from itertools import chain
10
11
12 class ExpressionContext(MutableMapping):
13 def __init__(self, dict, parent=None):
14 """
15 Create a new expression context that looks for values in the
16 container object 'dict', and falls back to 'parent'
17 """
18 self.dict = dict
19 self.parent = parent
20
21 def __delitem__(self, key):
22 if key in self.dict:
23 del self.dict[key]
24 elif self.parent is not None and key in self.parent:
25 del self.parent[key]
26
27 def __iter__(self):
28 return chain(iter(self.dict), iter(self.parent or []))
29
30 def __len__(self):
31 return len(self.dict) + len(self.parent or [])
32
33 def __getitem__(self, key):
34 if key in self.dict:
35 return self.dict[key]
36 if self.parent is not None and key in self.parent:
37 return self.parent[key]
38 raise KeyError(key)
39
40 def __setitem__(self, key, value):
41 self.dict[key] = value
42
43 def __contains__(self, key):
44 if key in self.dict:
45 return True
46 if self.parent is not None and key in self.parent:
47 return True
48 return False
49
50 def __str__(self):
51 return str(self.dict)
52
53 def __bool__(self):
54 if not self.dict and not self.parent:
55 return False
56 return True
57 __nonzero__ = __bool__