是否有可能ujson.dumps()python类实例(更快的deepcopy)

时间:2017-09-08 19:30:20

标签: python pickle deep-copy jsonpickle ujson

我正在尝试快速复制一个类实例。 cPickle.loads(cPickle.dumps(),-1)工作正常,几乎比copy.deepcopy快5倍,但我read that ujson is much faster than cPickle。我无法让ujson使用自定义类,是否可以这样做?

示例:

import cPickle as pickle
import ujson

class AClass(object):
    def __init__(self):
        print('init')
        self.v = 10
        self.z = [2,3,4]
        self._zdict = dict(zip(self.z,self.z))

a = AClass()
a
#<__main__.AClass at 0x118b1d390>


# does not work with ujson
ua = ujson.dumps(a)
au = ujson.loads(ua)
au
#{u'v': 10, u'z': [2, 3, 4]}


# but works with pickle
pa = pickle.dumps(a)
ap = pickle.loads(pa)
ap
#<__main__.AClass at 0x117460190>

2 个答案:

答案 0 :(得分:2)

ujson未序列化对象;它只是将其属性dict编码为JSON对象。那里没有足够的信息来完整地再现原始对象;最明显的迹象是ujson.dumps的输出中没有任何内容记录了a类的实例。

usjoncPickle快得多的原因是cPickle必须做的更多。

答案 1 :(得分:1)

一个想法是定义你自己的protocole,这是为pickle描述的概念的基础。 在班级中定义__getstate____setsatte__个实例:

class AClass(object):
    def __init__(self, v, z):
        self.v = v
        self.z = z
        self._zdict = dict(zip(self.z, self.z))

    def __repr__(self):
        return repr({'v': self.v, 'z': self.z, '_zdict': self._zdict})

    def __getstate__(self):
        return {'v': self.v, 'z': self.z}

    def __setstate__(self, state):
        self.__dict__.update(state)
        self._zdict = dict(zip(self.z, self.z))

然后,您可以像这样定义save()load()函数:

import importlib
import json
import io

def save(instance, dst_file):
    data = {
        'module': instance.__class__.__module__,
        'class': instance.__class__.__name__,
        'state': instance.__getstate__()}
    json.dump(data, dst_file)


def load(src_file):
    obj = json.load(src_file)
    module_name = obj['module']
    mod = importlib.import_module(module_name)
    cls = getattr(mod, obj['class'])
    instance = cls.__new__(cls)
    instance.__setstate__(obj['state'])
    return instance

简单使用(在这里使用StringIO而不是经典文件):

a_class = AClass(10, [2, 3, 4])
my_file = io.StringIO()
save(a_class, my_file)

print(my_file.getvalue())
# -> {"module": "__main__", "class": "AClass", "state": {"v": 10, "z": [2, 3, 4]}}

my_file = io.StringIO(my_file.getvalue())
instance = load(my_file)

print(repr(instance))
# -> {'v': 10, 'z': [2, 3, 4], '_zdict': {2: 2, 3: 3, 4: 4}}