Store object using Python pickle, and load it into different namespace

前端 未结 3 1971
失恋的感觉
失恋的感觉 2021-01-12 02:08

I\'d like to pass object state between two Python programs (one is my own code running standalone, one is a Pyramid view), and different namespaces. Somewhat related questio

相关标签:
3条回答
  • 2021-01-12 02:41

    Solution 1

    On pickle.load, the module __main__ needs to have a function or class called MyClass. This does not need to be the original class with the original source code. You can put other methods in it. It should work.

    class MyClass(object):
        pass
    
    with open("my_c.pik", "rb") as f :
        c = pickle.load(f)
    

    Solution 2

    Use the copyreg module which is used to register constructors and pickle functions to pickle specific objects. This is the example given by the module for a complex number:

    def pickle_complex(c):
        return complex, (c.real, c.imag)
    
    copyreg.pickle(complex, pickle_complex, complex)
    

    Solution 3

    Override the persistent_id method of the Pickler and Unpickler. pickler.persistent_id(obj) shall return an identifier that can be resolved by unpickler.persistent_id(id) to the object.

    0 讨论(0)
  • 2021-01-12 02:50

    The easiest solution is to use cloudpickle:

    https://github.com/cloudpipe/cloudpickle

    It enabled me to easily send a pickled class file to another machine and unpickle it using cloudpickle again.

    0 讨论(0)
  • 2021-01-12 02:54

    Use dill instead of pickle, because dill by default pickles by serializing the class definition and not by reference.

    >>> import dill
    >>> class MyClass:
    ...   def __init__(self): 
    ...     self.data = set()
    ...     self.more = dict()
    ...   def do_stuff(self):
    ...     return sorted(self.more)
    ... 
    >>> c = MyClass()
    >>> c.data.add(1)
    >>> c.data.add(2)
    >>> c.data.add(3)
    >>> c.data
    set([1, 2, 3])
    >>> c.more['1'] = 1
    >>> c.more['2'] = 2
    >>> c.more['3'] = lambda x:x
    >>> def more_stuff(self, x):  
    ...   return x+1
    ... 
    >>> c.more_stuff = more_stuff
    >>> 
    >>> with open('my_c.pik', "wb") as f:
    ...   dill.dump(c, f)
    ... 
    >>> 
    

    Shut down the session, and restart in a new session…

    Python 2.7.8 (default, Jul 13 2014, 02:29:54) 
    [GCC 4.2.1 Compatible Apple Clang 4.1 ((tags/Apple/clang-421.11.66))] on darwin
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import dill
    >>> with open('my_c.pik', "rb") as f:
    ...   c = dill.load(f)
    ... 
    >>> c.data
    set([1, 2, 3])
    >>> c.more
    {'1': 1, '3': <function <lambda> at 0x10473ec80>, '2': 2}
    >>> c.do_stuff()
    ['1', '2', '3']
    >>> c.more_stuff(5)
    6
    

    Get dill here: https://github.com/uqfoundation/dill

    0 讨论(0)
提交回复
热议问题