@@ -327,14 +327,14 @@ def parse_cond(cond_str: str) -> Tuple[UnaryCheck, str, BoundFromDtype]:
327
327
condition, otherwise False.
328
328
2. A string template for expressing the condition.
329
329
3. A xps.from_dtype()-like function which returns a strategy that generates
330
- elements which meet the condition.
330
+ elements that meet the condition.
331
331
332
332
e.g.
333
333
334
334
>>> cond, expr_template, from_dtype = parse_cond('greater than ``0``')
335
335
>>> cond(42)
336
336
True
337
- >>> cond(-128 )
337
+ >>> cond(-123 )
338
338
False
339
339
>>> expr_template.replace('{}', 'x_i')
340
340
'x_i > 0'
@@ -582,8 +582,8 @@ def parse_unary_docstring(docstring: str) -> List[UnaryCase]:
582
582
...
583
583
... For floating-point operands,
584
584
...
585
- ... - If ``x_i`` is ``NaN``, the result is ``NaN``.
586
585
... - If ``x_i`` is less than ``0``, the result is ``NaN``.
586
+ ... - If ``x_i`` is ``NaN``, the result is ``NaN``.
587
587
... - If ``x_i`` is ``+0``, the result is ``+0``.
588
588
... - If ``x_i`` is ``-0``, the result is ``-0``.
589
589
... - If ``x_i`` is ``+infinity``, the result is ``+infinity``.
@@ -602,11 +602,16 @@ def parse_unary_docstring(docstring: str) -> List[UnaryCase]:
602
602
>>> unary_cases = parse_unary_docstring(sqrt.__doc__)
603
603
>>> for case in unary_cases:
604
604
... print(repr(case))
605
- UnaryCase(x_i == NaN -> NaN)
606
- UnaryCase(x_i < 0 -> NaN)
607
- UnaryCase(x_i == +0 -> +0)
608
- UnaryCase(x_i == -0 -> -0)
609
- UnaryCase(x_i == +infinity -> +infinity)
605
+ UnaryCase(<x_i < 0 -> NaN>)
606
+ UnaryCase(<x_i == NaN -> NaN>)
607
+ UnaryCase(<x_i == +0 -> +0>)
608
+ UnaryCase(<x_i == -0 -> -0>)
609
+ UnaryCase(<x_i == +infinity -> +infinity>)
610
+ >>> lt_0_case = unary_cases[0]
611
+ >>> lt_0_case.cond(-123)
612
+ True
613
+ >>> lt_0_case.check_result(-123, float('nan'))
614
+ True
610
615
611
616
"""
612
617
@@ -841,6 +846,22 @@ def integers_from_dtype(dtype: DataType, **kw) -> st.SearchStrategy[float]:
841
846
842
847
843
848
def parse_binary_case (case_str : str ) -> BinaryCase :
849
+ """
850
+ Parses a Sphinx-formatted binary case string to return codified binary cases, e.g.
851
+
852
+ >>> case_str = (
853
+ ... "If ``x1_i`` is greater than ``0``, ``x1_i`` is a finite number, "
854
+ ... "and ``x2_i`` is ``+infinity``, the result is ``NaN``."
855
+ ... )
856
+ >>> case = parse_binary_case(case_str)
857
+ >>> case
858
+ BinaryCase(<x1_i > 0 and isfinite(x1_i) and x2_i == +infinity -> NaN>)
859
+ >>> case.cond(42, float('inf'))
860
+ True
861
+ >>> case.check_result(42, float('inf'), float('nan'))
862
+ True
863
+
864
+ """
844
865
case_m = r_binary_case .match (case_str )
845
866
if case_m is None :
846
867
raise ParseError (case_str )
@@ -857,12 +878,12 @@ def parse_binary_case(case_str: str) -> BinaryCase:
857
878
raise ParseError (cond_str )
858
879
partial_expr = f"{ in_sign } x{ in_no } _i == { other_sign } x{ other_no } _i"
859
880
860
- input_wrapper = lambda i : - i if other_sign == "-" else noop
861
881
# For these scenarios, we want to make sure both array elements
862
882
# generate respective to one another by using a shared strategy.
863
883
shared_from_dtype = lambda d , ** kw : st .shared (
864
884
xps .from_dtype (d , ** kw ), key = cond_str
865
885
)
886
+ input_wrapper = lambda i : - i if other_sign == "-" else noop
866
887
if other_no == "1" :
867
888
868
889
def partial_cond (i1 : float , i2 : float ) -> bool :
@@ -1077,9 +1098,9 @@ def parse_binary_docstring(docstring: str) -> List[BinaryCase]:
1077
1098
>>> binary_cases = parse_binary_docstring(logaddexp.__doc__)
1078
1099
>>> for case in binary_cases:
1079
1100
... print(repr(case))
1080
- BinaryCase(x1_i == NaN or x2_i == NaN -> NaN)
1081
- BinaryCase(x1_i == +infinity and not x2_i == NaN -> +infinity)
1082
- BinaryCase(not x1_i == NaN and x2_i == +infinity -> +infinity)
1101
+ BinaryCase(< x1_i == NaN or x2_i == NaN -> NaN> )
1102
+ BinaryCase(< x1_i == +infinity and not x2_i == NaN -> +infinity> )
1103
+ BinaryCase(< not x1_i == NaN and x2_i == +infinity -> +infinity> )
1083
1104
1084
1105
"""
1085
1106
0 commit comments