-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathpredicate.py
More file actions
341 lines (296 loc) · 10.1 KB
/
Copy pathpredicate.py
File metadata and controls
341 lines (296 loc) · 10.1 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
#! /usr/bin/env python
'''predicates that can be chained together with boolean expressions before evaluation
e.g.
a = predicate(lambda s: 'a' in s)
b = predicate(lambda s: 'b' in s)
c = predicate(lambda s: 'c' in s)
anyof = a | b | c
allof = a & b & c
not_anyof = anyof != True
assert anyof('--a--')
assert allof('-abc-')
assert not_anyof('12345')
Also, generate predicates such as above from strings
pf = PredicateContainsFactory()
anyof2 = pf.predicate_from_string('a | b | c')
pallof2 = pf.predicate_from_string('a & b & c')
not_anyof2 = pf.predicate_from_string('!(a & b & c)')
assert anyof2('--a--')
assert allof2('-abc-')
assert not_anyof2('12345')
These can be very useful for filtering of dependency graphs
'''
import operator
import re
def defer(origfunc,*argfs,**argfd):
'''defer execution of the arguments of a function
given origfunc return a function such that the code
f = defer(origfunc, arga, key=argb)
f(*newargs, **newargd)
is equivalent to:
origfunc(arga(*newargs,**newargd), key=argb(*newargs,**newargd))
'''
def wrapper(*args, **argd):
newargs = [argf(*args, **argd) for argf in argfs]
newargd = dict((k,argf(*args, **argd)) for k,argf in argfd.items())
return origfunc(*newargs, **newargd)
wrapper.origfunc = origfunc
wrapper.argfs = argfs
wrapper.argfd = argfd
wrapper.__repr__ = lambda s: "defer <%s>( *(%s) **(%s)) " % (repr(origfunc),repr(argfs),repr(argfd))
return wrapper
def always(val):
'''returns a function that always returns val regardless of inputs'''
def alwaysf(*args, **argd): return val
alwaysf.val = val
return alwaysf
class predicate(object):
'''chainable predicates
e.g.
a = predicate(lambda s: 'a' in s)
b = predicate(lambda s: 'b' in s)
c = predicate(lambda s: 'c' in s)
anyof = a | b | c
allof = a & b & c
not_anyof = anyof != True
assert anyof('--a--')
assert allof('-abc-')
assert not_anyof('12345')
'''
def __init__(self, func):
self.func = func
def __call__(self, arg):
return self.func(arg)
def __and__(self,other):
return self.__defer_infix__(other,operator.__and__)
def __or__(self,other):
return self.__defer_infix__(other,operator.__or__)
def __ne__(self,other):
return self.__defer_infix__(other,operator.__ne__)
def __defer_infix__(self,other,op):
if isinstance(other, bool):
other = always(other)
elif not isinstance(other, predicate):
return NotImplemented
return self.__class__(defer(op, self, other))
def __repr__(self):
return 'pred( '+repr(self.func)+' )'
class notp(predicate):
'''exactly the same as a predicate but inverts it's __call__ output'''
def __call__(self, *args, **argd):
return not predicate.__call__(self, *args, **argd)
def partition_list(items, partition):
'''works like str.partition but for lists
e.g. partition(['aa','bb','cd','ee'],'cd') == ['aa','bb'],'cd',['ee']
partition(['aa','bb','cd','ee'],'ff') == ['aa','bb','cd','ee'],None,[]
'''
for i,obj in enumerate(items):
if obj == partition:
return items[:i],obj,items[i+1:]
return items,None,[]
class ParseSyntaxError(Exception): pass
class LexParse(object):
'''very simple lexer/parser'''
class _leaf(object):
def __init__(self, data): self.data = data
def __repr__(self): return '_leaf(%s)' %(repr(self.data))
valid_tokens = ['(',')','!','&','|']
def _match_bracket(self, tokens, i, bopen='(',bclose=')'):
'''find the closing bracket that matches an open bracket
return None if there is no matching bracket
otherwise the index into tokens of the close bracket that matches the opening bracket at position i
'''
assert i < len(tokens)
assert tokens[i] == bopen
depth = 0
for i in xrange(i,len(tokens)):
tok = tokens[i]
if tok == bopen:
depth += 1
elif tok == bclose:
depth -= 1
if depth < 0: return None
if depth == 0: return i
return None
def lex(self, s):
'''returns a list of tokens from a string
tokens returned are anything inside self.valid_tokens or
any other string not containing tokens, stripped
of leading and trailing whitespace
'''
s = s.strip()
if s == '': return []
for tok in self.valid_tokens:
l,t,r = s.partition(tok)
if t==tok: return self.lex(l)+[tok]+self.lex(r)
return [self._leaf(s)]
def parse(self, tokens):
'''parse a list of tokens in order of predicence and return the output'''
if len(tokens) == 0:
raise ParseSyntaxError('Cannot parse empty subexpression')
# Brackets
l,part,r = partition_list(tokens, '(')
if part != None:
if ')' in l: raise ParseSyntaxError('unmatched ) near',tokens)
r.insert(0,'(')
rindex = self._match_bracket(r, 0)
if rindex is None: raise ParseSyntaxError('unmatched ( near',tokens)
assert r[rindex] == ')'
inner = r[1:rindex]
r = r[rindex+1:]
inner = self.brackets(self.parse(inner))
return self.parse(l+[inner]+r)
# unary not
if tokens[0] == '!':
if len(tokens) < 2: raise ParseSyntaxError('syntax error near',tokens)
# this only works without other unary operators
if tokens[1] in self.valid_tokens: raise ParseSyntaxError('syntax error near', tokens)
argument = self.parse([ tokens[1] ])
inv = self.notx(argument)
return self.parse([inv]+tokens[2:])
# and
l,part,r = partition_list(tokens, '&')
if part != None:
if not len(l) or not len(r):
raise ParseSyntaxError('syntax error near', tokens)
l,r = self.parse(l), self.parse(r)
return self.andx(l,r)
# or
l,part,r = partition_list(tokens, '|')
if part != None:
if not len(l) or not len(r):
raise ParseSyntaxError('syntax error near', tokens)
l,r = self.parse(l), self.parse(r)
return self.orx(l,r)
if len(tokens) == 1:
if isinstance(tokens[0], self._leaf):
return self.data(tokens[0].data) # base case
elif tokens[0] in self.valid_tokens:
raise ParseSyntaxError('syntax error near',tokens)
return tokens[0] # Already parsed
# Nothing else is sane
print repr(tokens)
raise ParseSyntaxError('syntax error near', tokens)
def brackets(self, expr):
'''You almost never want to override this'''
return expr
def notx(self, expr): pass
def andx(self, expr_l, expr_r): pass
def orx(self, expr_l, expr_r): pass
def data(self, data): pass
class BoolParse(LexParse):
'''example parser implementation
bp = BoolParse()
assert False or (False and not (True or False)) == False
inp = 'False | (False & ! (True | False))'
assert bp.parse(bp.lex(inp)) is False
'''
notx = lambda s,expr: not expr
andx = lambda s,l,r: l and r
orx = lambda s,l,r: l or r
def data(self,data):
return not(data.lower() == 'false' or data == '0')
class PredicateContainsFactory(LexParse):
'''create predicates that act on the contents of a container passed to them'''
def predicate_from_string(self, definition):
tokens = self.lex(definition)
return self.parse(tokens)
def notx(self, pred):
return notp(pred)
def andx(self, pred_l, pred_r):
return pred_l & pred_r
def orx(self, pred_l, pred_r):
return pred_l | pred_r
def data(self, data):
return predicate(lambda container: data in container)
if __name__ == "__main__":
def defer_sample():
def a(arga, moo=None, argb=None):
return arga+argb
def b(arga, moo=None, argb=None):
return arga^argb
def c(arga, moo=None, argb=None):
return arga,argb
ooer = defer(c, a,argb=b)
result = ooer(1234,argb=4312)
assert result == (5546, 5130)
def predicate_sample():
a = predicate(lambda s: 'a' in s)
b = predicate(lambda s: 'b' in s)
c = predicate(lambda s: 'c' in s)
d = predicate(lambda s: 'd' in s)
anyof = a | b | c
allof = a & b & c
not_anyof = anyof != True
not_allof = allof != True
assert anyof('asdf')
assert allof('abc')
assert not anyof('1234')
assert not allof('ab')
assert not_anyof('1234')
assert not_allof('1234')
nottest = a & b & notp( c | d )
assert nottest('ab')
assert not nottest('abc')
assert not nottest('b')
assert not nottest('d')
assert not nottest('bd')
assert not nottest('abd')
e = predicate(lambda n: n%2==0)
t = predicate(lambda n: n%3==0)
eset = set(filter(e, range(1000)))
tset = set(filter(t, range(1000)))
eutset = set(filter(e|t, range(1000)))
eitset = set(filter(e&t, range(1000)))
assert eutset == eset.union(tset)
assert eitset == eset.intersection(tset)
def parser_internal_test():
lp = LexParse()
#lp._match_bracket(self, tokens, i, bopen='(',bclose=')'):
assert lp._match_bracket('()',0) == 1
assert lp._match_bracket('(',0) == None
assert lp._match_bracket(')))))()))))',5) == 6
assert lp._match_bracket('((()(()))(()((()(()()))())()))',0) == 29
assert lp._match_bracket('((()(()))(()((()(()()))())()))',2) == 3
assert lp._match_bracket('((()(()))(()((()(()()))())()))',12) == 25
assert lp._match_bracket('((()(()))(()((()(()()))())()))',23) == 24
assert lp._match_bracket('((()(()))(()((()(()()))())()))',23) == 24
assert lp._match_bracket('((()(()))(()((()(()()))())()))',26) == 27
assert lp._match_bracket('((()(()))(()((()(()()))())()))',9) == 28
assert lp._match_bracket('((()(()))(()((()(()()))())()))',10) == 11
assert lp._match_bracket('((()(()))(()((()(()()))())()))',16) == 21
def parser_sample():
bp = BoolParse()
assert False or (False and not (True or False)) == False
inp = 'False | (False & ! (True | False))'
assert bp.parse(bp.lex(inp)) is False
assert bp.parse(bp.lex('true & !false'))
def predicate_factory_sample():
pf = PredicateContainsFactory()
pred = pf.predicate_from_string('fish & !cow')
assert pred(['fish', 'bat', 'pidgeon'])
assert not pred( ['fish', 'cow', 'bat'] )
assert not pred( [] )
assert not pred( ['cow'] )
assert not pred( ['bat','pig'] )
a = predicate(lambda s: 'a' in s)
b = predicate(lambda s: 'b' in s)
c = predicate(lambda s: 'c' in s)
anyof2 = pf.predicate_from_string('a | b | c')
allof2 = pf.predicate_from_string('a & b & c')
not_anyof2 = pf.predicate_from_string('!(a & b & c)')
assert anyof2('--a--')
assert allof2('-abc-')
assert not_anyof2('12345')
pred = pf.predicate_from_string('( a | b | c ) & ( c | e | d )')
assert not pred('b')
assert pred('c')
assert pred('cd')
assert pred('acd')
assert not pred('ab')
assert not pred('a')
parser_internal_test()
defer_sample()
predicate_sample()
parser_sample()
predicate_factory_sample()