Skip to content

Commit

Permalink
[Python] Accept numpy types for python_to_anyvalue
Browse files Browse the repository at this point in the history
Also expose AnyValue utility functions from _utils.pyx
  • Loading branch information
ischoegl authored and speth committed Mar 16, 2023
1 parent bfa7545 commit 67a9833
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
3 changes: 3 additions & 0 deletions interfaces/cython/cantera/_utils.pxd
Expand Up @@ -95,3 +95,6 @@ cdef Composition comp_map(X) except *

cdef CxxAnyMap dict_to_anymap(dict data, cbool hyphenize=*) except *
cdef anymap_to_dict(CxxAnyMap& m)

cdef CxxAnyValue python_to_anyvalue(item, name=*) except *
cdef anyvalue_to_python(string name, CxxAnyValue& v)
6 changes: 4 additions & 2 deletions interfaces/cython/cantera/_utils.pyx
Expand Up @@ -239,6 +239,8 @@ cdef get_types(item):
itype = numbers.Integral
elif isinstance(item.flat[0], numbers.Real):
itype = numbers.Real
elif isinstance(item.flat[0], np.str_):
itype = str
else:
itype = item.dtype.type
return itype, item.ndim
Expand Down Expand Up @@ -319,9 +321,9 @@ cdef CxxAnyValue python_to_anyvalue(item, name=None) except *:
v = list_to_anyvalue(item)
elif isinstance(item, str):
v = stringify(item)
elif isinstance(item, bool):
elif isinstance(item, (bool, np.bool_)):
v = <cbool>(item)
elif isinstance(item, int):
elif isinstance(item, (int, np.int32, np.int64)):
v = <long int>(item)
elif isinstance(item, float):
v = <double>(item)
Expand Down

0 comments on commit 67a9833

Please sign in to comment.