boolparse 0.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,16 @@
1
+ Metadata-Version: 2.1
2
+ Name: boolparse
3
+ Version: 0.1.0
4
+ Summary: BoolParse is a lightweight library to evaluate boolean expressions
5
+ Home-page: https://github.com/josemvas/boolparse
6
+ License: LGPLv3+
7
+ Keywords: parser boolean expression
8
+ Platform: UNKNOWN
9
+ Classifier: Programming Language :: Python :: 3 :: Only
10
+ Classifier: License :: OSI Approved :: GNU Lesser General Public License v3 or later (LGPLv3+)
11
+ Classifier: Operating System :: POSIX
12
+ Requires-Python: >=3.6
13
+
14
+ UNKNOWN
15
+
16
+
@@ -0,0 +1,5 @@
1
+ boolparse.py,sha256=OKRLx4pN4MHePtifdTCyMDLejq-lmXvgEi9FiIDXiLI,2644
2
+ boolparse-0.1.0.dist-info/METADATA,sha256=ymn1Esp23T_IvHYQ5uOQUoicpC1WkZzyhrYwL9XpwVs,472
3
+ boolparse-0.1.0.dist-info/WHEEL,sha256=G16H4A3IeoQmnOrYV4ueZGKSjhipXx8zc8nu9FGlvMA,92
4
+ boolparse-0.1.0.dist-info/top_level.txt,sha256=78n8ZjXNZNKkWcE_VWtucwv2VyNipcRmTaO3IJSiq-I,10
5
+ boolparse-0.1.0.dist-info/RECORD,,
@@ -0,0 +1,5 @@
1
+ Wheel-Version: 1.0
2
+ Generator: bdist_wheel (0.37.1)
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
5
+
@@ -0,0 +1 @@
1
+ boolparse
boolparse.py ADDED
@@ -0,0 +1,92 @@
1
+ __all__ = (
2
+ 'BoolParser',
3
+ )
4
+
5
+ import re
6
+
7
+ def tokenize(expr):
8
+ for token in re.findall(r'(?:[^ ()]+|[()])', expr):
9
+ yield token
10
+
11
+ class Node:
12
+ def __init__(self, left, right, name):
13
+ self.left = left
14
+ self.right = right
15
+ self.name = name
16
+ def pr(self):
17
+ a = '('
18
+ if self.left != None:
19
+ a += self.left.pr()
20
+ a += ' ' + self.name + ' '
21
+ if self.right != None:
22
+ a += self.right.pr()
23
+ a += ')'
24
+ return a
25
+ def evaluate(self):
26
+ if self.name == 'not':
27
+ return not self.right.evaluate()
28
+ elif self.name == 'and':
29
+ return self.left.evaluate() and self.right.evaluate()
30
+ elif self.name == 'or':
31
+ return self.left.evaluate() or self.right.evaluate()
32
+ elif self.name in evaldict:
33
+ return evaldict[self.name]
34
+ else:
35
+ raise Exception(self.name, 'not in value dict')
36
+
37
+ class BoolParser:
38
+ def __init__(self, expr):
39
+ self.tokens = tokenize(expr)
40
+ self.current = next(self.tokens, None)
41
+ self.etree = self.Disj()
42
+ def pr(self):
43
+ return self.etree.pr()
44
+ def evaluate(self, values):
45
+ global evaldict
46
+ evaldict = values
47
+ return self.etree.evaluate()
48
+ def accept(self, c):
49
+ if self.current == c:
50
+ self.current = next(self.tokens, None)
51
+ return True
52
+ return False
53
+ def expect(self, c):
54
+ if self.current == c:
55
+ self.current = next(self.tokens, None)
56
+ return True
57
+ raise Exception('Unexpected token', self.current, 'expected', c)
58
+ def Disj(self):
59
+ l = self.Conj()
60
+ if self.accept('or'):
61
+ r = self.Disj()
62
+ if r is None:
63
+ return None
64
+ return Node(l, r, 'or')
65
+ return l
66
+ def Conj(self):
67
+ l = self.Neg()
68
+ if self.accept('and'):
69
+ r = self.Conj()
70
+ if r is None:
71
+ return None
72
+ return Node(l, r, 'and')
73
+ return l
74
+ def Neg(self):
75
+ if self.accept('not'):
76
+ l = self.Lit()
77
+ if l is None:
78
+ return None
79
+ return Node(None, l, 'not')
80
+ return self.Lit()
81
+ def Lit(self):
82
+ if self.accept('('):
83
+ r = self.Disj()
84
+ if self.expect(')'):
85
+ return r
86
+ return None
87
+ l = self.current
88
+ self.current = next(self.tokens, None)
89
+ if re.fullmatch(r'[a-zA-Z0-9_.]+', l):
90
+ return Node(None, None, l)
91
+ else:
92
+ raise Exception('Expected an alphanumeric string')