treat built-in bools as ints, handle numpy scalars

This commit is contained in:
2021-03-05 19:35:01 +00:00
parent 71c0c82db2
commit 344768713d
2 changed files with 43 additions and 4 deletions

View File

@ -36,8 +36,9 @@ class PVInfo:
def infer_type(value, parse_string=True): #TODO lists/tuples/...? char vs. strings?
if isinstance(value, np.ndarray):
return infer_type_numpy(value) #TODO: also parse strings for np arrays?!
else:
return infer_type_scalar(value, parse_string=parse_string)
elif isinstance(value, np.generic): # covers all numpy scalars
value = value.item()
return infer_type_scalar(value, parse_string=parse_string)
@ -56,6 +57,11 @@ def infer_type_scalar(value, parse_string=True):
if isinstance(dtype, str):
raise Exception("how?")
#TODO: bool -> int or string?
if isinstance(value, bool):
dtype = int
value = int(value)
if dtype in DTYPES:
dtype = DTYPES[dtype]
else:

View File

@ -66,10 +66,12 @@ def test_it_None():
assert it(None) == ("string", "None")
def test_it_True():
assert it(True) == ("string", "True")
# assert it(True) == ("string", "True")
assert it(True) == ("int", 1)
def test_it_False():
assert it(False) == ("string", "False")
# assert it(False) == ("string", "False")
assert it(False) == ("int", 0)
def test_it_nan():
assert it(nan) == ("float", nan)
@ -92,6 +94,21 @@ def test_it_np2D_int():
arr = arr.reshape(x, y)
assert it(arr) == ("int", ref, n)
def test_it_np1D_float():
n = 10
arr = np.linspace(0.1, 23.4, n)
ref = arr.tolist()
assert it(arr) == ("float", ref, n)
def test_it_np2D_float():
x = y = 10
n = x * y
arr = np.linspace(0.1, 23.4, n)
ref = arr.tolist()
arr = arr.reshape(x, y)
assert it(arr) == ("float", ref, n)
def test_it_np1D_bool():
arr = np.array([True, False, True])
ref = [1, 0, 1]
@ -106,4 +123,20 @@ def test_it_np1D_object():
it(arr)
def test_it_np_scalar_int():
scalar = np.array(123)[()]
ref = scalar.item()
assert it(scalar) == ("int", ref)
def test_it_np_scalar_float():
scalar = np.array(1.23)[()]
ref = scalar.item()
assert it(scalar) == ("float", ref)
def test_it_np_scalar_bool():
scalar = np.array(True)[()]
ref = scalar.item()
assert it(scalar) == ("int", 1)