Take a look at the test routine for for my CreateDict class (just happens to be a resturant application):
First add this to top of testit():
Note:The test routine, was just that. The concept is good, byt to really be useful, there should be methods for sdding a resturant, rather than using inline code.
import os class CreateDict: """ Generic Software tools used by Trailmapper. CreateDict.py - Contains methods to simplify node and cell creation within a dictionary Usage: The best way to learn what can be done is to examine the testit function included in this module. new_dict(dictname) - Creates a new dictionary instance with the name contained in dictname add_node(parent, nodename) - Creates a new node (nested dictionary) named in nodename, in parent dictionary. add_cell(nodename, cellname, value) - Creates a leaf node within node named in nodename, with a cell name of cellname, and value of value. display_dict(dictname) - Recursively displays a nested dictionary. Requirements: Trailmapper software: None Python standard library: os Author: Larz60+ -- May 2019. """ def __init__(self): os.chdir(os.path.abspath(os.path.dirname(__file__))) def new_dict(self, dictname): setattr(self, dictname, {}) def add_node(self, parent, nodename): node = parent[nodename] = {} return node def add_cell(self, nodename, cellname, value): cell = nodename[cellname] = value return cell def display_dict(self, dictname, level=0): indent = " " * (4 * level) for key, value in dictname.items(): if isinstance(value, dict): print(f'\n{indent}{key}') level += 1 self.display_dict(value, level) else: print(f'{indent}{key}: {value}') if level > 0: level -= 1 def testit(): # instantiate class cd = CreateDict() # create new dictionary named CityList cd.new_dict('CityList') # add node Boston boston = cd.add_node(cd.CityList, 'Boston') # add sub node Resturants bos_resturants = cd.add_node(boston, 'Resturants') # Add subnode 'Spoke Wine Bar' to parent bos_resturants spoke = cd.add_node(bos_resturants, 'Spoke Wine Bar') cd.add_cell(spoke, 'Addr1', '89 Holland St') cd.add_cell(spoke, 'City', 'Sommerville') cd.add_cell(spoke, 'Addr1', '02144') cd.add_cell(spoke, 'Phone', '617-718-9463') # Add subnode 'Highland Kitchen' to parent bos_resturants highland = cd.add_node(bos_resturants, 'Highland Kitchen') cd.add_cell(highland, 'Addr1', '150 Highland Ave') cd.add_cell(highland, 'City', 'Sommerville') cd.add_cell(highland, 'ZipCode', '02144') cd.add_cell(highland, 'Phone', '617-625-1131') # display dictionary print(f'\nCityList Dictionary') cd.display_dict(cd.CityList) print(f'\nraw data: {cd.CityList}') if __name__ == '__main__': testit()Here's what you get from the restaurant display,
Output:CityList Dictionary
Boston
Resturants
Spoke Wine Bar
Addr1: 02144
City: Sommerville
Phone: 617-718-9463
Highland Kitchen
Addr1: 150 Highland Ave
City: Sommerville
ZipCode: 02144
Phone: 617-625-1131
raw data: {'Boston': {'Resturants': {'Spoke Wine Bar': {'Addr1': '02144', 'City': 'Sommerville', 'Phone': '617-718-9463'}, 'Highland Kitchen': {'Addr1': '150 Highland Ave', 'City': 'Sommerville', 'ZipCode': '02144', 'Phone': '617-625-1131'}}}}
note all of the restauant data is saved in the dictionary and can be saved to a json file by adding (after all current code in testit):First add this to top of testit():
import jsonthen this to bottom of testit()
with open('myjsonfilename', 'w') as fp: json.dump(CityList, fp)The json part is untested but I think it's correct.
Note:The test routine, was just that. The concept is good, byt to really be useful, there should be methods for sdding a resturant, rather than using inline code.