Python Forum
Create Dynamic nested Dictionaries
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Create Dynamic nested Dictionaries
#1
I just posted this on thread: https://python-forum.io/Thread-how-to-cr...ested-dict

It's useful enough that I'm posting it here as well:
#  Author: Larz60+ Nov 22, 2018
import os


class CreateDict:
    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():
    cd = CreateDict()

    cd.new_dict('CityList')

    boston = cd.add_node(cd.CityList, 'Boston')
    bos_resturants = cd.add_node(boston, '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')

    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')

    print(f'\nCityList Dictionary')
    cd.display_dict(cd.CityList)
    print(f'\nraw data: {cd.CityList}')

if __name__ == '__main__':
    testit()
Test results:
Output:
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'}}}}
Reply


Messages In This Thread
Create Dynamic nested Dictionaries - by Larz60+ - Nov-23-2018, 04:06 AM
RE: Create Dynamic nested Dictionaries - by Larz60+ - Nov-24-2018, 11:15 AM
RE: Create Dynamic nested Dictionaries - by Larz60+ - Nov-25-2018, 02:37 PM
RE: Create Dynamic nested Dictionaries - by Larz60+ - Nov-25-2018, 05:07 PM
RE: Create Dynamic nested Dictionaries - by Larz60+ - Nov-25-2018, 07:54 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Dynamic Allocation of Nested Dictionaries Larz60+ 15 17,704 Oct-11-2016, 02:42 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020