@@ 259-303 (lines=45) @@ | ||
256 | __rand__ = __and__ |
|
257 | ||
258 | ||
259 | class Or(Fields.predicates): |
|
260 | """ |
|
261 | `Or` predicate. Exits at first sub-predicate that returns ``True``. |
|
262 | """ |
|
263 | ||
264 | def __init__(self, *predicates): |
|
265 | self.predicates = predicates |
|
266 | ||
267 | def __str__(self): |
|
268 | return 'Or(%s)' % ', '.join(str(p) for p in self.predicates) |
|
269 | ||
270 | def __repr__(self): |
|
271 | return '<hunter.predicates.Or: predicates=%r>' % (self.predicates,) |
|
272 | ||
273 | def __call__(self, event): |
|
274 | """ |
|
275 | Handles the event. |
|
276 | """ |
|
277 | for predicate in self.predicates: |
|
278 | if predicate(event): |
|
279 | return True |
|
280 | else: |
|
281 | return False |
|
282 | ||
283 | def __eq__(self, other): |
|
284 | if isinstance(other, Or): |
|
285 | if len(self.predicates) != len(other.predicates): |
|
286 | return False |
|
287 | return set(self.predicates) == set(other.predicates) |
|
288 | return NotImplemented |
|
289 | ||
290 | def __or__(self, other): |
|
291 | return Or(*chain(self.predicates, other.predicates if isinstance(other, Or) else (other,))) |
|
292 | ||
293 | def __and__(self, other): |
|
294 | return And(self, other) |
|
295 | ||
296 | def __invert__(self): |
|
297 | return Not(self) |
|
298 | ||
299 | def __hash__(self): |
|
300 | return hash(frozenset(self.predicates)) |
|
301 | ||
302 | __ror__ = __or__ |
|
303 | __rand__ = __and__ |
|
304 | ||
305 | ||
306 | class Not(Fields.predicate): |
|
@@ 212-256 (lines=45) @@ | ||
209 | __rand__ = __and__ |
|
210 | ||
211 | ||
212 | class And(Fields.predicates): |
|
213 | """ |
|
214 | `And` predicate. Exits at the first sub-predicate that returns ``False``. |
|
215 | """ |
|
216 | ||
217 | def __init__(self, *predicates): |
|
218 | self.predicates = predicates |
|
219 | ||
220 | def __str__(self): |
|
221 | return 'And(%s)' % ', '.join(str(p) for p in self.predicates) |
|
222 | ||
223 | def __repr__(self): |
|
224 | return '<hunter.predicates.And: predicates=%r>' % (self.predicates,) |
|
225 | ||
226 | def __call__(self, event): |
|
227 | """ |
|
228 | Handles the event. |
|
229 | """ |
|
230 | for predicate in self.predicates: |
|
231 | if not predicate(event): |
|
232 | return False |
|
233 | else: |
|
234 | return True |
|
235 | ||
236 | def __eq__(self, other): |
|
237 | if isinstance(other, And): |
|
238 | if len(self.predicates) != len(other.predicates): |
|
239 | return False |
|
240 | return set(self.predicates) == set(other.predicates) |
|
241 | return NotImplemented |
|
242 | ||
243 | def __or__(self, other): |
|
244 | return Or(self, other) |
|
245 | ||
246 | def __and__(self, other): |
|
247 | return And(*chain(self.predicates, other.predicates if isinstance(other, And) else (other,))) |
|
248 | ||
249 | def __invert__(self): |
|
250 | return Not(self) |
|
251 | ||
252 | def __hash__(self): |
|
253 | return hash(frozenset(self.predicates)) |
|
254 | ||
255 | __ror__ = __or__ |
|
256 | __rand__ = __and__ |
|
257 | ||
258 | ||
259 | class Or(Fields.predicates): |