[docs]defis_casadi_type(object:Any,recursive:bool=True)->bool:""" Returns a boolean of whether an object is a CasADi data type or not. If the recursive flag is True, iterates recursively, returning True if any subelement (at any depth) is a CasADi type. Args: object: The object to evaluate. recursive: If the object is a list or tuple, recursively iterate through every subelement. If any of the subelements (at any depth) are a CasADi type, return True. Otherwise, returns False. Returns: A boolean if the object is (or contains, if recursive=True) a CasADi data type. """t=type(object)# NumPy arrays cannot be or contain CasADi types, unless they are object arraysift==_onp.ndarrayandobject.dtype!="O":returnFalse# Skip certain Python types known not to be or contain CasADi types.fortype_to_skipin(float,int,complex,bool,str,range,type(None),bytes,bytearray,memoryview,):ift==type_to_skip:returnFalse# If it's directly a CasADi type, we're done.ift==_cas.MXort==_cas.DMort==_cas.SX:returnTrue# At this point, we know it's not a CasADi type, but we don't know if it *contains* a CasADi type (relevant if recursing)ifrecursive:if(issubclass(t,list)orissubclass(t,tuple)orissubclass(t,set)or(t==_onp.ndarrayandobject.dtype=="O")):forelementinobject:ifis_casadi_type(element,recursive=True):returnTruereturnFalseifissubclass(t,dict):forkvinobject.items():ifis_casadi_type(kv,recursive=True):returnTruereturnFalsereturnFalseelse:returnFalse
[docs]defis_iterable(x):""" Returns a boolean of whether an object is iterable or not. Args: x: Returns: """try:iter(x)returnTrueexceptTypeError:returnFalse