Python Forum
Hashing an address for binary file
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Hashing an address for binary file
#1
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
        
Gribouillis likes this post
Reply
#2
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.
Reply
#3
(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.
Reply
#4
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.
Reply
#5
(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/5234...processing it
Reply
#6
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.
Reply
#7
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".
Reply
#8
The truncate() method can be used to extend the file in size.
Reply
#9
Also posted in Dream.in.code: https://www.dreamincode.net/forums/topic...inary-file.
buran likes this post
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  problem in entering address of a file in input akbarza 0 619 Oct-18-2023, 08:16 AM
Last Post: akbarza
  How do I read and write a binary file in Python? blackears 6 6,016 Jun-06-2023, 06:37 PM
Last Post: rajeshgk
  Read/Write binary file deanhystad 3 3,094 Feb-01-2021, 10:29 AM
Last Post: Larz60+
  Binary File Error SalsaBeanDip 1 1,724 Oct-12-2020, 09:13 PM
Last Post: bowlofred
  Convert file of hex strings to binary file medatib531 4 13,573 Oct-09-2020, 05:42 PM
Last Post: DeaD_EyE
  Binary File Read Aussie 6 8,226 Sep-03-2020, 03:57 AM
Last Post: deanhystad
  Binary file Aussie 12 4,382 Aug-31-2020, 09:20 PM
Last Post: Aussie
  python read binary file Pyguys 4 3,792 Jul-13-2020, 02:34 AM
Last Post: Pyguys
  Failure in writing binary text to file Gigux 7 3,717 Jul-04-2020, 08:41 AM
Last Post: Gigux
  search binary file and list all founded keyword offset Pyguys 4 2,699 Mar-17-2020, 06:46 AM
Last Post: Pyguys

Forum Jump:

User Panel Messages

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