Python Forum
Mirroring disk structures in nested dictionaries
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Mirroring disk structures in nested dictionaries
#11
deanhystad,

Thank you, finally got to code that works.
from pprint import pprint
def get_all_values(obj, level=0):                   # Not my function
    """Walk through a dictionary of dicts and lists."""
    if type(obj) is dict:
        for key, value in obj.items():
            if type(value) in [dict, list]:
                print('    ' * level, key, sep='')
                level = level + 1
                get_all_values(value, level)
                level = level - 1
            else:
                print('    ' * (level), key, ': ', value, sep='')

    elif type(obj) is list:
        for i, element in enumerate(obj):
            if type(element) in [dict, list]:
                print('    ' * level, i, sep='')
                level = level + 1
                get_all_values(element, level)
                level = level - 1
            else:
                print('    ' * (level), element, sep='')
    else:
        raise ValueError

def add(old, new):                      # function by deanhystad - curbie comment
    """Add new dictionary to old."""
    while True:
        key, value = next(iter(new.items()))
        if key in old:
            old = old[key]
        else:
            old[key] = value
            break
        new = value

dskn = 0                                # alpha micro up to 32mb disk number (16 bit logical disk)
ufd = ['1,2', '1,2', '1,4', '1,4']      # list for user file directories
file = ['BADBLK.SYS', 'AMOSL.DIR', 'AMOSL.INI', 'AMOS32.INI']                    # file name
blocks = [3, 42, 2, 4]                  # list for number of full blocks in file
active = [500, 400, 300, 200]           # list for number of bytes in last block of file
link = [1600, 1500, 1400, 1300]         # list for link to first block of file

dsk = {}                                # create blank dictionary

for i in range(0, 4):                   # loop through adding files
    uuu = ufd[i]
    fff = file[i]
    bbb = blocks[i]
    aaa = active[i]
    lll = link[i]
    add(dsk, {dskn: {uuu: {fff: {"blocks": bbb, "active": aaa, "link": lll}}}})

get_all_values(dsk)



(Oct-01-2024, 05:17 PM)deanhystad Wrote: But before writing this, make sure you need it. I would start this project by writing the code that loops through the entire disk (the part you are putting off). That code may reveal disks, dictionaries and files in an order that naturally lends itself to making a nested dictionary without much effort.

Done before I started this thread.
[1,2]	Link:    127	PW: |      |
[1,3]	Link:   1081	PW: |      |
[1,4]	Link:   2157	PW: |      |
plus 29 more UFDs
*********************************************************
AAA   .BAK   Blocks:      1 Active:      2   Link:   2156
ABC   .LIT   Blocks:     19 Active:     60   Link:   2158
ABCLON.LIT   Blocks:     29 Active:    373   Link:   2177
ABMAST.LIT   Blocks:     17 Active:    370   Link:   2206
ABMON .CMD   Blocks:      1 Active:     54   Link:   2223
ABREAD.LIT   Blocks:      4 Active:    254   Link:   2224
ABRELI.EXP   Blocks:      1 Active:     30   Link:   2228
ABRELI.LIT   Blocks:     21 Active:    454   Link:   2229
ABRELI.OVR   Blocks:     10 Active:    416   Link:   2250
ABRELI.RKT   Blocks:      4 Active:    299   Link:   2260
ABRELI.RTB   Blocks:      4 Active:    228   Link:   2264
ABRELI.SYS   Blocks:     16 Active:    122   Link:   2268
ABRND .LIT   Blocks:      5 Active:     20   Link:   2284
plus 1400 more full file specs distributed among the UFDs
Now on to studying your 'add' function to try to figure out where I goofed up.
Reply
#12
Why do you do this:
for i in range(0, 4):                   # loop through adding files
    uuu = ufd[i]
    fff = file[i]
    bbb = blocks[i]
    aaa = active[i]
    lll = link[i]
    add(dsk, {dskn: {uuu: {fff: {"blocks": bbb, "active": aaa, "link": lll}}}})
instead of this:
for i in range(0, 4):                   # loop through adding files
    add(dsk, {dskn: {ufd[i]: {file[i]: {"blocks": blocks[i], "active": active[i], "link": link[i]}}}})
Reply
#13
Because, I find it easier to read and debug while learning a new language, when everything works as intended, I can compress for speed and fewer lines.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Nested Lists & Dictionaries Hudjefa 5 1,318 Sep-23-2024, 08:20 PM
Last Post: DeaD_EyE
  DEC pack, unpack and disk-images Curbie 32 6,935 Aug-23-2024, 03:37 PM
Last Post: Curbie
  Windows Disk Cleanup Code Help Needed nal2us2 3 1,029 Jul-19-2024, 04:03 AM
Last Post: deanhystad
  Hard disk structure like a file selection dialog malonn 2 1,723 Aug-09-2023, 09:14 PM
Last Post: malonn
  Searching through Nested Dictionaries and Lists Dave_London 1 11,712 Jul-09-2020, 03:36 PM
Last Post: mrdominikku
  ctypes and custom data type (structures, pointer to structures) python_user_n 0 3,820 Jul-08-2020, 05:52 PM
Last Post: python_user_n
  How to Calculate CPU, Disk, Memory and Network utilization rate skvivekanand 1 2,626 Jun-16-2020, 08:53 PM
Last Post: jefsummers
  Creating Nested Dictionaries Confusion gw1500se 2 2,819 May-18-2020, 11:16 PM
Last Post: gw1500se
  how to write offset number to disk use python? Pyguys 4 4,188 Apr-11-2020, 07:53 AM
Last Post: Pyguys
  Finding value in nested dictionaries with lists mart79 16 18,423 Mar-08-2020, 08:16 PM
Last Post: ndc85430

Forum Jump:

User Panel Messages

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