Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement load_strings(), save_strings(), load_bytes(), etc. and all the other relevant IO methods #164

Closed
hx2A opened this issue Sep 24, 2022 · 8 comments
Labels
enhancement New feature or request

Comments

@hx2A
Copy link
Collaborator

hx2A commented Sep 24, 2022

Methods for load_strings(), save_strings(), load_bytes(), etc. would be useful to beginners.

@hx2A hx2A added the enhancement New feature or request label Sep 24, 2022
@hx2A
Copy link
Collaborator Author

hx2A commented Sep 30, 2022

I've written the code for load_strings(), save_strings(), load_bytes(), and save_bytes(). Will write the reference docs next.

@hx2A
Copy link
Collaborator Author

hx2A commented Oct 10, 2022

Almost done with this. Today I added load and save methods for pickle files, which is a nice Python-specific addition.

@hx2A
Copy link
Collaborator Author

hx2A commented Oct 12, 2022

This was closed with #174

@hx2A hx2A closed this as completed Oct 12, 2022
@villares
Copy link
Collaborator

This is wonderful.
About the pickle feature. I remember it was frustrating not to be able to pickle PImage, can we pickle Py5Image objects now?

@hx2A
Copy link
Collaborator Author

hx2A commented Oct 13, 2022

No py5 objects can be pickled, just like file handles, sockets, and several other things cannot be pickled.

https://docs.python.org/3/library/pickle.html#what-can-be-pickled-and-unpickled

Consider that py5 objects are linked to Processing Java objects that live in the JVM. Pickle has no way of recreating those Java objects in the JVM, and even if it did, what would happen if the JVM wasn't running at that time? Also, All py5 objects like Py5Image are "linked" to the parent Sketch that created them and it can possibly cause weird exceptions if a Py5Image were to be used with a different Sketch than the one that created it. However, it wouldn't be that hard to "re-parent" them to point to a different Sketch, but this isn't how Processing is designed to be used.

I remember it was frustrating not to be able to pickle PImage, can we pickle Py5Image objects now?

Nevertheless, I do want to understand this frustration and your needs here. Why not just save Py5Image objects to a png file? That works just fine. Perhaps you have an object or data structure that contained Py5Image objects or other py5 objects and you wanted to pickle the whole thing to one pickle file. In that case, the pickle library would throw an exception when it got to a py5 object. I believe it is possible to write code that controls how pickle will serialize certain objects, so maybe we could write some code like this to convert the Py5Image object to a PIL image first, and then pickle that instead. When reading the pickle file, it could convert the PIL image back to a Py5Image object (assuming the JVM is running and this is being done with py5, otherwise you would get a PIL image, which would be the next best thing). This would be achievable, but I don't see an analogous approach for other py5 objects.

If this Py5Image to PIL Image idea is useful, we can add it to the list of potential enhancements.

@villares
Copy link
Collaborator

villares commented Oct 13, 2022 via email

@hx2A
Copy link
Collaborator Author

hx2A commented Oct 13, 2022

Hmmm, you could create a Sketch that had a separate "state" object and then pickled that. The state object could contain PIL Images if need be. You can't Pickle an entire py5 Sketch though. Whatever is relevant to the state you wish to recreate could be added the pickle-able state object.

@hx2A
Copy link
Collaborator Author

hx2A commented Oct 13, 2022

I just remembered that Java does support serialization of Java objects:

https://www.baeldung.com/java-serialization

Since Python allows you to customize how pickling is done, it would be possible to add code to serialize the Java objects to bytes and then add those bytes to the pickle file. Then more custom code could be used when the pickle file is read back, to convert the bytes back to objects.

So theoretically, any py5 object could be pickled, but making it do so would be non-trivial amount of work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants