Library to explicitly test all the fields of a python dictionary, even when you don't know all of their values.
From PyPi:
$ pip install dsert
From GitHub:
$ pip install git+https://github.com/paxos-bankchain/dsert.git
>>> from dsert import assert_valid_dict
>>> my_dict = {'balance': 123.45, 'status': 'homeowner', 'good_credit': True}
Check all the fields (will return None
):
>>> assert_valid_dict(my_dict, known_contents={'balance': 123.45}, known_types={'status': str, 'good_credit': bool})
>>>
Check some of the fields (will raise an Exception
with helpful debug instructions):
>>> assert_valid_dict(my_dict, known_contents={'balance': 123.45})
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "dsert/__init__.py", line 35, in assert_valid_dict
raise KeyError(err_msg)
KeyError: "Keys for {'good_credit': True, 'status': 'homeowner'} not in known_contents keys (['balance']), known_types keys ([]), nor excluded_fields ([])."
Most tests are opt-in, where we test certain keys/values only:
>>> self.assertEqual(some_dict['a'], 1)
This can work well, but it can also cause situations where the tests pass and yet a bug has slipped in!
From The Zen of Python
Explicit is better than implicit.
More complex validators. Don't just test that a dictionary value is of type int
, test that it's a positive/even/prime int
.
Check out repo:
$ git checkout git+https://github.com/paxos-bankchain/dsert.git && cd dsert
Install locally
$ pip install --editable .
Confirm tests pass:
$ nosetests .
(this requires having nose installed)
Make your changes and confirm that tests still pass:
$ nosetests .
You must have the credentials in order to push updates to PyPi.
Create a .pypirc
file in your home directory:
$ cat ~/.pypirc
[distutils]
index-servers=
pypi
[pypi]
repository = https://pypi.python.org/pypi
username = paxos
password = <password goes here>
Install twine:
$ pip install twine
Create a distribution:
$ python setup.py sdist bdist_wheel
Push your distribution to PyPi:
$ twine upload dist/* -r pypi
To test this process, you can use PyPi's test server. Add an entry to .pypirc
that looks like this with whatever creds you create for testpypi:
[testpypi]
repository = https://testpypi.python.org/pypi
username = <your user name goes here>
password = <your password goes here>
Then use the following command to push your distrobution to test PyPi:
$ twine upload dist/* -r testpypi