serialize object in jython, read into python

P

py

I want to serialize an object in jython and then be able to read it in
using python, and vice versa.

Any suggestions on how to do this? pickle doesnt work, nor does using
ObjectOutputStream (from jython).

I prefer to do it file based ...something like

pickle.dump(someObj, open("output.txt", "w"))

as opposed to shelve

f = shelve.open("output.txt")
f['somedata'] = someObj

Thanks for the help in advance.
 
N

Noah

py said:
I want to serialize an object in jython and then be able to read it in
using python, and vice versa.

Any suggestions on how to do this? pickle doesnt work, nor does using
ObjectOutputStream (from jython).

I prefer to do it file based ...something like

pickle.dump(someObj, open("output.txt", "w"))

as opposed to shelve

f = shelve.open("output.txt")
f['somedata'] = someObj

Thanks for the help in advance.

You can give up on pickle, because pickle is only
guaranteed to work with the exact same version of the Python
interpreter.
(It will work on the same version of the Python interpreter on
different platforms, but
that's probably not useful to you here).

How complex of a serialization do you need?
Would simply saving a dictionary of strings work for you?
That's what I do for HTTP session management.
I then map my session dictionary to the dictionary of object
attributes.

You might also consider JSON which is very simple and lightweight.
http://www.json.org/

Yours,
Noah
 
P

py

Noah said:
You can give up on pickle, because pickle is only
guaranteed to work with the exact same version of the Python
interpreter.
:(


How complex of a serialization do you need?

simple

I just wrote the info out to a file. then i have a thread which reads
in the files and parses them and creates the necessary object.

thanks anyway
 
F

Fredrik Lundh

Noah said:
You can give up on pickle, because pickle is only guaranteed
to work with the exact same version of the Python interpreter.

nope.

maybe you're thinking of marshalled bytecode, but I don't think
that's what the OP was talking about.

</F>
 
P

Paul Rubin

py said:

No that's confusing pickle with marshal. Pickle is supposed to work
across versions, though it has recently grown a few slightly
incompatible optional modes.
 
E

Erik Max Francis

Noah said:
You can give up on pickle, because pickle is only
guaranteed to work with the exact same version of the Python
interpreter.

Not true. You're thinking of marshal.
 
K

Kent Johnson

py said:
I want to serialize an object in jython and then be able to read it in
using python, and vice versa.

Any suggestions on how to do this? pickle doesnt work, nor does using
ObjectOutputStream (from jython).

I prefer to do it file based ...something like

pickle.dump(someObj, open("output.txt", "w"))

as opposed to shelve

f = shelve.open("output.txt")
f['somedata'] = someObj

It works for me. I think the problem is that your pickle file is not closed properly, when
I tried it with the form you have above the file was never written. Here is an example
that works with Jython 2.1 and Python 2.4.2:

D:\WUTemp>jython
Jython 2.1 on java1.4.2_06 (JIT: null)
Type "copyright", "credits" or "license" for more information..... pass
....
>>> d=Foo()
>>> lst=[a,b,c,d]
>>> lst
['hello world', (0, 1, 2, 3, 4, 5, 6, 7, 8, 9), {'b': 2, 'a': 1, 'c': 3}, <__main__.Foo
instance at 17930334>]
D:\WUTemp>python
Python 2.4.2 (#67, Sep 28 2005, 12:41:11) [MSC v.1310 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information. ... pass
...['hello world', (0, 1, 2, 3, 4, 5, 6, 7, 8, 9), {'a': 1, 'c': 3, 'b': 2}, <__main__.Foo
instance at 0x00A51350>]
Kent
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,229
Latest member
GloryAngul

Latest Threads

Top