Python Forum
DEC pack, unpack and disk-images
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
DEC pack, unpack and disk-images
#11
Pedroski55.

I'm on Linux, but even so that "looks" like what I'm after, thanks!

I'll get at it tomorrow morning after I better understand Gribouillis's rad50 code which I'm still chasing.

thanks again

curbie
Reply
#12
I don't really know what you want to do with the SD cards. I don't understand that technical stuff!

They said on linuxquestions.org, if you can mount the cards, you can copy the data to your computer.

Assuming the card is mounted at /dev/sdb

Quote:dd if=/dev/sdb of=/path/to/file.img bs=4M

Once on your computer, you can do what you want with the image file, or a copy of it!
Reply
#13
Pedroski55,

first off, Thanks for the code you posted yesterday, after some monkeying around, it works just fine for what I want.

def read_sector(disk, sector_no=0):
    """Read a single sector of the specified disk.

    Keyword arguments:
    disk -- the physical ID of the disk to read.
    curbie -- or full file path to disk image file.
    sector_no -- the sector number to read (default: 0).
    """
    # curbie -- defined disk sector notes
    # D_INFO = 0                    # disk info block
    # D_LABEL = 1                   # disk label
    # D_MFD = 2                     # disk master file directory

    # File operations with `with` syntax. To reduce file handling efforts.
    with open(disk, 'rb') as fp:    # open file read binary, get file descriptor
        fp.seek(sector_no * 512)    # seek file to byte (sector * 512)
        read = fp.read(512)         # read sector
    return read                     # return sector

sector = read_sector("/media/curbie/0012-D687/For Curbie/Zulu/HD.IMG", 2)

print(sector)                       # print sector as byte array
and returns
b'\x02\x01\x19\x00\x00\x00\x00\x00\x04\x01\xa9\x00\x00\x00\x00\x00\x06\x010\n\x00\x00\x00\x00\x02\x02K\x19\x00\x00\x00\x00\x00
which i recognize as BIG endian octal 1,2 on sector x19, then 4 rad50 bytes, 1,4 on sector xa9, then 4 rad50 bytes,...

the SD is a replacement disk for a machine's old ST506 MFM type drive, first i hope to read from it and write to it from my Linux machine, then the sky is the limit, playing with these old machines is my hobby.

Curbie

curbie
Reply
#14
(Jun-03-2024, 09:43 PM)Gribouillis Wrote:
r50 = b'ABCDEFGHIJKLMNOPQRSTUVWXYZ.$%0123456789'
illegal = 127
 
def prepare_table():
    s = r50.decode('ascii')
    d = {ord(c): i for i, c in enumerate(s)}
    d.update({ord(c.lower()): i for i, c in enumerate(s)})
    L = [illegal for i in range(256)]
    for c, i in d.items():
        L[c] = i
    table = ''.join(chr(x) for x in L).encode('ascii')
    return table
 
table = prepare_table()

For me when learning a new language, every new piece of knowledge gained comes with at least one new question to chase down. After trying
(and failing) to chase down the best or any logical way to learn libraries, I'm back to just completing and logging tutorials, but this second go round, I'm not rigidly sticking to the tutorial to learn what the author wants to teach, but am chasing down the questions raised by the lessons.

After chasing down a question raised by a lesson, that answer answered a question I had about Gribouillis's code and as always raised a new question:

Is the "table" variable in Gribouillis's code rebuilt every use (through prepare_table()) or is a "static" variable or does python have some sort of smart pseudo-static assignment?

Curbie
Reply
#15
(Jun-16-2024, 06:43 PM)Curbie Wrote: Is the "table" variable in Gribouillis's code rebuilt every use (through prepare_table()) or is a "static" variable or does python have some sort of smart pseudo-static assignment?
the function prepare_table() is only called once when the statement
table = prepare_table()
is executed. This takes 39 microseconds on my computer.

