Pythonic type checking
Project description
# datatyping
datatyping lets you define a data structure of types and then verify that your
data is well-formed!
``` python
>>> import datatyping
>>> structure = {
... 'id': int,
... 'cars': [{'model': str, 'passengers': int}],
... }
>>> data = {
... 'id': 215,
... 'cars': [
... {'model': 'Cadillac', 'passengers': 2},
... {'model': 'Volvo', 'passengers': 4},
... ]
... }
>>> datatyping.validate(structure, data)
```
## Install
Latest release:
`pip install datatyping`
Development version:
`pip install git+https://github.com/Zaab1t/datatyping`
## Benefits
- Documentation of incoming data in source code.
- Good for testing (especially if you offer something like a json api).
- Early failure in a specific spot if data is malformed.
- Readable, explicit code base helps maintainability.
## Features
### Basics
``` python
>>> from datatyping import validate
>>> # Working with lists
>>> validate([int, str], [1, 'a'])
>>> validate([dict], [{'can have': 1}, {'any keys': 2}])
>>> validate([[int], [str]], [[1, 2, 3], ['a', 'b', 'c'])
>>>
>>> # Working with dicts
>>> validate({'a': int, 'b': str}, {'a': 4, 'b': 'c'})
>>> validate({'a': int}, {'a': 2, 'b': 'oops'})
KeyError: {'b'}
>>> validate({'a': int}, {'a': 2, 'b': 'yay'}, strict=False)
```
### Custom types
``` python
>>> from datatyping import validate, customtype
>>> @customtype
>>> def positive_int(i):
... if i < 1:
... raise TypeError('%d is not positive' % i)
>>> validate([positive_int], [1, 2, 3, 4])
>>> validate([positive_int], [1, 2, 3, -4])
TypeError: -4 is not positive
```
### Structure generation
Since typing out the structure can be quite tedious, `datatyping` provides a tool that does most of the work for you:
``` python
>>> from datatyping.printer import pprint
>>> import requests
>>> r = requests.get('http://httpbin.org/anything')
>>> pprint(r.json())
{
'args': dict,
'data': str,
'files': dict,
'form': dict,
'headers': {
'Accept': str,
'Accept-Encoding': str,
'Connection': str,
'Host': str,
'User-Agent': str,
},
'json': NoneType,
'method': str,
'origin': str,
'url': str,
}
```
## Testimonials
> does the data good
>> -- [theelous3](https://github.com/theelous3)
## Develop with me :)
``` bash
# fork it
$ git clone https://github.com/your_name/datatyping
$ cd datatyping
$ sudo python3 setup.py develop
# make your changes, commit and push
$ python3 -m pytest
# submit a pull request
```
## Notes
- Inspired by ["How Python Makes Working With Data More Difficult in the Long Run"](https://jeffknupp.com/blog/2016/11/13/how-python-makes-working-with-data-more-difficult-in-the-long-run/).
- Any and all contributions are welcome.
- Please open an issue if there's anything you can't make work (read: I messed up).
- Please let me know if there is an unsupported data structure, you'd like to see support for.
- If you enjoy and use the software, you can [say thanks](https://saythanks.io/to/Zaab1t) :)
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
datatyping-0.5.1.tar.gz
(4.5 kB
view hashes)
Built Distributions
datatyping-0.5.1-py3.6.egg
(8.9 kB
view hashes)
Close
Hashes for datatyping-0.5.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9ec71162c573cf0b3f25fcb736d07f7e5e55c865175627dbb4fdebc44a45cf1d |
|
MD5 | 6b35814a30ace9ef17b57c2af608eef6 |
|
BLAKE2b-256 | 51ddb96230a6950437e17a84807ba4410936be65df398befcb2b99186c8f2b8c |