Python类型转换器
项目描述
TypePigeon
TypePigeon是一个专注于在Python各种数据类型之间转换值的Python类型转换器。
pip install typepigeon
特性
- 使用
to_type()
直接将值从一种Python类型转换为另一种类型 - 使用
to_json()
将值转换为JSON格式 - 使用
subscripted_type()
将泛型别名(List[str]
)转换为简单集合类型([str]
)
使用方法
使用TypePigeon,您可以轻松地将简单值从一种类型转换为另一种类型
to_type()
import typepigeon
typepigeon.to_type(0.55, str)
'0.55'
typepigeon.to_type(1, float)
1.0
typepigeon.to_type([1], str)
'[1]'
此外,您还可以将值转换为集合
import typepigeon
typepigeon.to_type([1, 2.0, '3'], [int])
[1, 2, 3]
typepigeon.to_type('[1, 2, 3]', (int, str, float))
[1, '2', 3.0]
typepigeon.to_type({'a': 2.5, 'b': 4, 3: '18'}, {str: float})
{'a': 2.5, 'b': 4.0, '3': 18.0}
一些常用类,如datetime
和CRS
也得到支持
from datetime import datetime, timedelta
from pyproj import CRS
import typepigeon
typepigeon.to_type(datetime(2021, 3, 26), str)
'2021-03-26 00:00:00'
typepigeon.to_type('20210326', datetime)
datetime(2021, 3, 26)
typepigeon.to_type('01:13:20:00', timedelta)
timedelta(days=1, hours=13, minutes=20, seconds=0)
typepigeon.to_type(timedelta(hours=1), str)
'01:00:00.0'
typepigeon.to_type(timedelta(hours=1), int)
3600
typepigeon.to_type(CRS.from_epsg(4326), int)
4326
typepigeon.to_type(CRS.from_epsg(4326), str)
'GEOGCRS["WGS 84",ENSEMBLE["World Geodetic System 1984 ensemble",MEMBER["World Geodetic System 1984 (Transit)"],MEMBER["World Geodetic System 1984 (G730)"],MEMBER["World Geodetic System 1984 (G873)"],MEMBER["World Geodetic System 1984 (G1150)"],MEMBER["World Geodetic System 1984 (G1674)"],MEMBER["World Geodetic System 1984 (G1762)"],ELLIPSOID["WGS 84",6378137,298.257223563,LENGTHUNIT["metre",1]],ENSEMBLEACCURACY[2.0]],PRIMEM["Greenwich",0,ANGLEUNIT["degree",0.0174532925199433]],CS[ellipsoidal,2],AXIS["geodetic latitude (Lat)",north,ORDER[1],ANGLEUNIT["degree",0.0174532925199433]],AXIS["geodetic longitude (Lon)",east,ORDER[2],ANGLEUNIT["degree",0.0174532925199433]],USAGE[SCOPE["Horizontal component of 3D system."],AREA["World."],BBOX[-90,-180,90,180]],ID["EPSG",4326]]'
typepigeon.to_type(4326, CRS)
CRS.from_epsg(4326)
to_json()
from datetime import datetime
import typepigeon
typepigeon.to_json(5)
5
typepigeon.to_json('5')
'5'
typepigeon.to_json(datetime(2021, 3, 26))
'2021-03-26 00:00:00'
typepigeon.to_json([5, '6', {3: datetime(2021, 3, 27)}])
[5, '6', {3: '2021-03-27 00:00:00'}]
typepigeon.to_json({'test': [5, '6', {3: datetime(2021, 3, 27)}]})
{'test': [5, '6', {3: '2021-03-27 00:00:00'}]}
subscripted_type()
import typepigeon.types
from typing import Dict, List, Tuple
import typepigeon
typepigeon.types.subscripted_type(List[str])
[str]
typepigeon.types.subscripted_type(Dict[str, float])
{str: float}
typepigeon.types.subscripted_type({str: (Dict[int, str], str)})
{str: ({int: str}, str)}
项目详情
下载文件
下载您平台上的文件。如果您不确定选择哪个,请了解更多关于 安装包 的信息。
源分布
typepigeon-2.0.3.tar.gz (55.0 kB 查看哈希值)
构建分发包
typepigeon-2.0.3-py3-none-any.whl (32.0 kB 查看哈希值)
关闭
typepigeon-2.0.3.tar.gz 的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | e75f57c7b0b1265ecf6a266b00f988bea695821fad7807a4128e9364aa85ee17 |
|
MD5 | ed6bf9b2cd062028547427890e3e9334 |
|
BLAKE2b-256 | bd2217c4d75e69ecf6805d4b2789bcc93fece1e81e99580190c06ec437145e89 |
关闭
typepigeon-2.0.3-py3-none-any.whl 的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | ec5d5205a5ea9d8c917590f64f7686dcee334cd401073fe90445a1da2e435cbf |
|
MD5 | 6e21e3326cce9ad42e8c8abe778abd4a |
|
BLAKE2b-256 | cf74e17369eb9e14545badb4cf6a21f3ac8a9683c02dd46221a9ac46af636d98 |