MCPcopy Index your code
hub / github.com/ipython/ipython / float_doctest

Function float_doctest

IPython/sphinxext/custom_doctests.py:85–149  ·  view source on GitHub ↗

Doctest which allow the submitted output to vary slightly from the input. Here is how it might appear in an rst file: .. code-block:: rst .. ipython:: @doctest float In [1]: 0.1 + 0.2 Out[1]: 0.3

(sphinx_shell, args, input_lines, found, submitted)

Source from the content-addressed store, hash-verified

83 return a
84
85def float_doctest(sphinx_shell, args, input_lines, found, submitted):
86 """
87 Doctest which allow the submitted output to vary slightly from the input.
88
89 Here is how it might appear in an rst file:
90
91 .. code-block:: rst
92
93 .. ipython::
94
95 @doctest float
96 In [1]: 0.1 + 0.2
97 Out[1]: 0.3
98
99 """
100 import numpy as np
101
102 if len(args) == 2:
103 rtol = 1e-05
104 atol = 1e-08
105 else:
106 # Both must be specified if any are specified.
107 try:
108 rtol = float(args[2])
109 atol = float(args[3])
110 except IndexError:
111 e = ("Both `rtol` and `atol` must be specified "
112 "if either are specified: {0}".format(args))
113 raise IndexError(e) from e
114
115 try:
116 submitted = str_to_array(submitted)
117 found = str_to_array(found)
118 except:
119 # For example, if the array is huge and there are ellipsis in it.
120 error = True
121 else:
122 found_isnan = np.isnan(found)
123 submitted_isnan = np.isnan(submitted)
124 error = not np.allclose(found_isnan, submitted_isnan)
125 error |= not np.allclose(found[~found_isnan],
126 submitted[~submitted_isnan],
127 rtol=rtol, atol=atol)
128
129 TAB = ' ' * 4
130 directive = sphinx_shell.directive
131 if directive is None:
132 source = 'Unavailable'
133 content = 'Unavailable'
134 else:
135 source = directive.state.document.current_source
136 # Add tabs and make into a single string.
137 content = '\n'.join([TAB + line for line in directive.content])
138
139 if error:
140
141 e = ('doctest float comparison failure\n\n'
142 'Document source: {0}\n\n'

Callers

nothing calls this directly

Calls 2

str_to_arrayFunction · 0.85
formatMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…