to_type()
- typepigeon.to_type(input_value: Any, output_type: type | Collection[type]) Any
Convert a value to the specified type.
- Parameters:
input_value – Python value
output_type – type to convert to
- Returns:
converted value
>>> to_type(0.55, str) '0.55' >>> to_type('0.55', float) 0.55 >>> to_type('0.55', 'float') 0.55 >>> to_type(0.55, int) 0
>>> to_type([1], str) '[1]' >>> to_type([1, 2, '3', '4'], [int]) [1, 2, 3, 4] >>> to_type([1, 2, '3', '4'], (int, str, float, str)) (1, '2', 3.0, '4')
>>> to_type({'a': 2.5, 'b': 4, 3: '18'}, {str: float}) {'a': 2.5, 'b': 4.0, '3': 18.0}
>>> to_type(datetime(2021, 3, 26), str) '2021-03-26 00:00:00' >>> to_type('20210326', datetime) datetime(2021, 3, 26) >>> to_type('01:13:20:00', timedelta) timedelta(days=1, hours=13, minutes=20, seconds=0) >>> to_type(timedelta(hours=1), str) '01:00:00.0' >>> to_type(timedelta(hours=1), int) 3600
>>> to_type(CRS.from_epsg(4326), int) 4326 >>> 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]] >>> to_type(4326, CRS) CRS.from_epsg(4326)