forked from Bartman0/document-api-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexpression.py
More file actions
44 lines (37 loc) · 1.11 KB
/
expression.py
File metadata and controls
44 lines (37 loc) · 1.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
class Expression(object):
"""A class representing expression inside Data Sources."""
_LOGICALS = [
'AND',
'OR'
]
_COMPARISONS = [
'=',
'>',
'>=',
'<',
'<=',
'!=',
'<>'
]
_OPS = _LOGICALS + _COMPARISONS
def __init__(self, expression_xml):
"""Expression is usually instantiated by passing in expression XML
from a Relation.
"""
self._expressionXML = expression_xml
self._op = expression_xml.get('op')
self._expressions = list(map(Expression, self._expressionXML.findall('./expression')))
@property
def op(self):
return self._op
@property
def expressions(self):
return self._expressions
def __str__(self):
if self.op in Expression._LOGICALS:
return (" " + self.op + " ").join(map(str, self._expressions))
if self.op in Expression._COMPARISONS:
return f"{self._expressions[0]} {self.op} {self._expressions[1]}"
if len(self._expressions) == 0:
return f"{self.op}"
return "ERROR"