Pickle Not Dumping Everything?

Viewed 39

I am currently working on a simple robot, and am looking to use Pickle to store the main object of the robot. Trying to interact with components initially declared does not seem to be working however. The code for storing and reading the item is shown below. It should be noted that this object here is in working order:

import pickle, os

from sys import argv

from zumicreator import ZumiCreator
from zumi.zumi import Zumi
functions = {"move_inches": "z.move_inches(fParam)"}

def getZumi():
    file = open(os.getcwd() + "/Code/lib/state", "rb")
    zumiHousing = pickle.load(file)
    return zumiHousing

def main():
    if argv[1] == "create":
        
        # Create the Zumi object
        # and store it with pickle.
        
        zumiHousing = Zumi()
        file = open(os.getcwd() + "/Code/lib/state", "wb")
        pickle.dump(zumiHousing, file)

        print('Dump Successful!')
        
        # Exit since that is all we 
        # needed to do.
        quit()
        
    # ! Do NOT change ANY of the following variable names! 
    z = getZumi()
    fName = argv[1].split()[0]
    fParam = str(argv[1].split()[1:]).replace("[", "").replace("]", "").replace("'", "")

    # Runs the function with the desired arguments.
    eval(functions[fName])
    
if __name__ == '__main__':
    main()

As you may have guessed, the object has a method named: move_inches. The object is created in the if block, and referenced in main function.

I am receiving this error when using the object that has been read in:

Traceback (most recent call last):
  File "/home/pi/.local/lib/python3.5/site-packages/zumi/zumi.py", line 526, in __read_raw_MPU_data
    high = self.bus.read_byte_data(Device.MPU, addr)
  File "/home/pi/.local/lib/python3.5/site-packages/smbus2/smbus2.py", line 429, in read_byte_data
    self._set_address(i2c_addr, force=force)
  File "/home/pi/.local/lib/python3.5/site-packages/smbus2/smbus2.py", line 354, in _set_address
    ioctl(self.fd, I2C_SLAVE, address)
OSError: [Errno 9] Bad file descriptor

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/pi/Code/lib/translator.py", line 40, in <module>
    main()
  File "/home/pi/Code/lib/translator.py", line 37, in main
    eval(functions[fName])
  File "<string>", line 1, in <module>
  File "/home/pi/.local/lib/python3.5/site-packages/zumi/zumi.py", line 2029, in move_inches
    angle = self.read_z_angle()
  File "/home/pi/.local/lib/python3.5/site-packages/zumi/zumi.py", line 1170, in read_z_angle
    angle_list = self.update_angles()
  File "/home/pi/.local/lib/python3.5/site-packages/zumi/zumi.py", line 1088, in update_angles
    self.mpu_list = self.mpu.read_all_MPU_data()
  File "/home/pi/.local/lib/python3.5/site-packages/zumi/zumi.py", line 568, in read_all_MPU_data
    Gyro.X, Gyro.Y, Gyro.Z)
  File "/home/pi/.local/lib/python3.5/site-packages/zumi/zumi.py", line 543, in read_multiple_raw_MPU_data
    result.append(self.__read_raw_MPU_data(item))
  File "/home/pi/.local/lib/python3.5/site-packages/zumi/zumi.py", line 533, in __read_raw_MPU_data
    return self.__read_raw_MPU_data()
TypeError: __read_raw_MPU_data() missing 1 required positional argument: 'addr'

This leads me to believe that I cannot store the object like such.

Does anyone know any alternatives to store the entire state of the object, or prevent calling the constructor again?

Thanks!

1 Answers

It's unclear why you're averse to re-running the Zumi() ctor.

The documentation explains that there are some objects pickle cannot serialize. An open file descriptor is one of them.

You may be able to recover by unpickling and then calling some zumi function which re-establishes (re-opens) an I2C connection.

Alternatively, consider creating a companion object which has most of the attributes, but omits non-serializable ones such as open file descriptors. Call Zumi() in the usual way to create z, and copy attributes from the companion on top of z's attributes.

Related