Skip to main content

Python type converter

Project description

TypePigeon

tests codecov build version license style

TypePigeon is a Python type converter focused on converting values between various Python data types.

pip install typepigeon

Features

  • convert values directly from one Python type to another with convert_value()
  • convert values to JSON format with convert_to_json()
  • convert generic aliases (List[str]) to simple collection types ([str]) with guard_generic_alias()

Usage

With TypePigeon, you can convert simple values from one type to another:

convert_value()

import typepigeon

typepigeon.convert_value(0.55, str)
'0.55'

typepigeon.convert_value(1, float)
1.0

typepigeon.convert_value([1], str)
'[1]'

Additionally, you can also cast values into a collection:

import typepigeon

typepigeon.convert_value([1, 2.0, '3'], [int])
[1, 2, 3]

typepigeon.convert_value('[1, 2, 3]', (int, str, float))
[1, '2', 3.0]

typepigeon.convert_value({'a': 2.5, 'b': 4, 3: '18'}, {str: float})
{'a': 2.5, 'b': 4.0, '3': 18.0}

Some commonly-used classes such as datetime and CRS are also supported:

from datetime import datetime, timedelta

from pyproj import CRS
import typepigeon

typepigeon.convert_value(datetime(2021, 3, 26), str)
'2021-03-26 00:00:00'

typepigeon.convert_value('20210326', datetime)
datetime(2021, 3, 26)

typepigeon.convert_value('01:13:20:00', timedelta)
timedelta(days=1, hours=13, minutes=20, seconds=0)

typepigeon.convert_value(timedelta(hours=1), str)
'01:00:00.0'

typepigeon.convert_value(timedelta(hours=1), int)
3600

typepigeon.convert_value(CRS.from_epsg(4326), int)
4326

typepigeon.convert_value(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.convert_value(4326, CRS)
CRS.from_epsg(4326)

convert_to_json()

from datetime import datetime

import typepigeon

typepigeon.convert_to_json(5)
5

typepigeon.convert_to_json('5')
'5'

typepigeon.convert_to_json(datetime(2021, 3, 26))
'2021-03-26 00:00:00'

typepigeon.convert_to_json([5, '6', {3: datetime(2021, 3, 27)}])
[5, '6', {3: '2021-03-27 00:00:00'}]

typepigeon.convert_to_json({'test': [5, '6', {3: datetime(2021, 3, 27)}]})
{'test': [5, '6', {3: '2021-03-27 00:00:00'}]}

guard_generic_alias()

from typing import Dict, List, Tuple

import typepigeon

typepigeon.guard_generic_alias(List[str])
[str]

typepigeon.guard_generic_alias(Dict[str, float])
{str: float}

typepigeon.guard_generic_alias({str: (Dict[int, str], str)})
{str: ({int: str}, str)}

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

typepigeon-1.0.8.tar.gz (6.1 kB view details)

Uploaded Source

Built Distribution

typepigeon-1.0.8-py3-none-any.whl (6.7 kB view details)

Uploaded Python 3

File details

Details for the file typepigeon-1.0.8.tar.gz.

File metadata

  • Download URL: typepigeon-1.0.8.tar.gz
  • Upload date:
  • Size: 6.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.63.0 importlib-metadata/4.11.2 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.10

File hashes

Hashes for typepigeon-1.0.8.tar.gz
Algorithm Hash digest
SHA256 6cff2980aa11a6db5599f5e1c960f735082ee67362f39e10e775bb945472c376
MD5 f01aba5b1d334daabce0271543a2378d
BLAKE2b-256 fd784b33df84fda9dfe8aee514a7b2ccbec07a84777559deadd26bbca34244d2

See more details on using hashes here.

File details

Details for the file typepigeon-1.0.8-py3-none-any.whl.

File metadata

  • Download URL: typepigeon-1.0.8-py3-none-any.whl
  • Upload date:
  • Size: 6.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.63.0 importlib-metadata/4.11.2 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.10

File hashes

Hashes for typepigeon-1.0.8-py3-none-any.whl
Algorithm Hash digest
SHA256 116542338946dcd4abb9fbaf8d91860d365ba24cd25417b8fde5441a9ef0fa70
MD5 97884837bc034d040d4f39e4c4e869a6
BLAKE2b-256 5499c472195faca237a1710fa22406d0a9f698e7b64f5eeeabe6a96b5ec805d8

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page