Datacast is a Python package that validates and converts your data.
Basic Usage
Install with pip:
pip install datacast
Define schema (can be any class with annotations) and use cast function.
from datacast import cast
class SimpleSchema:
one: int
two: str
three: (lambda x: x ** 2)
zero: (int, bool)
four: float = 0.4
five: None = 'five'
cast({'one': 1, 'two': 2, 'three': 3, 'zero': '0', 'five': 5}, SimpleSchema)
# {'one': 1, 'two': '2', 'three': 9, 'zero': False, 'four': 0.4, 'five': 5}
Rules are simple:
Params without annotations will be ignored.
Annotation is a caster, which will be called with the provided value, eg. bool(0).
Caster is any callable. Functions, lambdas, classes etc.
It also can be list or tuple (or another iterable). Then it acts like a chain of casters, eg. int('0') -> bool(0) -> False.
If there is no default value - param is required and will raise RequiredFieldError if not provided.
None in annotation means no casting.
Config
You can use Config class which acts like a schema AND stores result data.
from datacast import Config
class SimpleConfig(Config):
spam: bool
ham: None
rabbit: float = None
config = SimpleConfig({'spam': 0, 'ham': 1})
assert config.spam == False
assert config.ham == 1
assert config.rabbit == None
assert config._asdict() == {'spam': False, 'ham': 1, 'rabbit': None}
Also there is EnvironConfig which loads input data from environment, casts strings to appropriate types and ignores extra vars.
from datacast import EnvironConfig
class SimpleEnvironConfig(EnvironConfig):
SPAM: bool
HAM: int
RABBIT: str
NONE_VAL: None
os.environ['SPAM'] = '0'
os.environ['HAM'] = '1'
os.environ['RABBIT'] = '2'
os.environ['NONE_VAL'] = 'null'
config = SimpleEnvironConfig()
assert config.SPAM == False
assert config.HAM == 1
assert config.RABBIT == '2'
assert config.NONE_VAL == None
- Valid None strings:
'none', 'null', 'nil'
- Valid True strings:
'true', 't', 'yes', 'y', 'on', '1'
- Valid False strings:
'false', 'f', 'no', 'n', 'off', '0', ''
Case doesn’t matter.
Settings
You can specify various settings and apply them in a bunch of different ways.
from datacast import apply_settings, Settings
@apply_settings(
on_missing='store',
missing_value=False
)
class SimpleSchema:
...
# OR
class SimpleSettings(Settings):
on_missing = 'store'
missing_value = False
@apply_settings(SimpleSettings)
class SimpleSchema:
...
# OR pass it to the cast function or Config creation
cast(input_data, SimpleSchema, settings=SimpleSettings)
cast(input_data, SimpleSchema, on_missing='store', missing_value=False)
Config(input_data, settings=SimpleSettings)
Config(input_data, on_missing='store', missing_value=False)
# OR use class attribute
class SimpleSchema:
__settings__ = SimpleSettings
# OR
__settings__ = {'on_missing': 'store', 'missing_value': False}
...
List of settings
Name |
Default |
Description |
|---|---|---|
on_extra |
'ignore' |
What to do with values that absent from schema. |
on_invalid |
'raise' |
What to do when casting has failed. |
on_missing |
'raise' |
What to do when value is missing but required. |
missing_value |
None |
What to store when value is missing. |
store_callables |
False |
If False - execute callable value on store. |
result_class |
dict |
Class which stores result data. |
precasters |
() |
Prepend additional casters. |
postcasters |
() |
Append additional casters. |
cast_defaults |
False |
Cast default values with full casters chain. |
raise_original |
False |
Raise original exception instead of CastError. |
Options for ‘on_extra’, ‘on_invalid’ and ‘on_missing’
- ignore:
Value will be ignored and not be stored in the result.
- store:
Value will be stored in the result as is. In case of on_missing it will store missing_value.
- raise:
Corresponding exception will be raised.
- cast:
Value will be casted with precasters, postcasters and then stored. Works only with on_extra!
With precasters and postcasters you will transform every caster in schema into a chain, which starts and/or ends with those casters.
Release files for datacast 0.3.5
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| datacast-0.3.5.tar.gz | 8.2 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| datacast-0.3.5-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 21.5 kB
Release files / datacast-0.3.5.tar.gz
| Download URL | datacast-0.3.5.tar.gz |
|---|---|
| Size | 8.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
dc2ef6322e495bfebd8b1042e69fabf629154c816344ba0dc2495a6ec77b7f70
|
|
BLAKE2b-256 checksum How to use checksums |
c0ecc337e1c6473781cc128392094b45cad77257a6f1c072ea0c073250a7f2f0
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/1.12.1 pkginfo/1.4.2 requests/2.19.1 setuptools/40.4.3 requests-toolbelt/0.8.0 tqdm/4.27.0 CPython/3.7.0
|
Release files / datacast-0.3.5-py3-none-any.whl
| Download URL | datacast-0.3.5-py3-none-any.whl |
|---|---|
| Size | 13.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
34a4d28136857375e045a2dc8161affe7cd4e3f2260128aa7df8ce439420f475
|
|
BLAKE2b-256 checksum How to use checksums |
09ee70dc7c2ea54878bc7798e9cc43553bd7c33a4ffaf3c7845e242702acc66c
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/1.12.1 pkginfo/1.4.2 requests/2.19.1 setuptools/40.4.3 requests-toolbelt/0.8.0 tqdm/4.27.0 CPython/3.7.0
|