everything2everything.core 模块是 everything2everything 库的核心实现,提供了全面的类型转换兼容层,支持所有Python标准数据类型和第三方库类型之间的双向转换。
该模块的主要功能包括:
TypeConverter 是库的核心类,提供了第三方库类型之间的互转功能,以及标准类型的转换方法。
class TypeConverter:
"""类型转换兼容层
A comprehensive type conversion compatibility layer that supports bidirectional
conversion between all standard Python data types and third-party library types.
"""
通用类型转换方法,支持将任意类型转换为指定的目标类型。
| 参数 | 类型 | 描述 |
|---|---|---|
| obj | Any | 要转换的对象 |
| target_type | str | 目标类型,可以是以下字符串之一: 'numpy', 'cupy', 'scipy', 'pandas', 'torch', 'xarray', 'jax', 'tensorflow' |
ValueError - 如果目标类型无效TypeError - 如果无法转换输入对象from everything2everything import TypeConverter
import numpy as np
# 将numpy数组转换为torch张量
arr = np.array([1, 2, 3])
tensor = TypeConverter.convert(arr, 'torch')
print(type(tensor)) # 输出: <class 'torch.Tensor'>
# 将torch张量转换为tensorflow张量
tensor = TypeConverter.convert(tensor, 'tensorflow')
print(type(tensor)) # 输出: <class 'tensorflow.Tensor'>
将numpy数组转换为xarray DataArray,自动生成维度和坐标。
| 参数 | 类型 | 描述 |
|---|---|---|
| obj | numpy.ndarray | 要转换的numpy数组 |
TypeError - 如果输入不是numpy数组将xarray DataArray或Dataset转换为numpy数组。
| 参数 | 类型 | 描述 |
|---|---|---|
| obj | xarray.DataArray 或 xarray.Dataset | 要转换的xarray对象 |
TypeError - 如果输入不是xarray对象ValueError - 如果输入是空的xarray Dataset将torch Tensor转换为numpy数组,必要时从计算图中分离。
| 参数 | 类型 | 描述 |
|---|---|---|
| obj | torch.Tensor | 要转换的torch张量 |
TypeError - 如果输入不是torch张量将numpy数组转换为torch Tensor。
| 参数 | 类型 | 描述 |
|---|---|---|
| obj | numpy.ndarray | 要转换的numpy数组 |
TypeError - 如果输入不是numpy数组将jax数组转换为numpy数组。
| 参数 | 类型 | 描述 |
|---|---|---|
| obj | jax.numpy.ndarray | 要转换的jax数组 |
TypeError - 如果输入不是jax数组将numpy数组转换为jax数组。
| 参数 | 类型 | 描述 |
|---|---|---|
| obj | numpy.ndarray | 要转换的numpy数组 |
TypeError - 如果输入不是numpy数组将tensorflow Tensor转换为numpy数组。
| 参数 | 类型 | 描述 |
|---|---|---|
| obj | tensorflow.Tensor | 要转换的tensorflow张量 |
TypeError - 如果输入不是tensorflow张量将numpy数组转换为tensorflow Tensor。
| 参数 | 类型 | 描述 |
|---|---|---|
| obj | numpy.ndarray | 要转换的numpy数组 |
TypeError - 如果输入不是numpy数组将pandas DataFrame或Series转换为numpy数组。
| 参数 | 类型 | 描述 |
|---|---|---|
| obj | pandas.DataFrame 或 pandas.Series | 要转换的pandas对象 |
TypeError - 如果输入不是pandas对象将numpy数组转换为pandas Series或DataFrame。
| 参数 | 类型 | 描述 |
|---|---|---|
| obj | numpy.ndarray | 要转换的numpy数组 |
TypeError - 如果输入不是numpy数组ValueError - 如果输入数组维度大于2TypeConverter 类还提供了一系列标准类型的转换方法,这些方法都使用了 @conversion_cache 装饰器,以提升性能。
将任意对象转换为列表。
将任意对象转换为字符串。
将任意对象转换为整数。
将任意对象转换为浮点数。
将任意对象转换为字典。
将任意对象转换为集合。
将任意对象转换为元组。
模块提供了一系列与Python内置函数兼容的转换函数,这些函数是对 TypeConverter 类方法的封装,使用更加方便。
将任意对象转换为列表,默认为空列表。
| 参数 | 类型 | 描述 |
|---|---|---|
| obj | Any, optional | 要转换的对象,默认为None |
from everything2everything import e2e_list
# 基本类型转换
e2e_list(123) # 输出: [123]
e2e_list(None) # 输出: []
e2e_list("hello") # 输出: ['h', 'e', 'l', 'l', 'o']
# 容器类型转换
e2e_list({"a": 1, "b": 2}) # 输出: [("a", 1), ("b", 2)]
# 第三方库类型转换
import numpy as np
e2e_list(np.array([1, 2, 3])) # 输出: [1, 2, 3]
将任意对象转换为字符串,默认为空字符串。
| 参数 | 类型 | 描述 |
|---|---|---|
| obj | Any, optional | 要转换的对象,默认为空字符串 |
将任意对象转换为整数,默认为0。
| 参数 | 类型 | 描述 |
|---|---|---|
| obj | Any, optional | 要转换的对象,默认为0 |
将任意对象转换为浮点数,默认为0.0。
| 参数 | 类型 | 描述 |
|---|---|---|
| obj | Any, optional | 要转换的对象,默认为0.0 |
将任意对象转换为字典,默认为空字典。
| 参数 | 类型 | 描述 |
|---|---|---|
| obj | Any, optional | 要转换的对象,默认为None |
将任意对象转换为集合,默认为空集合。
| 参数 | 类型 | 描述 |
|---|---|---|
| obj | Any, optional | 要转换的对象,默认为None |
将任意对象转换为元组,默认为空元组。
| 参数 | 类型 | 描述 |
|---|---|---|
| obj | Any, optional | 要转换的对象,默认为空元组 |
缓存转换结果的装饰器,用于提升转换性能,避免重复转换。
| 参数 | 类型 | 描述 |
|---|---|---|
| func | callable | 转换函数 |
模块支持以下第三方库类型之间的互转:
模块使用了 conversion_cache 装饰器,对转换结果进行缓存,避免重复转换,提升性能。
在 convert 方法中,实现了一些直接转换路径,避免不必要的中间转换,例如:
第三方库采用延迟导入的方式,只有在需要时才会导入,减少启动时间和内存占用。
from everything2everything import (
e2e_list, e2e_str, e2e_int, e2e_float, e2e_dict, e2e_set, e2e_tuple
)
# 示例 1:基本类型转换
print(e2e_list(123)) # 输出: [123]
print(e2e_str(None)) # 输出: ""
print(e2e_int("123")) # 输出: 123
print(e2e_float(True)) # 输出: 1.0
# 示例 2:容器类型转换
print(e2e_list({"a": 1, "b": 2})) # 输出: [("a", 1), ("b", 2)]
print(e2e_dict([1, 2, 3])) # 输出: {0: 1, 1: 2, 2: 3}
print(e2e_set("hello")) # 输出: {'h', 'e', 'l', 'o'}
# 示例 3:双向转换
print(e2e_list(e2e_tuple([1, 2, 3]))) # 输出: [1, 2, 3]
print(e2e_dict(e2e_list({"a": 1, "b": 2}))) # 输出: {0: ("a", 1), 1: ("b", 2)}
from everything2everything import TypeConverter
import numpy as np
import torch
# 示例 1:numpy 与 torch 互转
arr = np.array([1, 2, 3])
tensor = TypeConverter.numpy_to_torch(arr)
print(type(tensor)) # 输出: <class 'torch.Tensor'>
arr2 = TypeConverter.torch_to_numpy(tensor)
print(type(arr2)) # 输出: <class 'numpy.ndarray'>
# 示例 2:使用通用转换方法
result = TypeConverter.convert(arr, 'torch') # 转换为torch张量
print(type(result)) # 输出: <class 'torch.Tensor'>
# 示例 3:numpy 与 pandas 互转
import pandas as pd
series = TypeConverter.numpy_to_pandas(arr)
print(type(series)) # 输出: <class 'pandas.Series'>
arr3 = TypeConverter.pandas_to_numpy(series)
print(type(arr3)) # 输出: <class 'numpy.ndarray'>
everything2everything.core 模块是一个功能强大、设计优雅的类型转换兼容层,它解决了Python中类型转换的各种问题,特别是在处理第三方库类型时的复杂性。
主要优势包括:
这个模块为Python开发者提供了一个统一、简洁的类型转换接口,大大简化了代码编写,提高了开发效率。