@@ -134,7 +134,7 @@ class ParseError(ValueError):
134
134
135
135
def parse_value (value_str : str ) -> float :
136
136
"""
137
- Parse a value string to return a float, e.g.
137
+ Parses a value string to return a float, e.g.
138
138
139
139
>>> parse_value('1')
140
140
1.
@@ -169,7 +169,7 @@ def parse_value(value_str: str) -> float:
169
169
170
170
def parse_inline_code (inline_code : str ) -> float :
171
171
"""
172
- Parse a Sphinx code string to return a float, e.g.
172
+ Parses a Sphinx code string to return a float, e.g.
173
173
174
174
>>> parse_value('``0``')
175
175
0.
@@ -208,7 +208,7 @@ class BoundFromDtype(FromDtypeFunc):
208
208
209
209
We can bound:
210
210
211
- 1. Keyword arguments that xps.from_dtype() can use, e.g.
211
+ 1. Keyword arguments that xps.from_dtype() can use, e.g.
212
212
213
213
>>> from_dtype = BoundFromDtype(kwargs={'min_value': 0, 'allow_infinity': False})
214
214
>>> strategy = from_dtype(xp.float64)
@@ -219,7 +219,7 @@ class BoundFromDtype(FromDtypeFunc):
219
219
220
220
i.e. a strategy that generates finite floats above 0
221
221
222
- 2. Functions that filter the elements strategy that xps.from_dtype() returns, e.g.
222
+ 2. Functions that filter the elements strategy that xps.from_dtype() returns, e.g.
223
223
224
224
>>> from_dtype = BoundFromDtype(filter=lambda i: i != 0)
225
225
>>> strategy = from_dtype(xp.float64)
@@ -230,7 +230,7 @@ class BoundFromDtype(FromDtypeFunc):
230
230
231
231
i.e. a strategy that generates any floats except 0
232
232
233
- 3. The underlying function that returns an elements strategy from a dtype, e.g.
233
+ 3. The underlying function that returns an elements strategy from a dtype, e.g.
234
234
235
235
>>> from_dtype = BoundFromDtype(
236
236
... from_dtype=lambda d: st.integers(
@@ -307,14 +307,48 @@ def __add__(self, other: BoundFromDtype) -> BoundFromDtype:
307
307
308
308
309
309
def wrap_strat_as_from_dtype (strat : st .SearchStrategy [float ]) -> FromDtypeFunc :
310
+ """
311
+ Wraps an elements strategy as a xps.from_dtype()-like function
312
+ """
310
313
def from_dtype (dtype : DataType , ** kw ) -> st .SearchStrategy [float ]:
311
- assert kw == {} # sanity check
314
+ assert len ( kw ) == 0 # sanity check
312
315
return strat
313
316
314
317
return from_dtype
315
318
316
319
317
320
def parse_cond (cond_str : str ) -> Tuple [UnaryCheck , str , FromDtypeFunc ]:
321
+ """
322
+ Parses a Sphinx-formatted condition string to return:
323
+
324
+ 1. A function which takes an input and returns True if it meets the
325
+ condition, otherwise False.
326
+ 2. A string template for expressing the condition.
327
+ 3. A xps.from_dtype()-like function which returns a strategy that generates
328
+ elements which meet the condition.
329
+
330
+ e.g.
331
+
332
+ >>> cond_func, expr_template, cond_from_dtype = parse_cond(
333
+ ... 'greater than ``0``'
334
+ ... )
335
+ >>> expr_template.replace('{}', 'x_i')
336
+ >>> expr_template.replace('{}', 'x_i')
337
+ 'x_i > 0'
338
+ >>> cond_func(42)
339
+ True
340
+ >>> cond_func(-128)
341
+ False
342
+ >>> strategy = cond_from_dtype(xp.float64)
343
+ >>> for _ in range(5):
344
+ ... print(strategy.example())
345
+ 1.
346
+ 0.1
347
+ 1.7976931348623155e+179
348
+ inf
349
+ 124.978
350
+
351
+ """
318
352
if m := r_not .match (cond_str ):
319
353
cond_str = m .group (1 )
320
354
not_cond = True
0 commit comments