{"id":15969020,"url":"https://github.com/dylanwal/dictpy","last_synced_at":"2025-03-17T20:30:57.117Z","repository":{"id":40770189,"uuid":"413194897","full_name":"dylanwal/dictpy","owner":"dylanwal","description":"Advanced tools for Python dictionaries. Including search and serialization tools.","archived":false,"fork":false,"pushed_at":"2022-06-27T18:55:01.000Z","size":108,"stargazers_count":4,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-02-28T03:41:55.640Z","etag":null,"topics":["json","python","search"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-3-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/dylanwal.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.txt","contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2021-10-03T20:54:16.000Z","updated_at":"2024-01-25T02:44:09.000Z","dependencies_parsed_at":"2022-09-07T14:50:38.471Z","dependency_job_id":null,"html_url":"https://github.com/dylanwal/dictpy","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dylanwal%2Fdictpy","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dylanwal%2Fdictpy/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dylanwal%2Fdictpy/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dylanwal%2Fdictpy/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/dylanwal","download_url":"https://codeload.github.com/dylanwal/dictpy/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243878490,"owners_count":20362433,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["json","python","search"],"created_at":"2024-10-07T19:20:41.325Z","updated_at":"2025-03-17T20:30:56.807Z","avatar_url":"https://github.com/dylanwal.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# dictpy (Dictionary Python)\n\n![PyPI](https://img.shields.io/pypi/v/dictpy)\n![tests](https://raw.githubusercontent.com/dylanwal/dictpy/master/tests/badges/tests-badge.svg)\n![coverage](https://raw.githubusercontent.com/dylanwal/dictpy/master/tests/badges/coverage-badge.svg)\n![flake8](https://raw.githubusercontent.com/dylanwal/dictpy/master/tests/badges/flake8-badge.svg)\n![downloads](https://static.pepy.tech/badge/dictpy)\n![license](https://img.shields.io/github/license/dylanwal/dictpy)\n\nAdvanced tools for Python dictionaries.\n\nIncluded Tools:\n\n* `DictSearch`: Search large and complex Python dictionaries/JSON files.\n* `Serializer`: Make custom JSON serializable Python classes serializable (make safe for conversion to JSON).\n\n## Installation\n\nPip installable package available.\n\n`pip install dictpy`\n\n\n---\n\n---\n\n## Searching (DictSearch)\n\nImagine you have some big ugly Python dictionary (like the one produced by [PubChem](https://pubchem.ncbi.nlm.nih.gov/)\nwhen you download the JSON file\nfor [CID 6](https://pubchem.ncbi.nlm.nih.gov/rest/pug_view/data/compound/6/JSON/?response_type=display)) and you want to\nextract some specific piece of information. This section will show how `DictSearch` can make this easy.\n\nTo perform the search we can pass the Python dictionary, and a search target (more discussion below on this) to\n`DictSearch`. It will find **all** valid objects for the search. The results of the search will be stored in `.result`.\n\n```python\nimport dictpy\n\nsearch = dictpy.DictSearch(data=json_data, target=target)\nprint(search.result)\n\n```\n\nThe return object is a `list[list[tree, obj]]`\n\n* `tree`: shows the navigation to get to the data ('.' separated)\n    * Keys are recorded for dictionaries\n    * Integer are recorded for position in lists\n    * Example: `Record.Section.1.Description`\n        ```python\n        {\"Record\": {\n            \"Section\": [\n                ######,\n                {\"Description\": #####}  # A match to the search!\n            ]\n        }}\n        ```\n* `obj` return the object\n    * **Options**:\n        * Return current object (default)\n            * Returns the object you search for\n            * Example:\n                * search: `{\"dog\": \"*\"}`; returns: `{\"dog\": \"golden retriever\"}`\n                * search: `\"dog\"`; returns: `{\"dog\": \"golden retriever\"}`\n                * search: `{\"dog\": \"golden retriever\"}`; returns: `{\"dog\": \"golden retriever\"}`\n        * Return parent object\n            * Returns parent object or whole current level\n            * To switch to returning parent objects, change `return_func`.\n              ```pyhton\n              search = dictpy.DictSearch(data=json_data, target=target, return_func=dictpy.DictSearch.return_parent_object)\n              ```\n            * Example\n                * search: `{\"dog\": \"*\"}`; returns:\n                  ```python\n                  {\n                  \"dog\": \"golden retriever\", \n                  \"cat\": \"bangel\", \n                  \"fish\": \"goldfish\"\n                  }\n                  ```\n                    * search: `\"dog\"`; returns:\n                  ```python\n                  {\n                  \"dog\": \"golden retriever\", \n                  \"cat\": \"bangel\", \n                  \"fish\": \"goldfish\"\n                  }\n                  ```\n\n### How to format `target`\n\nTarget can take match accept `strings`, `int`, `floats`, single line `dictionaries`, and `regex` (regular expression).\nWild cards(`*`) can also be used for partial dictionary searches.\n\nExample Targets:\n* `{\"RecordType\": \"CID\"}`\n    * Will match exactly to both 'key', and 'value' (won't match to list entries)\n* `{\"RecordNumber\": 6}`\n    * Will match exactly to both 'key', and 'value' (won't match to list entries)\n    * With numbers, the default search behavior auto-coverts strings to number. \n        * So this would hit to {\"RecordNumber\": \"6\"}\n        * To change this behavior set `op_convert_str_to_num=False`\n* `2526`\n    * Will look for 2526 in either 'key', 'value' or list entry.\n* `3D Conformer`\n    * Will look for \"3D Conformer\" in either 'key', 'value' or list entry.\n* `{\"MoveToTop\": \"*\"}`\n    * Will look for \"MoveToTop\" as a dictionary 'key' and the 'value' can be anything. (won't match to list entries)\n* `{\"*\": \"Chemical Safety\"}`\n    * Will look for \"Chemical Safety\" as a dictionary 'value' and the 'key' can be anything. (won't match to list entries)\n* `\"^[A-I]{3}$\"`\n    * Regular expression search will match in either 'key', 'value' or list entry.\n* `{\"^RecordT\": \"*\"}`\n    * Regular expression search will match for 'key' and 'value' can be anything. (won't match to list entries)\n    \nFor more examples see \n[tests/test_dict_search.py](https://github.com/dylanwal/dictpy/blob/master/tests/test_dict_search.py).\n\n\n\n### Example\n\nThis example will extract data from a JSON for \"1-Chloro-2,4-dinitrobenzene\" download from\n[PubChem](https://pubchem.ncbi.nlm.nih.gov/).\n\n[Example JSON File](https://github.com/dylanwal/dictpy/blob/master/tests/cid_6.json)\n\nFirst, we will load our example above (change \"/path/to/data/\" to your file location for the file above):\n\n```python\nimport json\n\nwith open(\"C:/path/to/data/cid_6.json\", \"r\") as f:\n    text = f.read()\n    json_data = json.loads(text)\n\nprint(json_data)\n```\n\nYou will get a massive printout of the 12,000 line JSON file.\n\n```python\nimport dictpy\n\nsearch = dictpy.DictSearch(data=json_data, target={\"RecordType\": \"CID\"})\nprint(search.result)\n```\nPrint out:\n```python\n[['Record.RecordType', {'RecordType': 'CID'}]]\n```\n\nInteger search target:\n```python\nsearch = dictpy.DictSearch(data=json_data, target=2526)\nprint(search.result)\n```\nPrint out:\n```python\n[\n    ['Record.Section.3.Section.1.Section.14.Information.1.Value.Number', 2526],\n    ['Record.Section.3.Section.1.Section.14.Information.1.Value.Number', 2526]\n]\n```\n\n---\n\n---\n\n## Serialization (Serializer)\n\n`Serializer` is useful for turning custom python classes into JSON compatible dictionaries.\n\nThis serialization class is a useful pre-process step for complex custom python class that contain non-JSON serializable\nsafe objects (Example: datatime objects, custom classes, any classes from other packages, ObjectIDs, etc.)\n\nInherit `Serializer` in to your custom python class.\n\n```python\nimport json\nimport datetime\n\nimport dictpy\n\nclass Example(dictpy.Serializer):\n\n    def __init__(self, datetime_obj, stuff2):\n        self.datetime_obj = datetime_obj  # NOT JSON serializable object\n        self.stuff2 = stuff2\n        self.stuff3 = None \n\n\nexample = Example(datetime.time(), \"stuff2\")\n\n# json_output = json.dumps(example)  # This will fail with NOT JSON serializable objects\n\ndict_of_example = example.as_dict()\ndict_of_example = dictpy.Serializer.dict_cleanup(dict_of_example)  # converts NOT JSON serializable objects to strings. \ndict_of_example = dictpy.Serializer.remove_none(dict_of_example)  # Optional: remove None; self.stuff3 removed\n\njson_output = json.dumps(dict_of_example)\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdylanwal%2Fdictpy","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdylanwal%2Fdictpy","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdylanwal%2Fdictpy/lists"}