Python Forum
Nested Dictionaries, good programming practice?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Nested Dictionaries, good programming practice?
#1
I’m still playing around with nested dictionaries trying to store an ancient and arcane file system in memory to implement a PC <> Alpha file transfer utility. I can work with the nested directories to get what I want. But my question is this structure good programming practice, or even though I can get it to work, it’s goofed up? I don’t want build on a shaky foundation!


What’s is in the file system, and what’s being saved in case it matters to anyone.
‘ dsk0’ is the combined file-system storage-device’s logical (virtual) name (‘dsk’) and number (‘0’), hung off the device name/number are the user-account names ('1,2'), hung off the user-account names is the user-account attributes LNK (link) and PW (password), hung off the user-account attributes is the user-account files (‘AMOS .BAK’) and hung off that are that file’s attributes, ‘BLK’ (file blocks), ‘ACT’ (active bytes in last block), ‘LNK’ (link to first block in file)

Output:
{'dsk0': {'1,2': {'LNK': 126, 'PW': ' ', '(AMOS .BAK)': {'BLK': 108, 'ACT': 407, 'LNK': 18}, '(APDDIR.DBD)'...
Full output attached.

curbie

Attached Files

.txt   fs.txt (Size: 149.54 KB / Downloads: 114)
Reply
#2
Instead of nested dictionaries, you could use the fs module (pyfilesystem2). It enables you in particular to store a filesystem in memory with a file system API instead of a dictionary API (and internally this is implemented with nested dictionaries).

The fs module is excellent and recommandable but it does not handle symbolic links. This can be an annoying limitation in some use cases.
« We can solve any problem by introducing an extra level of indirection »
Reply
#3
Gribouillis, thanks as always, but I’m still learning Python and I learn better by doing, so my question is, is my structure good programming practice, or even though I can get it to work, it’s goofed up? I don’t want to start building on shaky foundations.

I have a 68000 dis-assembler I wrote long ago that is in the queue for porting to python, which seems a good candidate for nested-dictionaries also, so I sorta don’t care at this moment about other things to learn, until I learn nested-dictionaries.

curbie
Reply
#4
(May-27-2025, 03:02 PM)Curbie Wrote: so my question is, is my structure good programming practice, or even though I can get it to work, it’s goofed up? I don’t want to start building on shaky foundations.
I think a more common practice is to have nested objects (class instances). You could have a device object containing a list of user accounts objects, each containing attributes and password, etc.

It requires more work for designing the classes but structured objects have many advantages over raw dictionaries.
« We can solve any problem by introducing an extra level of indirection »
Reply
#5
Gribouillis, Thanks again! I was most worried about using account and file names as “keys” instead of “values” (‘1,2’ or ‘AMOS .BAK’). I understand and have programmed in OOP, but I typically wait until I thoroughly understand the basics before OOP or shortcuts, and I have a ways to go.

curbie
Reply
#6
For nested dictionaries i like this module nested-lookup,make it easier to navigate.
Eg using your data disk is fs.txt.
from nested_lookup import nested_lookup

>>> find_1_250 = nested_lookup('1,250', disk)
>>> find_1_250
[{'(FILDIR.DBD)': {'ACT': 65535, 'BLK': 7, 'LNK': 62916},
  '(FILKY1.DBK)': {'ACT': 65535, 'BLK': 3, 'LNK': 62913},
  '(FILKY2.DBK)': {'ACT': 65535, 'BLK': 6, 'LNK': 62907},
  'LNK': 16020,
  'PW': '      '}]

>>> nested_lookup('LNK', find_1_250)
[16020, 62916, 62913, 62907]
Or find LNK directly in 1,250.
>>> nested_lookup(key="LNK", document=nested_lookup('1,250', disk), wild=True)
[16020, 62916, 62913, 62907]
As mention classed or eg @dataclass work fint for this.
from dataclasses import dataclass, field
from typing import Dict, Optional

@dataclass
class FileEntry:
    name: str
    blk: int
    act: int
    lnk: int

@dataclass
class Account:
    name: str
    lnk: int
    pw: str
    files: Dict[str, FileEntry] = field(default_factory=dict)

@dataclass
class Device:
    name: str
    accounts: Dict[str, Account] = field(default_factory=dict)
Pros: auto-generated __init__, clear fields, IDE auto-completion, type hints.
Cons: a bit more code up front, but well worth the maintainability.
Reply
#7
snippsat, thanks, I saved your post and will revisit it after I'm comfortable with working with nested-dictionaries manually.

curbie
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Mirroring disk structures in nested dictionaries Curbie 16 4,653 Apr-27-2025, 02:43 PM
Last Post: deanhystad
  Best programming practice for imports Curbie 8 3,234 Oct-16-2024, 02:20 AM
Last Post: Curbie
  Nested Lists & Dictionaries Hudjefa 5 2,258 Sep-23-2024, 08:20 PM
Last Post: DeaD_EyE
  how can I organise my code : best practice? pythonbum 0 925 May-24-2024, 03:17 PM
Last Post: pythonbum
  Searching through Nested Dictionaries and Lists Dave_London 1 12,452 Jul-09-2020, 03:36 PM
Last Post: mrdominikku
  Creating Nested Dictionaries Confusion gw1500se 2 3,468 May-18-2020, 11:16 PM
Last Post: gw1500se
  Threading best practice EvanS1 2 4,710 Apr-21-2020, 10:11 PM
Last Post: EvanS1
  Finding value in nested dictionaries with lists mart79 16 24,178 Mar-08-2020, 08:16 PM
Last Post: ndc85430
  nested dictionaries to CSV mart79 9 18,994 Jul-29-2019, 04:59 AM
Last Post: mart79
  Transform simplified dictionary to nested dictionaries bhojendra 1 3,519 Jul-02-2019, 02:05 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

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