ATTENTION: Dictor version 0.1.13 and up will be dropping support for Python 2
Dictor is a Python JSON and Dictionary (Hash, Map) handler.
Dictor takes a dictionary or JSON data and returns value for a specific key.
If Dictor doesnt find a value for a key, or if JSON or Dictionary data is missing the key, the return value is either None or whatever fallback value you provide.
Dictor is polite with Exception errors commonly encountered when parsing large Dictionaries/JSONs.
Using Dictor eliminates the repeated use of try/except blocks in your code when dealing with lookups of large JSON structures, as well as providing flexibility for inserting fallback values on missing keys/values.
Why not use dict.get("value") ?
using the built-in dict.get() does not parse the full body of a dict.
This method works if parsing a simple key=value structure, for example:
data = {"name": "Joe"}
>>> print(data.get("name"))
Joe
>>> print(data.get("age"))
None
>>> print(data.get("age", "this key doesnt exist"))
this key doesnt exist
But this wont work if the dict is a list,
>>> data = [{"name": "Joe"}]
>>> print(data.get("age"))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'list' object has no attribute 'get'
or if the dict has a complex and nested structure, for example if I want to get Joe's age, I need to create a for-loop to parse the hierarchial levels of the dict structure until I reach the "age" key:
you can provide a default fallback value either by passing
default="fallback value" or just placing a fallback string,
print(dictor(data, 'characters.Princess Leia', default='Not in Spaceballs'))
>> Not in spaceballs
or just add a fallback string,
print(dictor(data, 'characters.Princess Leia', 'fallback to this'))
>> fallback to this
if you want to error out on a None value, simply provide a CheckNone flag, a ValueError will be raised.
print(dictor(data, 'characters.Princess Leia', checknone=True))
Traceback (most recent call last):
File "test.py", line 14, in <module>
print(dictor(data, 'characters.Princess Leia', checknone=True))
File "/github.com/dictor/dictor/__init__.py", line 77, in dictor
raise ValueError('missing value for %s' % path)
ValueError: value not found for search path: "characters.Princess Leia"
Passing a variable into search path
if you need to pass a variable into search path
who = "Barfolomew"
print(dictor(data, "characters.{}.id".format(who)))
>> 55924
if using Python 3, you can also use F-strings
who = "Barfolomew"
print(dictor(data, f"characters.{who}.id"))
dictor will parse each lookup element hierarchicly, starting with top and will work down to the last element, reading in each dot-separated list index.
searching for dictor(data, 'dirty.harry') will return a None since Dictor sees the dot-separated entry as 2 separate keys.
To search for a key with a dot in the name, simply use a Path Separator flag, this allows you to control the separator of keys by using a custom character. (by default, pathsep is set to '.')
This will only return the desired output type if return value is string or int. If the return value is a dictionary, list or tuple, the original return value will be returned.
Testing
testing is done using Pytest. Tests are located in 'tests' directory.
pip3 install pytest
shell> cd tests
shell> pytest -sv test.py
Release Notes
0.1.12
removed Nose as testing lib as its unmaintained, added pytest