1

据我了解,使用 serpent 比 pickle 序列化对象更安全。

我使用以下类:

import serpent

class Test:
    def save(self, fileName) :
        ser = serpent.dumps({"schema": self}, indent=True)
        open(fileName, "wb" ).write(ser)


    def load(self, fileName) :

        self = serpent.load(open(fileName, "rb"))["schema"]



    def someFunction(self) :

        [...]

我希望能够做类似的事情

test = Test()
test.save("afile")

[...]

test2 = Test().load()
test2.someFunction()

但是,当我调用 Test().load() 时,我得到一个对象树,而不是一个对象。所以我不能使用它......如何从对象树中获取对象?这样做,我会得到与泡菜完全相同的安全问题吗?

编辑:来自Pyro 的文档:serpent 序列化为 Python 文字表达式。接受相当多的不同类型。许多将被序列化为字典。如果需要,您可能需要在接收端将文字显式转换回特定类型,因为大多数自定义类不会自动处理。

所以,我想最后的问题是:有没有关于如何将这样的文字字典翻译回对象的方法?我想,它一定已经被很多人做了很多次了......

4

1 回答 1

0

There might be better answers to this question but since none has been proposed, I can share some pieces of solution from my own researches. The solution to this problem depends very much on the structure of the classes that need to be serialized/deserialized but for an idea of how such a parsing can be made, one can have a look at the dict_to_class method implemented in Pyro4.util.

于 2013-10-16T06:42:19.023 回答