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)

Project details


Download files

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

Files for try-parse, version 0.0.8
Filename, size File type Python version Upload date Hashes
Filename, size try_parse-0.0.8-py3-none-any.whl (4.3 kB) File type Wheel Python version py3 Upload date Hashes View hashes
Filename, size try_parse-0.0.8.tar.gz (2.8 kB) File type Source Python version None Upload date Hashes View hashes

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN SignalFx SignalFx Supporter DigiCert DigiCert EV certificate StatusPage StatusPage Status page