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
|
# This file is part of A Boneless Datastructure Language
# Copyright (C) 2020 Soni L.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
"""Classes for use with ABDL's Predicate system.
See ``abdl.predicates.Predicate`` and the language reference for details.
"""
# pylint: disable=too-few-public-methods
class Predicate:
"""A predicate checks if an object is accepted in an ABDL expression.
"""
def accept(self, obj):
"""Checks if ``obj`` is accepted by this predicate.
Args:
obj: The object to be accepted.
Returns:
bool: True if the object is accepted, False otherwise.
"""
raise NotImplementedError
class IsInstance(Predicate):
"""A ``Predicate`` that accepts objects according to ``isinstance``.
Used to implement ``:$foo`` when ``defs['foo']`` is neither a ``Predicate`` nor a
``tuple``.
One generally does not need to explicitly create instances of this class, as abdl
accepts types wherever instances of this class are accepted.
Args:
ty (type): The expected type.
"""
def __init__(self, type_):
self.type_ = type_
"""The expected type."""
def accept(self, obj):
return isinstance(obj, self.type_)
class Union(Predicate):
"""A ``Predicate`` built from multiple predicates.
Used to implement ``:$foo`` when ``defs['foo']`` is not a ``Predicate`` but is a
``tuple``. Can be constructed from any iterable. If the iterable yields different
results on different runs (e.g. is a generator), the behaviour is undefined.
Has semantics equivalent to ``any(p.accept(obj) for p in predicates)``, except p
can also be a type or a tuple.
One generally does not need to explicitly create instances of this class, as abdl
accepts tuples wherever instances of this class are accepted.
Args:
predicates: An iterable of predicates, types and tuples that yields the same
sequence on different iterations.
"""
def __init__(self, predicates):
self.predicates = predicates
"""The iterable of predicates, types and tuples."""
def accept(self, obj):
for predicate in self.predicates:
if _to_predicate(predicate).accept(obj):
return True
return False
def _to_predicate(obj):
if isinstance(obj, Predicate):
return obj
if isinstance(obj, tuple):
return Union(obj)
# I don't know if anyone relies on the old behaviour of passing the thing directly to isinstance
# but this lets the exceptions be raised almost exactly like before
return IsInstance(obj)
|