This is a problem which I suspect is common, but I haven't found a solution for it. What I want is quite simple, and seemingly technically feasible: I have a simple python class, and I want to store it on disc, instance and definition, in a single file. Pickle will store the data, but it doesn't store the class definition. One might argue that the class definition is already stored in my .py file, but I don't want a separate .py file; my goal is to have a self-contained single file that I could pop back into my namespace with a single line of code.
So yes, I know this possible using two files and two lines of code, but I want it in one file and one line of code. The reason why is because I often find myself in this situation; I'm working on some big dataset, manipulating it in python, and then having to write my sliced, diced and transformed data back into some preexisting directory structure. What I don't want is to litter these data-directories with ill-named python class stubs to keep my code and data associated, and what I want even less is the hassle of keeping track of and organizing all these little ad hoc classes defined on the fly in a script independently.
So the convenience isn't so much in code readability, but in effortless and unfudgable association between code and data. That seems like a worthy goal to me, even though I understand it isn't appropriate in most situations.
So the question is: Is there a package or code snippet that does such a thing, because I can't seem to find any.
Pickle can't pickle python code, so I don't think this is possible at all with pickle.
If you use
dill
, it enables you to treat__main__
as if it were a python module (for the most part). Hence, you can serialize interactively defined classes, and the like.dill
also (by default) can transport the class definition as part of the pickle.Then shut down the interpreter, and send the file
test.pkl
over TCP. On your remote machine, now you can get the class instance.But how to get the class definition? So this is not exactly what you wanted. The following is, however.
Then after sending the file… you can get the class definition.
Since you were looking for a one liner, I can do better. I didn't show you can send over the class and the instance at the same time, and maybe that's what you were wanting.
It's still not a single line, however, it works.
So, within
dill
, there'sdill.source
, and that has methods that can detect dependencies of functions and classes, and take them along with the pickle (for the most part).So that's not "perfect" (or maybe not what's expected)… but it does serialize the code for a dynamically built method and it's dependencies. You just don't get the rest of the class -- but the rest of the class is not needed in this case. Still, it doesn't seem like what you wanted.
If you wanted to get everything, you could just pickle the entire session. And in one line (two counting the
import
).Then on the remote machine...
Lastly, if you want the transport to be "done" for you transparently (instead of using a file), you could use
pathos.pp
orppft
, which provide the ability to ship objects to a second python server (on a remote machine) or python process. They usedill
under the hood, and just pass the code across the wire.The
servers
argument is optional, and here is just connecting to the local machine on port1234
… but if you use the remote machine name and port instead (or as well), you'll fire off to the remote machine -- "effortlessly".Get
dill
,pathos
, andppft
here: https://github.com/uqfoundation