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

Function str_to_array

IPython/sphinxext/custom_doctests.py:51–83  ·  view source on GitHub ↗

Simplistic converter of strings from repr to float NumPy arrays. If the repr representation has ellipsis in it, then this will fail. Parameters ---------- s : str The repr version of a NumPy array. Examples -------- >>> s = "array([ 0.3, inf, nan])"

(s)

Source from the content-addressed store, hash-verified

49"""
50
51def str_to_array(s):
52 """
53 Simplistic converter of strings from repr to float NumPy arrays.
54
55 If the repr representation has ellipsis in it, then this will fail.
56
57 Parameters
58 ----------
59 s : str
60 The repr version of a NumPy array.
61
62 Examples
63 --------
64 >>> s = "array([ 0.3, inf, nan])"
65 >>> a = str_to_array(s)
66
67 """
68 import numpy as np
69
70 # Need to make sure eval() knows about inf and nan.
71 # This also assumes default printoptions for NumPy.
72 from numpy import inf, nan
73
74 if s.startswith(u'array'):
75 # Remove array( and )
76 s = s[6:-1]
77
78 if s.startswith(u'['):
79 a = np.array(eval(s), dtype=float)
80 else:
81 # Assume its a regular float. Force 1D so we can index into it.
82 a = np.atleast_1d(float(s))
83 return a
84
85def float_doctest(sphinx_shell, args, input_lines, found, submitted):
86 """

Callers 1

float_doctestFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…