Skip to main content

Easily and safety cast objects to the desired data type

Project description

What's the good of that?

PyPI version Supported Python versions License

  1. Cast the string representation of simple objects to the desired type.
  2. Safe type casting. The exception will not be thrown. The function returns status and result.

install

pip install try-parse

import

from try_parse.utils import ParseUtils

try_parse_date

Parse object to date

status, target = ParseUtils.try_parse_date('2018-11-23')
self.assertTrue(status)
self.assertIsInstance(target, date)
self.assertEqual(target, date(2018, 11, 23))

# See format https://docs.python.org/3/library/datetime.html#strftime-strptime-behavior
status, target = ParseUtils.try_parse_date('23.11.2018', format='%d.%m.%Y')
self.assertTrue(status)
self.assertIsInstance(target, date)
self.assertEqual(target, date(2018, 11, 23))

status, target = ParseUtils.try_parse_date('Invalid')
self.assertFalse(status)
self.assertIsNone(target)

try_parse_datetime

Parse object to datetime

status, target = ParseUtils.try_parse_datetime('2018-11-23 01:45:59')
self.assertTrue(status)
self.assertIsInstance(target, datetime)
self.assertEqual(target, datetime(2018, 11, 23, 1, 45, 59))

# See format https://docs.python.org/3/library/datetime.html#strftime-strptime-behavior
status, target = ParseUtils.try_parse_datetime('23.11.2018T01:45:59', format='%d.%m.%YT%H:%M:%S')
self.assertTrue(status)
self.assertIsInstance(target, datetime)
self.assertEqual(target, datetime(2018, 11, 23, 1, 45, 59))

status, target = ParseUtils.try_parse_datetime('Invalid')
self.assertFalse(status)
self.assertIsNone(target)

try_parse_int

Parse object to int

status, target = ParseUtils.try_parse_int('19')
self.assertTrue(status)
self.assertIsInstance(target, int)
self.assertEqual(target, 19)

status, target = ParseUtils.try_parse_int('Invalid')
self.assertFalse(status)
self.assertIsNone(target)

try_parse_float

Parse object to float

status, target = ParseUtils.try_parse_float('19.00')
self.assertTrue(status)
self.assertIsInstance(target, float)
self.assertEqual(target, 19.00)

status, target = ParseUtils.try_parse_float('Invalid')
self.assertFalse(status)
self.assertIsNone(target)

try_parse_decimal

Parse object to decimal

status, target = ParseUtils.try_parse_decimal('19.00')
self.assertTrue(status)
self.assertIsInstance(target, Decimal)
self.assertEqual(target, Decimal(19))

status, target = ParseUtils.try_parse_decimal('Invalid')
self.assertFalse(status)
self.assertIsNone(target)

try_parse_bool

Parse object to bool

for p in ["yes", "true", "t", "1", 1]:
    status, target = ParseUtils.try_parse_bool(p)
    self.assertTrue(status)
    self.assertIsInstance(target, bool)
    self.assertTrue(target)

status, target = ParseUtils.try_parse_bool('Invalid')
self.assertFalse(status)
self.assertIsNone(target)

Mypy

Mypy can be installed using pip:

pip install -U mypy

Testing

mypy ./try_parse/utils.py

Run unittest from console

python -m unittest discover -p "*_tests.py"

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

try_parse-0.0.9.tar.gz (3.1 kB view details)

Uploaded Source

Built Distribution

try_parse-0.0.9-py3-none-any.whl (4.5 kB view details)

Uploaded Python 3

File details

Details for the file try_parse-0.0.9.tar.gz.

File metadata

  • Download URL: try_parse-0.0.9.tar.gz
  • Upload date:
  • Size: 3.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.49.0 CPython/3.8.2

File hashes

Hashes for try_parse-0.0.9.tar.gz
Algorithm Hash digest
SHA256 9643cc2d392eab1a4abc5fe9ef0cd8094781730c779b7a5dacba1e0720af0e4a
MD5 5a8e416ad57fbded031f1346c5dee91d
BLAKE2b-256 89b90662b557c227e05b4bb4fb404ff55efd78e8cfb8aad7855982f78c74fbbc

See more details on using hashes here.

File details

Details for the file try_parse-0.0.9-py3-none-any.whl.

File metadata

  • Download URL: try_parse-0.0.9-py3-none-any.whl
  • Upload date:
  • Size: 4.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.49.0 CPython/3.8.2

File hashes

Hashes for try_parse-0.0.9-py3-none-any.whl
Algorithm Hash digest
SHA256 64a29d492339c9e7f1f2f1492179560dd9b17f2f528a585c2284e893055ee186
MD5 976b5c6c8222de0b7fc388563dd1bd61
BLAKE2b-256 9754104d3f9d7481bcfca199d54f39781ac7d37050e40b081397a1e1b0182f51

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