Python Forum
Hashing an address for binary file - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Hashing an address for binary file (/thread-35445.html)



Hashing an address for binary file - Python_help - Nov-03-2021

I am facing the error: OSError: [Errno 22] Invalid argument when trying to dump an object record to the hashed address in a .dat binary file, any idea where I am going wrong?

import pickle


# Creating the Record


class CarRecord:

    def __init__(self):
        self.VehicleID=""
        self.Registration=""
        self.DateOfRegistration = None
        self.EngineSize = 0
        self.PurchasePrice = 0.00

CarsArray=["" for i in range(2)]   #Creating the array

#Creating the binary file
CarsFile=open("CarsRAND.dat", 'wb')


#Assigning each array element to the class

for i in range(2):
    CarsArray[i]= CarRecord()
    VehicleID=str(input("Enter Vehicle ID"))
    CarsArray[i].VehicleID=(VehicleID)
    print ("Vehicle ID:", i, CarsArray[i].VehicleID, " has been entered")

#writing each record to the file
for i in range(2):
    Address=hash(CarsArray[i].VehicleID) #Hashing address
    print("Address: ", str(i), Address)
    CarsFile.seek(Address) #Seeking hashed address
    pickle.dump(CarsArray[i],CarsFile) #Putting record in hashed address


CarsFile.close


# Reading record from hashed address.
CarsFile=open("CarsRND.dat", 'rb')
VehicleID=str(input("Enter Vehicle ID to Load"))
Address=hash(VehicleID)
CarsFile.seek(Address)
Record = pickle.load(CarsFile)
print(Record.VehicleID)

CarsFile.close
        



RE: Hashing an address for binary file - Gribouillis - Nov-03-2021

I don't understand the call to seek() in the file with an offset equal to the hash() of some data called VehicleID. What is the purpose of this call? It will obviously lead to invalid operations on the file.


RE: Hashing an address for binary file - Python_help - Nov-03-2021

(Nov-03-2021, 04:58 PM)Gribouillis Wrote: I don't understand the call to seek() in the file with an offset equal to the hash() of some data called VehicleID. What is the purpose of this call? It will obviously lead to invalid operations on the file.

It takes the VehicleID value and puts it through the hash function to create an address.
The seek part creates the address in the file and dumps the record into it.


RE: Hashing an address for binary file - Gribouillis - Nov-03-2021

Python_help Wrote:The seek part creates the address in the file
We don't have the same notion of files' seek() function. As far as I know it can only set the file's position to an existing position in the file, not create a position, or an address.


RE: Hashing an address for binary file - Python_help - Nov-03-2021

(Nov-03-2021, 05:18 PM)Gribouillis Wrote:
Python_help Wrote:The seek part creates the address in the file
We don't have the same notion of files' seek() function. As far as I know it can only set the file's position to an existing position in the file, not create a position, or an address.

My issue is similar to the one that has been discussed in another forum by someone else some time ago, i'm guessing that the same reason applies to my problem?

https://stackoverflow.com/questions/52347437/oserror-errno-22-invalid-argument-python-file-processing it


RE: Hashing an address for binary file - Gribouillis - Nov-03-2021

As you can see in this discussion, the error was raised by the call to seek(). It means that the offset passed to seek() is not accepted by the operating system.


RE: Hashing an address for binary file - deanhystad - Nov-03-2021

As far as I know (or found with minimal search efforts) the behavior of seeking past the end of a file is not defined. This simple test packs the first 32 bytes with zeros.
with open("test.bin", "wb") as file:
    file.seek(32)
    file.write(b"Where's Waldo")
Output:
Dump ................ ................ Where's Waldo
But this might just be the behavior for the compiler I am using or the platform I am running on. I have not found anything definitive that says this is what I should expect. And since I don't know this will always work, I am not going to depend on it ever working.

Either way I would never use the value returned by hash as a file offset. hash(2000) == 2000 and hash(2001) == 2001. 2000 requires at least 3 bytes of storage, so I cannot store 2000 and 2001 using your scheme. And what about this?
x = "one"
y = "two"
print(hash(x), hash(y), id(x), id(y))
Output:
6029220552427752314 -4114662839508124676 1827875350256 1827875262064
I don't think I can seek(-4114662839508124676), and I don't want to make a 6029220552427752317 byte file to store "one".


RE: Hashing an address for binary file - Gribouillis - Nov-03-2021

The truncate() method can be used to extend the file in size.


RE: Hashing an address for binary file - ndc85430 - Nov-04-2021

Also posted in Dream.in.code: https://www.dreamincode.net/forums/topic/421793-hashed-address-in-binary-file.