convert#

convert(obj, from_type: str, to_type: str | list[str], as_scitype: str = None, store=None, store_behaviour: str = None, return_to_mtype: bool = False)[source]#

在不同的机器表示之间转换对象,受 scitype 约束。

参数:
obj要转换的对象 - 任何类型,应符合 as_scitype 的 mtype 规范
from_typestr - “obj” 的原始类型,一个有效的 mtype 字符串

包含解释的有效 mtype 字符串在 datatypes.MTYPE_REGISTER 中

to_typestr - 要将 “obj” 转换成的 mtype,一个有效的 mtype 字符串

或 str 列表,这指定了允许转换到的类型;如果是列表,将转换为与 from_mtype 具有相同 scitype 的第一个 mtype

as_scitypestr,可选 - 对象 “obj” 被视为的 scitype 名称

默认 = 从 from_type 推断;包含解释的有效 scitype 字符串在 datatypes.SCITYPE_REGISTER 中

store可选,用于有损转换的存储引用,默认 = None (无引用)

如果不是 None 且 store_behaviour=”reset” 或 “update”,则会通过副作用更新

store_behaviourstr,可选,可以是 None (默认)、”reset”、”freeze”、”update” 之一

“reset” - 存储被清空,然后根据转换结果更新 “freeze” - 存储是只读的,转换可以使用/读取但不能更改 “update” - 存储根据转换结果更新并保留之前的内容 None - 自动:“update”(如果存储为空且不是 None);否则为“freeze”

return_to_mtype: bool,可选 (默认=False)

如果为 True,还会返回转换成的 mtype 字符串

返回:
converted_objto_type - 将对象 obj 转换成的 mtype to_type

如果 objNone,则返回 None

to_typestr,仅当 return_to_mtype=True 时返回

converted_obj 的 mtype - 如果 to_type 是列表,则很有用

抛出:
如果转换未实现,则抛出 KeyError
如果输入不符合规范,则抛出 TypeError 或 ValueError