serialize object in jython, read into python

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • py

    serialize object in jython, read into python

    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
    ObjectOutputStr eam (from jython).

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

    pickle.dump(som eObj, open("output.tx t", "w"))

    as opposed to shelve

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

    Thanks for the help in advance.

  • Noah

    #2
    Re: serialize object in jython, read into python

    py wrote:[color=blue]
    > 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
    > ObjectOutputStr eam (from jython).
    >
    > I prefer to do it file based ...something like
    >
    > pickle.dump(som eObj, open("output.tx t", "w"))
    >
    > as opposed to shelve
    >
    > f = shelve.open("ou tput.txt")
    > f['somedata'] = someObj
    >
    > Thanks for the help in advance.[/color]

    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.


    Yours,
    Noah

    Comment

    • py

      #3
      Re: serialize object in jython, read into python

      Noah wrote:[color=blue]
      > You can give up on pickle, because pickle is only
      > guaranteed to work with the exact same version of the Python
      > interpreter.[/color]

      :(

      [color=blue]
      > How complex of a serialization do you need?[/color]

      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

      Comment

      • Paul Rubin

        #4
        Re: serialize object in jython, read into python

        "py" <codecraig@gmai l.com> writes:[color=blue]
        > Noah wrote:[color=green]
        > > You can give up on pickle, because pickle is only
        > > guaranteed to work with the exact same version of the Python
        > > interpreter.[/color]
        >
        > :([/color]

        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.

        Comment

        • Fredrik Lundh

          #5
          Re: serialize object in jython, read into python

          "Noah" <noah@noah.or g> wrote:
          [color=blue][color=green]
          > > Thanks for the help in advance.[/color]
          >
          > You can give up on pickle, because pickle is only guaranteed
          > to work with the exact same version of the Python interpreter.[/color]

          nope.

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

          </F>



          Comment

          • Erik Max Francis

            #6
            Re: serialize object in jython, read into python

            Noah wrote:
            [color=blue]
            > You can give up on pickle, because pickle is only
            > guaranteed to work with the exact same version of the Python
            > interpreter.[/color]

            Not true. You're thinking of marshal.

            --
            Erik Max Francis && max@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

            Comment

            • Kent Johnson

              #7
              Re: serialize object in jython, read into python

              py wrote:[color=blue]
              > 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
              > ObjectOutputStr eam (from jython).
              >
              > I prefer to do it file based ...something like
              >
              > pickle.dump(som eObj, open("output.tx t", "w"))
              >
              > as opposed to shelve
              >
              > f = shelve.open("ou tput.txt")
              > f['somedata'] = someObj[/color]

              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>jytho n
              Jython 2.1 on java1.4.2_06 (JIT: null)
              Type "copyright" , "credits" or "license" for more information.[color=blue][color=green][color=darkred]
              >>> a='hello world'
              >>> b=tuple(range(1 0))
              >>> c={ 'a':1, 'b':2, 'c':3}
              >>> class Foo:[/color][/color][/color]
              .... pass
              ....[color=blue][color=green][color=darkred]
              >>> d=Foo()
              >>> lst=[a,b,c,d]
              >>> lst[/color][/color][/color]
              ['hello world', (0, 1, 2, 3, 4, 5, 6, 7, 8, 9), {'b': 2, 'a': 1, 'c': 3}, <__main__.Foo
              instance at 17930334>][color=blue][color=green][color=darkred]
              >>> f=open('pickle. txt', 'wb')
              >>> import pickle
              >>> pickle.dump(lst , f)
              >>> f.close()
              >>> ^Z[/color][/color][/color]

              D:\WUTemp>pytho n
              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.[color=blue][color=green][color=darkred]
              >>> import pickle
              >>> class Foo:[/color][/color][/color]
              ... pass
              ...[color=blue][color=green][color=darkred]
              >>> f=open('pickle. txt')
              >>> lst = pickle.load(f)
              >>> f.close()
              >>> lst[/color][/color][/color]
              ['hello world', (0, 1, 2, 3, 4, 5, 6, 7, 8, 9), {'a': 1, 'c': 3, 'b': 2}, <__main__.Foo
              instance at 0x00A51350>][color=blue][color=green][color=darkred]
              >>>[/color][/color][/color]

              Kent

              Comment

              Working...