MCPcopy Create free account
hub / github.com/numpy/numpy / get_expected_stringlength

Function get_expected_stringlength

numpy/core/tests/test_casting_unittests.py:39–71  ·  view source on GitHub ↗

Returns the string length when casting the basic dtypes to strings.

(dtype)

Source from the content-addressed store, hash-verified

37
38
39def get_expected_stringlength(dtype):
40 """Returns the string length when casting the basic dtypes to strings.
41 """
42 if dtype == np.bool_:
43 return 5
44 if dtype.kind in "iu":
45 if dtype.itemsize == 1:
46 length = 3
47 elif dtype.itemsize == 2:
48 length = 5
49 elif dtype.itemsize == 4:
50 length = 10
51 elif dtype.itemsize == 8:
52 length = 20
53 else:
54 raise AssertionError(f"did not find expected length for {dtype}")
55
56 if dtype.kind == "i":
57 length += 1 # adds one character for the sign
58
59 return length
60
61 # Note: Can't do dtype comparison for longdouble on windows
62 if dtype.char == "g":
63 return 48
64 elif dtype.char == "G":
65 return 48 * 2
66 elif dtype.kind == "f":
67 return 32 # also for half apparently.
68 elif dtype.kind == "c":
69 return 32 * 2
70
71 raise AssertionError(f"did not find expected length for {dtype}")
72
73
74class Casting(enum.IntEnum):

Callers 1

test_string_cancastMethod · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected