Save non-pickleable variable?

ROGER GRAYDON CHRISTMAN dvl at psu.edu
Sat Oct 21 14:29:58 EDT 2017


On Sat, Oct 21, 2017, Israel Brewster wrote: >
tldr: I have an object that can't be picked. Is there any way to do a
>"raw" dump of the binary data to a file, and re-load it later?
>
<snip>
>Details: I am using a java (I know, I know - this is a python list. I'm
>not asking about the java - honest!) library (Jasper Reports)
>that I access from python using py4j (www.py4j.org
><http://www.py4j.org/>). At one point in my code I call a java
>function which, after churning on some data in a database, returns an object
>(a jasper report object populated with the final report data) that I
>can use (via another java call) to display the results in a variety
>of formats (HTML, PDF, XLS, etc). At the time I get the object back,
>I use it to display the results in HTML format for quick display, but the user
>may or may not also want to get a PDF copy in the near future. 
>
>Since it can take some time to generate this object, and also since the data
>may change between when I do the HTML display and when the user requests a PDF
>(if they do at all), I would like to save this object for potential
>future re-use. Because it might be large, and there is actually a fairly good
>chance the user won't need it again, I'd like to save it in a temp file
>(tat would be deleted when the user logs out) rather than in memory.
>Unfortunately, since this is an object created by and returned from a java
>function, not a native python object, it is not able to be pickled (as the
>suggestion typically is), at least to my knowledge.
>
>Given that, is there any way I can write out the "raw" binary data to
>a file, and read it back in later? Or some other way to be able to save this
>object? It is theoretically possible that I could do it on the java side, i.e.
>the library may have some way of writing out the file, but obviously I wouldn't
>expect anyone here to know anything about that - I'm just asking about the
>python side :-)
>
>

Since you suggest a file is a suitable medium, perhaps you can try the JSON
format
I have heard about (but never actually used myself).   Apparently there is
module
support for both Java and Python for this format, which is considered to be
language
independent.

And since the Python view of this data would be a Python data structure,
it should also be pickleable.

The "Json.org" web site should give you a place to download the modules
you would need for each of the two languages.  Hope this helps.

Roger Christman
Pennsylvania State University





More information about the Python-list mailing list