You could go faster by assigning directly the result in the code
table = b'\x7f\x7f\x7f\x7f\x7f\x7f\x7f\x7f\x7f\x7f\x7f\x7f\x7f\x7f\x7f\x7f\x7f\x7f\x7f\x7f\x7f\x7f\x7f\x7f\x7f\x7f\x7f\x7f\x7f\x7f\x7f\x7f\x00\x7f\x7f\x7f\x1c\x1d\x7f\x7f\x7f\x7f\x7f\x7f\x7f\x7f\x1b\x7f\x1e\x1f !"#$%&\'\x7f\x7f\x7f\x7f\x7f\x7f\x7f\x01\x02\x03\x04\x05\x06\x07\x08\t\n\x0b\x0c\r\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x7f\x7f\x7f\x7f\x7f\x7f\x01\x02\x03\x04\x05\x06\x07\x08\t\n\x0b\x0c\r\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x7f\x7f\x7f\x7f\x7f\x7f\x7f\x7f\x7f\x7f\x7f\x7f\x7f\x7f\x7f\x7f\x7f\x7f\x7f\x7f\x7f\x7f\x7f\x7f\x7f\x7f\x7f\x7f\x7f\x7f\x7f\x7f\x7f\x7f\x7f\x7f\x7f\x7f\x7f\x7f\x7f\x7f\x7f\x7f\x7f\x7f\x7f\x7f\x7f\x7f\x7f\x7f\x7f\x7f\x7f\x7f\x7f\x7f\x7f\x7f\x7f\x7f\x7f\x7f\x7f\x7f\x7f\x7f\x7f\x7f\x7f\x7f\x7f\x7f\x7f\x7f\x7f\x7f\x7f\x7f\x7f\x7f\x7f\x7f\x7f\x7f\x7f\x7f\x7f\x7f\x7f\x7f\x7f\x7f\x7f\x7f\x7f\x7f\x7f\x7f\x7f\x7f\x7f\x7f\x7f\x7f\x7f\x7f\x7f\x7f\x7f\x7f\x7f\x7f\x7f\x7f\x7f\x7f\x7f\x7f\x7f\x7f\x7f\x7f\x7f\x7f\x7f\x7f\x7f\x7f\x7f\x7f\x7f'
The 'table' variable has a 256 bytes string value.

'table' is a global variable in this code. Global variables are stored in a global dictionary. You can consider them static variables.
« We can solve any problem by introducing an extra level of indirection »
Reply
#16
Gribouillis thank you,

just what i was trying to figure out, prepare_table() is run once anytime either pack() or unpack() is called and could be made less instruction intensive by a direct table assignment as you listed. Not static.

Like I said, just chasing down answers to any question I don't know. Your implementation runs just fine, just studying the why of your choice of prepare_table().

This old DEC-type directory structures have two rad50 words for the file name, one rad50 word for the file extension, 42 files per full directory block 3 * 42 = 126) and less than 42 the the last directory block. rebuilding a table 100's of times for each directory block read that has maybe 10 directory blocks, triggers a red flag. Not because of speed, but personal practice.

Not to inpune your code which I believe was written to help

thanks again

Curbe
Reply
#17
(Jun-16-2024, 09:41 PM)Curbie Wrote: prepare_table() is run once anytime either pack() or unpack() is called and could be made less instruction intensive by a direct table assignment
No, prepare_table() is run only once. It is not run again when pack() and unpack() are called. The time taken by prepare_table() is neglectible because it is run only once.

Why didn't you read my reply?
« We can solve any problem by introducing an extra level of indirection »
Reply
#18
I'm confused,
Quote:Is the "table" variable in Gribouillis's code rebuilt every use (through prepare_table()) or is a "static" variable or does python have some sort of smart pseudo-static assignment?
Reply
#19
why is that NOT static?
Reply
#20
(Jun-17-2024, 09:08 PM)Curbie Wrote: why is that NOT static?
I don't understand the problem. Python is not C. There are no static variables. It is perfecly reasonable to initialize a variable by using a function. What's wrong with that? By looking at the function I can understand why the table contains that data.

If you prefer a literal 256-bytes string in your program, no problem. It won't change the execution of the program. You will just gain a few microseconds at program initialization.

Initialization code exists for libraries in all programming languages.
« We can solve any problem by introducing an extra level of indirection »
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Too much values to unpack actualpy 3 651 Feb-11-2024, 05:38 PM
Last Post: deanhystad
  Hard disk structure like a file selection dialog malonn 2 926 Aug-09-2023, 09:14 PM
Last Post: malonn
  unpack dict menator01 1 1,320 Apr-09-2022, 03:10 PM
Last Post: menator01
  ValueError: not enough values to unpack (expected 4, got 1) vlearner 2 6,564 Jan-28-2022, 06:36 PM
Last Post: deanhystad
  JS Buffer.from VS struct.pack DreamingInsanity 3 2,655 Apr-05-2021, 06:27 PM
Last Post: DreamingInsanity
  [SOLVED] [geopy] "ValueError: too many values to unpack (expected 2)" Winfried 2 3,033 Mar-30-2021, 07:01 PM
Last Post: Winfried
  Cannot unpack non-iterable NoneType object, i would like to ask for help on this. Jadiac 3 9,155 Oct-18-2020, 02:11 PM
Last Post: Jadiac
  subprogram issues: cannot unpack non-iterable function object error djwilson0495 13 6,368 Aug-20-2020, 05:53 PM
Last Post: deanhystad
  How to Calculate CPU, Disk, Memory and Network utilization rate skvivekanand 1 2,153 Jun-16-2020, 08:53 PM
Last Post: jefsummers
  struct.unpack failed Roro 2 3,511 Jun-13-2020, 05:28 PM
Last Post: DreamingInsanity

Forum Jump:

User Panel Messages

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