By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,187 Members | 1,030 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,187 IT Pros & Developers. It's quick & easy.

serialize object in jython, read into python

P: n/a
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.

Dec 22 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
py wrote:
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

Dec 22 '05 #2

P: n/a
py
Noah wrote:
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

Dec 22 '05 #3

P: n/a
"py" <co*******@gmail.com> writes:
Noah wrote:
You can give up on pickle, because pickle is only
guaranteed to work with the exact same version of the Python
interpreter.


:(


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.
Dec 22 '05 #4

P: n/a
"Noah" <no**@noah.org> wrote:
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.


nope.

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

</F>

Dec 22 '05 #5

P: n/a
Noah wrote:
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.

--
Erik Max Francis && ma*@alcyone.com && http://www.alcyone.com/max/
San Jose, CA, USA && 37 20 N 121 53 W && AIM erikmaxfrancis
Who knows whether any of us will be around in 1972?
-- John F. Kennedy
Dec 22 '05 #6

P: n/a
py wrote:
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.
a='hello world'
b=tuple(range(10))
c={ 'a':1, 'b':2, 'c':3}
class Foo: .... 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>] f=open('pickle.txt', 'wb')
import pickle
pickle.dump(lst, f)
f.close()
^Z
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. import pickle
class Foo: ... pass
... f=open('pickle.txt')
lst = pickle.load(f)
f.close()
lst ['hello world', (0, 1, 2, 3, 4, 5, 6, 7, 8, 9), {'a': 1, 'c': 3, 'b': 2}, <__main__.Foo
instance at 0x00A51350>]


Kent
Dec 23 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.