Easily and safety cast objects to the desired data type
Project description
What's the good of that?
- Cast the string representation of simple objects to the desired type.
- 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 hashes)
Built Distribution
Close
Hashes for try_parse-0.0.9-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 64a29d492339c9e7f1f2f1492179560dd9b17f2f528a585c2284e893055ee186 |
|
MD5 | 976b5c6c8222de0b7fc388563dd1bd61 |
|
BLAKE2b-256 | 9754104d3f9d7481bcfca199d54f39781ac7d37050e40b081397a1e1b0182f51 |