pickle

Simple, hassle-free, zero-boilerplate serialization in Scala/Java similar to Python's Pickle?

ⅰ亾dé卋堺 提交于 2019-12-31 08:29:34
问题 Is there a simple, hassle-free approach to serialization in Scala/Java that's similar to Python's pickle? Pickle is a dead-simple solution that's reasonably efficient in space and time (i.e. not abysmal) but doesn't care about cross-language accessibility, versioning, etc. and allows for optional customization. What I'm aware of: Java's built-in serialization is infamously slow ([1], [2]), bloated, and fragile. Also have to mark classes as Serializable---annoying when there are things that

Pickling a staticmethod in Python

喜欢而已 提交于 2019-12-30 11:15:15
问题 I've been trying to pickle an object which contains references to static class methods. Pickle fails (for example on module.MyClass.foo ) stating it cannot be pickled, as module.foo does not exist. I have come up with the following solution, using a wrapper object to locate the function upon invocation, saving the container class and function name: class PicklableStaticMethod(object): """Picklable version of a static method. Typical usage: class MyClass: @staticmethod def doit(): print "done"

Why dill dumps external classes by reference, no matter what?

你说的曾经没有我的故事 提交于 2019-12-30 10:36:40
问题 In the example below, I have placed the class Foo inside its own module foo . Why is the external class dumped by ref? The instance ff is not being dumped with its source code. I am using Python 3.4.3 and dill-0.2.4. import dill import foo class Foo: y = 1 def bar( self, x ): return x + y f = Foo() ff = foo.Foo() print( dill.dumps( f, byref=False, recurse=True ) ) print( '\n' ) print( dill.dumps( ff, byref=False, recurse=True ) ) Well, the code above is actually wrong (should be Foo.y ,

Persisting hashlib state

一个人想着一个人 提交于 2019-12-30 10:13:07
问题 I'd like to create a hashlib instance, update() it, then persist its state in some way. Later, I'd like to recreate the object using this state data, and continue to update() it. Finally, I'd like to get the hexdigest() of the total cumulative run of data. State persistence has to survive across multiple runs. Example: import hashlib m = hashlib.sha1() m.update('one') m.update('two') # somehow, persist the state of m here #later, possibly in another process # recreate m from the persisted

Python: can't pickle module objects error

ぐ巨炮叔叔 提交于 2019-12-30 08:09:10
问题 I'm trying to pickle a big class and getting TypeError: can't pickle module objects despite looking around the web, I can't exactly figure out what this means. and I'm not sure which module object is causing the trouble. is there a way to find the culprit? the stack trace doesn't seem to indicate anything. 回答1: I can reproduce the error message this way: import cPickle class Foo(object): def __init__(self): self.mod=cPickle foo=Foo() with file('/tmp/test.out', 'w') as f: cPickle.dump(foo, f)

Pickling weakref in Python

我的未来我决定 提交于 2019-12-30 06:31:08
问题 I am still pretty new to Python and even newer to pickling. I have a class Vertex(ScatterLayout) with a __getnewargs__(): def __getnewargs__(self): return (self.pos, self.size, self.idea.text) My understanding is that this will cause the pickle to pickle the object from __getnewargs__() rather than the object's dictionary. The pickle is called in the following method (in a different class MindMapApp(App) ): def save(self): vertices = self.mindmap.get_vertices() edges = self.mindmap.get_edges(

Pickling weakref in Python

China☆狼群 提交于 2019-12-30 06:30:27
问题 I am still pretty new to Python and even newer to pickling. I have a class Vertex(ScatterLayout) with a __getnewargs__(): def __getnewargs__(self): return (self.pos, self.size, self.idea.text) My understanding is that this will cause the pickle to pickle the object from __getnewargs__() rather than the object's dictionary. The pickle is called in the following method (in a different class MindMapApp(App) ): def save(self): vertices = self.mindmap.get_vertices() edges = self.mindmap.get_edges(

Serialize a python function with dependencies

核能气质少年 提交于 2019-12-30 03:15:07
问题 I have tried multiple approaches to pickle a python function with dependencies, following many recommendations on StackOverflow, (such as dill, cloudpickle, etc.) but all seem to run into a fundamental issue that I cannot figure out. I have a main module that tries to pickle a function from an imported module, sends it over ssh to be unpickled and executed at a remote machine. So main has: import dill (for example) import modulea serial=dill.dumps( modulea.func ) send (serial) On the remote

Do Pickle and Dill have similar levels of risk of containing malicious script?

丶灬走出姿态 提交于 2019-12-29 09:32:12
问题 Dill is obviously a very useful module, and it seems as long as you manage the files carefully it is relatively safe. But I was put off by the statement: Thus dill is not intended to be secure against erroneously or maliciously constructed data. It is left to the user to decide whether the data they unpickle is from a trustworthy source. I read in in https://pypi.python.org/pypi/dill. It's left to the user to decide how to manage their files. If I understand correctly, once it has been

problem with pickle and tkinter

不打扰是莪最后的温柔 提交于 2019-12-29 09:15:34
问题 To learn tkinter I'm making a simple Go game program. I now would like to be able to save a game using pickle, but when I try to pickle my GoBoardModel object I get: PicklingError: Can't pickle 'tkapp' object: <tkapp object at 0x01FCB090> I guess that comes from the fact that while the GUI and the model of the go board are quite well separated, the model still has a reference to the view in order to push some stuff, so pickle probably tries to pickle some tk stuff. Of course I would like to