Package: dictpy
dictpy (Dictionary Python)Advanced tools for Python dictionaries.Included Tools:DictSearch: Search large and complex Python dictionaries/JSON files.Serializer: Make custom JSON serializable Python classes serializable (make safe for conversion to JSON).InstallationPip installable package available.pip install dictpySearching (DictSearch)Imagine you have some big ugly Python dictionary (like the one produced byPubChemwhen you download the JSON file
forCID 6) and you want to
extract some specific piece of information. This section will show howDictSearchcan make this easy.To perform the search we can pass the Python dictionary, and a search target (more discussion below on this) toDictSearch. It will findallvalid objects for the search. The results of the search will be stored in.result.importdictpysearch=dictpy.DictSearch(data=json_data,target=target)print(search.result)The return object is alist[list[tree, obj]]tree: shows the navigation to get to the data ('.' separated)Keys are recorded for dictionariesInteger are recorded for position in listsExample:Record.Section.1.Description{"Record":{"Section":[######,{"Description":#####}  # A match to the search!]}}objreturn the objectOptions:Return current object (default)Returns the object you search forExample:search:{"dog": "*"}; returns:{"dog": "golden retriever"}search:"dog"; returns:{"dog": "golden retriever"}search:{"dog": "golden retriever"}; returns:{"dog": "golden retriever"}Return parent objectReturns parent object or whole current levelTo switch to returning parent objects, changereturn_func.search = dictpy.DictSearch(data=json_data, target=target, return_func=dictpy.DictSearch.return_parent_object)Examplesearch:{"dog": "*"}; returns:{"dog":"golden retriever","cat":"bangel","fish":"goldfish"}search:"dog"; returns:{"dog":"golden retriever","cat":"bangel","fish":"goldfish"}How to formattargetTarget can take match acceptstrings,int,floats, single linedictionaries, andregex(regular expression).
Wild cards(*) can also be used for partial dictionary searches.Example Targets:{"RecordType": "CID"}Will match exactly to both 'key', and 'value' (won't match to list entries){"RecordNumber": 6}Will match exactly to both 'key', and 'value' (won't match to list entries)With numbers, the default search behavior auto-coverts strings to number.So this would hit to {"RecordNumber": "6"}To change this behavior setop_convert_str_to_num=False2526Will look for 2526 in either 'key', 'value' or list entry.3D ConformerWill look for "3D Conformer" in either 'key', 'value' or list entry.{"MoveToTop": "*"}Will look for "MoveToTop" as a dictionary 'key' and the 'value' can be anything. (won't match to list entries){"*": "Chemical Safety"}Will look for "Chemical Safety" as a dictionary 'value' and the 'key' can be anything. (won't match to list entries)"^[A-I]{3}$"Regular expression search will match in either 'key', 'value' or list entry.{"^RecordT": "*"}Regular expression search will match for 'key' and 'value' can be anything. (won't match to list entries)For more examples seetests/test_dict_search.py.ExampleThis example will extract data from a JSON for "1-Chloro-2,4-dinitrobenzene" download fromPubChem.Example JSON FileFirst, we will load our example above (change "/path/to/data/" to your file location for the file above):importjsonwithopen("C:/path/to/data/cid_6.json","r")asf:text=f.read()json_data=json.loads(text)print(json_data)You will get a massive printout of the 12,000 line JSON file.importdictpysearch=dictpy.DictSearch(data=json_data,target={"RecordType":"CID"})print(search.result)Print out:[['Record.RecordType',{'RecordType':'CID'}]]Integer search target:search=dictpy.DictSearch(data=json_data,target=2526)print(search.result)Print out:[['Record.Section.3.Section.1.Section.14.Information.1.Value.Number',2526],['Record.Section.3.Section.1.Section.14.Information.1.Value.Number',2526]]Serialization (Serializer)Serializeris useful for turning custom python classes into JSON compatible dictionaries.This serialization class is a useful pre-process step for complex custom python class that contain non-JSON serializable
safe objects (Example: datatime objects, custom classes, any classes from other packages, ObjectIDs, etc.)InheritSerializerin to your custom python class.importjsonimportdatetimeimportdictpyclassExample(dictpy.Serializer):def__init__(self,datetime_obj,stuff2):self.datetime_obj=datetime_obj# NOT JSON serializable objectself.stuff2=stuff2self.stuff3=Noneexample=Example(datetime.time(),"stuff2")# json_output = json.dumps(example)  # This will fail with NOT JSON serializable objectsdict_of_example=example.as_dict()dict_of_example=dictpy.Serializer.dict_cleanup(dict_of_example)# converts NOT JSON serializable objects to strings.dict_of_example=dictpy.Serializer.remove_none(dict_of_example)# Optional: remove None; self.stuff3 removedjson_output=json.dumps(dict_of_example)