-
Notifications
You must be signed in to change notification settings - Fork 282
/
predicates.py
241 lines (203 loc) · 6.25 KB
/
predicates.py
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
"""Predicate functions."""
import functools
import numbers
import numpy as np
__all__ = ['are']
class are:
"""Predicate functions. The class is named "are" for calls to where.
For example, given a table, predicates can be used to pick rows as follows.
>>> from datascience import Table
>>> t = Table().with_columns([
... 'Sizes', ['S', 'M', 'L', 'XL'],
... 'Waists', [30, 34, 38, 42],
... ])
>>> t.where('Sizes', are.equal_to('L'))
Sizes | Waists
L | 38
>>> t.where('Waists', are.above(38))
Sizes | Waists
XL | 42
>>> t.where('Waists', are.above_or_equal_to(38))
Sizes | Waists
L | 38
XL | 42
>>> t.where('Waists', are.below(38))
Sizes | Waists
S | 30
M | 34
>>> t.where('Waists', are.below_or_equal_to(38))
Sizes | Waists
S | 30
M | 34
L | 38
>>> t.where('Waists', are.strictly_between(30, 38))
Sizes | Waists
M | 34
>>> t.where('Waists', are.between(30, 38))
Sizes | Waists
S | 30
M | 34
>>> t.where('Waists', are.between_or_equal_to(30, 38))
Sizes | Waists
S | 30
M | 34
L | 38
>>> t.where('Sizes', are.equal_to('L'))
Sizes | Waists
L | 38
>>> t.where('Waists', are.not_above(38))
Sizes | Waists
S | 30
M | 34
L | 38
>>> t.where('Waists', are.not_above_or_equal_to(38))
Sizes | Waists
S | 30
M | 34
>>> t.where('Waists', are.not_below(38))
Sizes | Waists
L | 38
XL | 42
>>> t.where('Waists', are.not_below_or_equal_to(38))
Sizes | Waists
XL | 42
>>> t.where('Waists', are.not_strictly_between(30, 38))
Sizes | Waists
S | 30
L | 38
XL | 42
>>> t.where('Waists', are.not_between(30, 38))
Sizes | Waists
L | 38
XL | 42
>>> t.where('Waists', are.not_between_or_equal_to(30, 38))
Sizes | Waists
XL | 42
>>> t.where('Sizes', are.containing('L'))
Sizes | Waists
L | 38
XL | 42
>>> t.where('Sizes', are.not_containing('L'))
Sizes | Waists
S | 30
M | 34
>>> t.where('Sizes', are.contained_in('MXL'))
Sizes | Waists
M | 34
L | 38
XL | 42
>>> t.where('Sizes', are.contained_in('L'))
Sizes | Waists
L | 38
>>> t.where('Sizes', are.not_contained_in('MXL'))
Sizes | Waists
S | 30
"""
@staticmethod
def equal_to(y):
"""Equal to y."""
return _combinable(lambda x: _equal_or_float_equal(x, y))
@staticmethod
def above(y):
"""Greater than y."""
return _combinable(lambda x: x > y)
@staticmethod
def below(y):
"""Less than y."""
return _combinable(lambda x: x < y)
@staticmethod
def above_or_equal_to(y):
"""Greater than or equal to y."""
return _combinable(lambda x: x >= y or _equal_or_float_equal(x, y))
@staticmethod
def below_or_equal_to(y):
"""Less than or equal to y."""
return _combinable(lambda x: x <= y or _equal_or_float_equal(x, y))
@staticmethod
def strictly_between(y, z):
"""Greater than y and less than z."""
return _combinable(lambda x: y < x < z)
@staticmethod
def between(y, z):
"""Greater than or equal to y and less than z."""
return _combinable(lambda x: (y <= x < z) or _equal_or_float_equal(x, y))
@staticmethod
def between_or_equal_to(y, z):
"""Greater than or equal to y and less than or equal to z."""
return _combinable(lambda x: (y <= x <= z) or _equal_or_float_equal(x, y) or _equal_or_float_equal(x, z))
@staticmethod
def containing(substring):
"""A string that contains within it the given substring."""
return _combinable(lambda x: substring in x)
@staticmethod
def contained_in(superstring):
"""A string that is part of the given superstring."""
return _combinable(lambda x: x in superstring)
@staticmethod
def not_equal_to(y):
"""Is not equal to y"""
return -(are.equal_to(y))
@staticmethod
def not_above(y):
"""Is not above y"""
return are.below_or_equal_to(y)
@staticmethod
def not_below(y):
"""Is not below y"""
return are.above_or_equal_to(y)
@staticmethod
def not_below_or_equal_to(y):
"""Is neither below y nor equal to y"""
return are.above(y)
@staticmethod
def not_above_or_equal_to(y):
"""Is neither above y nor equal to y"""
return are.below(y)
@staticmethod
def not_strictly_between(y, z):
"""Is equal to y or equal to z or less than y or greater than z"""
return -(are.strictly_between(y,z))
@staticmethod
def not_between(y, z):
"""Is equal to y or less than y or greater than z"""
return -(are.between(y,z))
@staticmethod
def not_between_or_equal_to(y,z):
"""Is less than y or greater than z"""
return -(are.between_or_equal_to(y,z))
@staticmethod
def not_containing(substring):
"""A string that does not contain substring"""
return -(are.containing(substring))
@staticmethod
def not_contained_in(superstring):
"""A string that is not contained within the superstring"""
return -(are.contained_in(superstring))
###############
# Combination #
###############
class _combinable:
"""A wrapper that allows one-arg predicate functions to be combined."""
def __init__(self, f):
self.f = f
functools.update_wrapper(self, f)
def __call__(self, x):
return self.f(x)
def __and__(self, other):
return _combinable(lambda x: self.f(x) and other.f(x))
def __or__(self, other):
return _combinable(lambda x: self.f(x) or other.f(x))
def __neg__(self):
return _combinable(lambda x: not self.f(x))
def __xor__(self, other):
return (self & -other) | (-self & other)
############
# Negation #
############
def _not(f):
return lambda *args: -f(*args)
def _equal_or_float_equal(x, y):
if isinstance(x, numbers.Real):
return x == y or np.nextafter(x, 1) == y or np.nextafter(x, 0) == y
else:
return x == y