Simple Deserialization Question

22 views
Skip to first unread message

Jonathan D

unread,
Dec 12, 2011, 7:46:50 PM12/12/11
to pic...@googlegroups.com
Suppose I:

serialized_obj = serialization.serialize(foo, True)

How do deserialize this?  Forgive the possibly inane question, but I can't find a way to do this.  (That is, I cannot find a complementary function to deserialize once I set the needsPyCloudSerializer argument to True.) 

Aaron Staley

unread,
Dec 12, 2011, 9:43:34 PM12/12/11
to pic...@googlegroups.com
Hello,

It looks like you are talking about the internal cloud serialization module?

In general, we don't recommend using functions outside of the outer cloud package.  Is there any reason you are looking to use this module?

That said, anything created with this serialize function can be desialized with the regular Python pickle module (pickle.loads)

Regards,
Aaron Staley
PiCloud, Inc.

Jonathan D

unread,
Dec 12, 2011, 9:54:57 PM12/12/11
to pic...@googlegroups.com
> In general, we don't recommend using functions outside of the outer cloud package.  Is there any reason you are looking to use this module?

Well, I'm looking for a way to persist more types of objects than the pickle module currently supports.  Searching comp.lang.python for things akin to "pickle a function" or "marshal code" returned a few posts about the picloud client package. So I checked it out.

> That said, anything created with this serialize function can be desialized with the regular Python pickle module (pickle.loads)

I tried using pickle.loads, and then serialization.deserialize:

from cloud import serialization

class Foo(object):
  
  def __init__(self, prop):
    
    self.prop = prop
    
  def add(self, a, b):
    
    print lambda a, b: a * b
    
  @classmethod
  def hello(cls, name):
    
    print "Hello, {}".format(name)
    
  def outer(self, phrase):
    
    def inner():
      print phrase
      
    return inner
  
foo = Foo(1)

cs = serialization.serialize(foo, True)

with open("C:\\test\\cloudpickle.pkl", 'w') as f:
  f.write(cs)

with open("C:\\test\\cloudpickle.pkl", 'r') as f:
  data = ''.join(f.readlines())

cd = serialization.deserialize(data)

"""
RESULT:

Traceback (most recent call last):
  File "C:\Documents and Settings\jdobson\My Documents\Python\Json Tools\cloudtest.py", line 35, in <module>
    cd = serialization.deserialize(data)
  File "c:\python27\lib\site-packages\cloud-2.3.9-py2.7.egg\cloud\serialization\__init__.py", line 47, in deserialize
    return Deserializer(str).deserializedObj
  File "c:\python27\lib\site-packages\cloud-2.3.9-py2.7.egg\cloud\serialization\serializationhandlers.py", line 112, in
__init__
    self.deserializedObj = pickle.loads(str)
EOFError
"""

Aaron Staley

unread,
Dec 13, 2011, 1:18:34 AM12/13/11
to pic...@googlegroups.com
The pickle data is binary, so it might not play well with f.readlines.


try just doing:
with open("C:\\test\\cloudpickle.pkl", 'r') as f:
  data = 'f.read()

Jonathan D

unread,
Dec 13, 2011, 8:28:34 AM12/13/11
to pic...@googlegroups.com
I receive the same errors when using pickle.loads() or serialization.deserialize() on 'data', whether I f.readlines() or f.read().  
  
  serialization.deserialize() returns an EOF error
  pickle.loads() returns 'TypeError: ord() expected a character, but string of length 0 found'.

I assume the pyCloudSerializer is doing things that a regular pickle.loads() can't reverse/handle?

Aaron Staley

unread,
Dec 13, 2011, 5:56:47 PM12/13/11
to pic...@googlegroups.com
pyCloudSerializer is fully compatible with the regular pickler.  We use pickle.loads() internally to load results of our custom serializer.

Looking at your directory paths, are you in windows?  If so, try opening the files in binary mode - "wb" and "rb" rather than just "w" or "r"; sorry I didn't catch this earlier - I'm on Linux where there is no distinction.

Best,
Aaron

Jonathan D

unread,
Dec 14, 2011, 8:55:49 PM12/14/11
to pic...@googlegroups.com
Thanks, Aaron.  That was the problem all along.  I should have clued in when attempting to print the result of the serialization (bunch of gobble-dee-gook, with a beep or two.) :)  Thanks again.
Reply all
Reply to author
Forward
0 new messages