473,397 Members | 1,985 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

serialize object in jython, read into python

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
6 2146
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
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
"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
"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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Michael Chermside | last post by:
Ype writes: > For the namespaces in Jython this 'Python internal thread safety' > is handled by the Java class: > > http://www.jython.org/docs/javadoc/org/python/core/PyStringMap.html > > which...
4
by: angel | last post by:
A java runtime environment includes jvm and java class (for example classes.zip in sun jre). Of course jython need jvm,but does it need java class. Thanx
3
by: Jim Hargrave | last post by:
I've read that it is possible to compile jython to native code using GCJ. PyLucene uses this approach, they then use SWIG to create a Python wrapper around the natively compiled (java) Lucene. Has...
1
by: Gregor Stich | last post by:
Dear all, I hope my question is here in the right place... What I want to achieve is a communication between Java and Python. We have a pretty strong framework of existing python scripts and...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.