泡菜不倾倒一切?
我目前正在使用一个简单的机器人工作,并希望使用Pickle存储机器人的主要对象。但是,试图与最初声明的组件进行互动似乎并不起作用。存储和读取该项目的代码如下所示。应该注意的是,此对象处于工作状态:
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()
正如您可能猜到的那样,该对象具有名为:move_inches
的方法。该对象是在if块中创建的,并在main
函数中引用。
当使用已读取的对象时,我会收到此错误:
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'
这使我相信我不能像这样存储这样的对象。
有谁知道存储对象的整个状态或防止再次调用构造函数的任何选择?
谢谢!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
目前尚不清楚为什么您不愿重新运行
Zumi()
ctor。文档解释说,有些对象泡菜不能序列化。
打开文件描述是其中之一。
您也许可以通过取消挑选然后致电一些Zumi来恢复
重新建立(重新打开)I2C连接的函数。
或者,考虑创建一个伴随对象
具有属性的最多,
但是省略了非秘密序列化的诸如打开文件描述符。
调用
zumi()
以通常的方式创建z
,并在
z
的属性上从同伴中复制属性。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 createz
,and copy attributes from the companion on top of
z
's attributes.