Python类设置为没有pickle的序列化 [英] Python class setup for serialization without pickle

查看:287
本文介绍了Python类设置为没有pickle的序列化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

场景



我在python中寻找一个面向对象的方法,使得可以在数据文件中保存一个类的实例,在稍后的时间点。我现在的方法如下:

  class A(object):
def __init __(self,ComplexParam1,ComplexParam2) :
self.ComplexParam1 = ComplexParam1
self.ComplexParam2 = ComplexParam2

@staticmethod
def创建(EasyParam1,EasyParam2):
#do一些复杂的计算从EasyParam1和EasyParam2获取ComplexParam1和ComplexParam2
return A(ComplexParam1,ComplexParam2)

def保存(self,Filename):
#write ComplexParam1和ComplexParam2 to disk

@staticmethod
def Load(Filename):
#read ComplexParam1和ComplexParam2和调用构造函数
return A(ComplexParam1,ComplexParam2)

正如你可以看到 ComplexParam1 ComplexParam2 是要计算的参数,不用于对象 A 的第一次创建,因为它们非常复杂,而 EasyParam1 EasyParam2 是已知参数。认为它好像 EasyParameters 是整数, ComplexParameters 是基于<$ c $构造的大型基数c> EasyParameters



所以我使用上面的设置保存 c $ c>加载对象到/从文件,其中创建使用构造函数,因为 ComplexParam1 ComplexParam2 存储在文件中,不需要再次计算。



问题



到目前为止,上面显示的方法对我来说很好。然而,当该方案也与类继承一起使用时,会出现问题。所以我正在寻找一个更好的和更清洁的解决方案,我的问题。



在C + +我将重载构造函数,并使两个可能的类的创建,但这不是

解决方案



任何帮助,链接和建议。

我认为这是 @classmethod的情况 装饰器。例如,如果将 Load 方法更改为以下内容:

  @classmethod 
def Load(cls,Filename):
#Do stuff
return cls(ComplexA,ComplexB)

然后你可以覆盖构造函数:

  B类$ b def __init __(self,complexA,complexB):
#无论你想要什么,包括调用父构造函数

最后,你可以调用 B .__ init __ B.Load(some_file)


Scenario

I am looking for an object oriented approach in python that makes it possible to save an instance of a class in a data file and also load it again in at a later point in time. My current approach looks like this:

class A(object):
    def __init__(self, ComplexParam1, ComplexParam2):
        self.ComplexParam1 = ComplexParam1
        self.ComplexParam2 = ComplexParam2

    @staticmethod
    def Create(EasyParam1, EasyParam2):
        #do some complex calculation to get ComplexParam1 and ComplexParam2 from EasyParam1 and EasyParam2
        return A(ComplexParam1, ComplexParam2)        

    def Save(self, Filename):
        #write ComplexParam1 and ComplexParam2 to disc

    @staticmethod
    def Load(Filename):
        #read ComplexParam1 and ComplexParam2 and call constructor
        return A(ComplexParam1, ComplexParam2)

As you can see ComplexParam1 and ComplexParam2 are to be calculated parameters and are not used for a first creation of the object A since they are very complex to get, whereas EasyParam1 and EasyParam2 are "known" parameters. Think of it as if the EasyParameters are integers and the ComplexParameters are large matricies that are constructed based on EasyParameters

So I am using the setup above to Save and Load objects to and from file, where Create uses the constructor since ComplexParam1 and ComplexParam2 are stored in file and do not need to be calculated again.

Problem

Up until now, the approach shown above worked just fine for me. Problems however arise, when this scheme is also used with class inheritances. So I am looking for a nicer and cleaner solution to my problem.

In C++ I would overload the constructor and make two possible creations of the class available, but this is not supported in python.

Any help, link and suggestion is appreciated.

解决方案

I think this is a case for the @classmethod decorator. For example, if you change the Load method to the following:

    @classmethod
    def Load(cls, Filename):
        # Do stuff
        return cls(ComplexA, ComplexB)

Then you could override the constructor:

class B(A):
    def __init__(self, complexA, complexB):
        # Whatever you want, including calling the parent constructor

And, finally, you could call B.Load(some_file) which would invoke B.__init__.

这篇关于Python类设置为没有pickle的序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