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 details)
Built Distributions
datatyping-0.5.1-py3.6.egg
(8.9 kB
view details)
File details
Details for the file datatyping-0.5.1.tar.gz
.
File metadata
- Download URL: datatyping-0.5.1.tar.gz
- Upload date:
- Size: 4.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | dad9924739946c1fd41f044ca993f294272f5b2abfea0ec826de297fe3c1ae1a |
|
MD5 | fadf7312b9fc3652a16a0e41e814cc65 |
|
BLAKE2b-256 | 7b966b31029e7026fdb0f7d18ddebf6f674ce047bc92443d8d2b5c0b6ac0134b |
File details
Details for the file datatyping-0.5.1-py3.6.egg
.
File metadata
- Download URL: datatyping-0.5.1-py3.6.egg
- Upload date:
- Size: 8.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1c9cc443c036c0134d64d7ae09490c23227539ef26aaef42b78370cc801e413a |
|
MD5 | 77eeaab0263beaadc11449459a252103 |
|
BLAKE2b-256 | ada570e9de78dafd23bd447020a1a927a0004e0f18dd0536f09c855da6a744c6 |
File details
Details for the file datatyping-0.5.1-py3-none-any.whl
.
File metadata
- Download URL: datatyping-0.5.1-py3-none-any.whl
- Upload date:
- Size: 7.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9ec71162c573cf0b3f25fcb736d07f7e5e55c865175627dbb4fdebc44a45cf1d |
|
MD5 | 6b35814a30ace9ef17b57c2af608eef6 |
|
BLAKE2b-256 | 51ddb96230a6950437e17a84807ba4410936be65df398befcb2b99186c8f2b8c |