Skip to main content

Cast unknown value to desired type with typing support.

Project description

cast-unknown-python

Action: Python package version python versions wheel

Cast unknown value to desired type with typing support.

Current supported cast target:

  • text (unicode for python2, str for python3)
  • binary (str for python2, bytes for python3)
  • iterable
  • datetime
  • one (one and the only one item from given iterable, otherwise None)
  • non_none (return default or raise error when given value is None)
  • instance (raise error when not is instance)
  • list (with optional item type check)
>>> import cast_unknown as cast
>>> import six
>>> cast.text('测试')
'测试'
>>> cast.text('测试'.encode('utf-8'))
'测试'
>>> cast.text(b'\xb2\xe2\xca\xd4', 'gbk')
'测试'
>>> cast.text(1)
'1'
>>> cast.text([])
'[]'
>>> cast.binary('测试')
b'\xe6\xb5\x8b\xe8\xaf\x95'
>>> cast.binary('测试'.encode('utf-8'))
b'\xe6\xb5\x8b\xe8\xaf\x95'
>>> cast.binary('测试', 'ascii')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "cast_unknown\binary.py", line 25, in binary
    return v.encode(encoding, errors)
UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-1: ordinal not in range(128)
>>> cast.binary('测试', 'gbk')
b'\xb2\xe2\xca\xd4'
>>> cast.binary([])
b'[]'
>>> cast.iterable(1)
[1]
>>> cast.iterable((i for i in (1,2,3)))
<generator object <genexpr> at 0x000000000220ECC8>
>>> cast.iterable([1])
[1]
>>> cast.iterable(None)
[]
>>> import datetime as dt
>>> cast.datetime(dt.datetime.now())
datetime.datetime(2021, 3, 10, 15, 27, 41, 69429)
>>> cast.datetime('2021-03-10T00:00:00+0800')
datetime.datetime(2021, 3, 10, 0, 0, tzinfo=tzoffset(None, 28800))
>>> cast.datetime('2021/03/10')
datetime.datetime(2021, 3, 10, 0, 0)
>>> cast.datetime(1615305600.0)
datetime.datetime(2021, 3, 9, 16, 0)
>>> cast.datetime(1615305600000)
datetime.datetime(2021, 3, 9, 16, 0)
>>> cast.one(1)
1
>>> cast.one(None)
None
>>> cast.one([1])
1
>>> cast.one([1,2])
None
>>> cast.not_none(1)
1
>>> cast.not_none(None)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "cast_unknown\not_none.py", line 14, in not_none
    raise CastError("value and default both None ")
cast_unknown.error.CastError: value and default both None
>>> cast.not_none(None, 1)
1
>>> cast.instance(1, int)
1
>>> cast.instance(1, str)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "cast_unknown\instance.py", line 23, in instance
    raise CastError(
cast_unknown.error.CastError: ('can not cast object to instance', 1, <class 'str'>)
>>> cast.instance(1, (str, int))
1
>>> cast.list_(None)
[]
>>> cast.list_([])
[]
>>> cast.list_(1)
[1]
>>> cast.list_([1])
[1]
>>> cast.list_([1], six.text_type)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "cast_unknown\list.py", line 14, in list_
    instance(i, class_or_tuple)
  File "cast_unknown\instance.py", line 23, in instance
    raise CastError(
cast_unknown.error.CastError: ('can not cast object to instance', 1, <class 'str'>)
>>> cast.list_([1], (int, six.text_type))
[1]
>>> cast.list_("abc")
['a', 'b', 'c']
>>> cast.list_("abc", six.text_type)
['abc']

related

Project details


Download files

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

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

cast_unknown-0.1.7-py2.py3-none-any.whl (10.9 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file cast_unknown-0.1.7-py2.py3-none-any.whl.

File metadata

  • Download URL: cast_unknown-0.1.7-py2.py3-none-any.whl
  • Upload date:
  • Size: 10.9 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/46.0.0 requests-toolbelt/0.8.0 tqdm/4.32.2 CPython/3.7.5

File hashes

Hashes for cast_unknown-0.1.7-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 7e3c3029ce288b610ac42070b7fddef04a68c4276b1b4543c3df8a68e24d6e26
MD5 ee7caeda5ff6a9af1f8aef6ab60267dd
BLAKE2b-256 31c20d4b9c3781f49cd3822e021f35a075de647a867f7ca0a0b2b226377035a0

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