Safe casting of Python base types
Project description
safe-cast
Safe casting of Python base types.
Badges
info |
|
---|---|
tests |
|
package |
Functions
Function |
Purpose |
---|---|
safe_cast(val, to_type, default=None) |
Cast value to requested type, if failed, then used default. |
safe_str(val, default=None) |
Cast value to type string, if failed, then used default. |
safe_float(val, ndigits=2, default=None) |
Cast value to type float, if failed, then used default. |
safe_int(val, default=None) |
Cast value to type int, if failed, then used default. |
safe_dict(val, default=None) |
Cast value to type dictionary, if failed, then used default. |
safe_smart_cast(val) |
Determine type based upon value, and cast to that type. |
safe_cost(val) |
Cast value to type float by 4 decimal points. |
Usage
safe_cast(val, to_type, default=None)
Safely cast a value to type, and if failed, returned default if exists.
Optional: Pass default value. Returned if casting fails.
- param val:
Value to be cast.
- param to_type:
Safely cast to a specific type.
- param default:
Default if casting fails.
- return:
Return casted value or default.
safe_int(val, default=None)
Safely cast a value to an integer.
Optional: Pass default value. Returned if casting fails.
- param val:
Value to be cast to int.
- param default:
Default if casting fails.
- return:
Return int casted value or default.
safe_float(val, ndigits=2, default=None)
Safely cast a value to float, remove ‘,’ if exists to ensure strings “1,234.5” are transformed to become “1234.5”.
Optional: Pass default value. Returned if casting fails.
- param val:
Value to be cast to float.
- param ndigits:
Number of digits in float.
- param default:
Default if casting fails.
- return:
Return float casted value or default.
safe_str(val, default=None)
Safely cast a value to a string.
Optional: Pass default value. Returned if casting fails.
- param val:
Value to be cast to string.
- param default:
Default if casting fails.
- return:
Return string casted value or default.
safe_dict(val, default=None)
Safely cast a value to a dictionary.
Optional: Pass default value. Returned if casting fails.
- param val:
Value to be cast to dictionary.
- param default:
Default if casting fails.
- return:
Return dictionary casted value or default.
safe_smart_cast(val)
Safely cast a value to the best matching type. Optional: Pass default value. Returned if casting fails.
- param val:
Value to be smartly cast.
- return:
Typed value
Release History
0.2.0 (2017-12-05)
README
0.1.3 (2017-10-31)
Added comments.
0.1.2 (2017-10-19)
Update requirements.txt
0.1.1 (2017-03-15)
Handle empty string -> int/float/dict casting
Added relevant testing
0.1.0 (2017-03-14)
Production ready
Cleanup
0.0.9 (2017-03-14)
Handle None input
0.0.8 (2017-03-12)
Rename package to ‘safe-cast’
0.0.7 (2017-03-09)
Renamed inner package folder to underscore: ‘smart_cast. To comply with Python standard.
Updated casting logic
Updated testing
Activate Continues deployment and testing with Travis.CI
Activate coverage testing: Coveralls.io
0.0.6 (2017-03-08)
Renamed from ‘safecast’ to ‘smart-cast’
0.0.1 (2017-03-07)
Conception
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distributions
File details
Details for the file safe-cast-0.2.2.tar.gz
.
File metadata
- Download URL: safe-cast-0.2.2.tar.gz
- Upload date:
- Size: 8.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | f74504d268c54bfd19dc0f7c5bfd1ea7b0146899bac4d12547d8af59a2d612e0 |
|
MD5 | 0dd28b72778a83432c9ec258ea4c3836 |
|
BLAKE2b-256 | bc36d33a118e491472993f2404147d89b53375a7ebb64f9a698c30ce293a1aad |
File details
Details for the file safe_cast-0.2.2-py3.6.egg
.
File metadata
- Download URL: safe_cast-0.2.2-py3.6.egg
- Upload date:
- Size: 5.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | e2e10f8d5a2da93dee71e46c0af848102359bf4713a784f499239676a50b540b |
|
MD5 | 902d2c2ebd25c801d62cafe473b48f3d |
|
BLAKE2b-256 | 06e2d1e39afab10a91030b49ebd754aecc1da5346fd6aeb280d4ab5f66e9025c |
File details
Details for the file safe_cast-0.2.2-py3-none-any.whl
.
File metadata
- Download URL: safe_cast-0.2.2-py3-none-any.whl
- Upload date:
- Size: 6.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 31467614e3cf9f1a0aee740afa7465eff7699505d58bbcb7b51ef84544613778 |
|
MD5 | 56f4e5305c407304af9c7204fa46b982 |
|
BLAKE2b-256 | f55b027d06adc36bb38296ce47f1ef09a7acbe2da27b4545d935465e7d7e231e |