MCPcopy Index your code
hub / github.com/plotly/plotly.py / to_scalar_or_list

Function to_scalar_or_list

_plotly_utils/basevalidators.py:41–64  ·  view source on GitHub ↗
(v)

Source from the content-addressed store, hash-verified

39# Utility functions
40# -----------------
41def to_scalar_or_list(v):
42 # Handle the case where 'v' is a non-native scalar-like type,
43 # such as numpy.float32. Without this case, the object might be
44 # considered numpy-convertable and therefore promoted to a
45 # 0-dimensional array, but we instead want it converted to a
46 # Python native scalar type ('float' in the example above).
47 # We explicitly check if is has the 'item' method, which conventionally
48 # converts these types to native scalars.
49 np = get_module("numpy", should_load=False)
50 pd = get_module("pandas", should_load=False)
51 if np and np.isscalar(v) and hasattr(v, "item"):
52 return to_non_numpy_type(np, v)
53 if isinstance(v, (list, tuple)):
54 return [to_scalar_or_list(e) for e in v]
55 elif np and isinstance(v, np.ndarray):
56 if v.ndim == 0:
57 return to_non_numpy_type(np, v)
58 return [to_scalar_or_list(e) for e in v]
59 elif pd and isinstance(v, (pd.Series, pd.Index)):
60 return [to_scalar_or_list(e) for e in v]
61 elif is_numpy_convertable(v):
62 return to_scalar_or_list(np.array(v))
63 else:
64 return v
65
66
67def copy_to_readonly_numpy_array(v, kind=None, force_numeric=False):

Callers 14

validate_coerceMethod · 0.85
validate_coerceMethod · 0.85
validate_coerceMethod · 0.85
validate_coerceMethod · 0.85
validate_coerceMethod · 0.85
validate_coerceMethod · 0.85
validate_coerceMethod · 0.85
validate_coerceMethod · 0.85
validate_coerceMethod · 0.85
validate_coerceMethod · 0.85
validate_coerceMethod · 0.85

Calls 4

get_moduleFunction · 0.90
to_non_numpy_typeFunction · 0.85
is_numpy_convertableFunction · 0.85
arrayMethod · 0.45

Tested by

no test coverage detected