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

Type hints or wrappers for some of Processing libraries? #51

Open
arseniiv opened this issue Dec 5, 2021 · 4 comments
Open

Type hints or wrappers for some of Processing libraries? #51

arseniiv opened this issue Dec 5, 2021 · 4 comments
Labels
enhancement New feature or request

Comments

@arseniiv
Copy link

arseniiv commented Dec 5, 2021

Hi! 🎈 While tinkering with this beautiful thing and Processing Sound library I thought it should be not too hard to add some support for APIs of major libraries like this one. How do I proceed?

I reckon it should be possible to just write a stub (for this example, named processing.sound.pyi or maybe just sound.pyi in a folder processing) and it would work? I hadn’t tried to write stubs yet though I type-hint my own code quite extensively.

Probably better would be to have wrappers to get rid of JVM types where Python types (and numpy arrays etc.) are more homey, how you did here for the core itself; but I don’t know if it’s easy or not. What could I do to help?

@hx2A
Copy link
Collaborator

hx2A commented Dec 6, 2021

Welcome! Thank you for giving py5 a try.

I thought it should be not too hard to add some support for APIs of major libraries like this one. How do I proceed?

It depends how far you want to take it. I would first recommend learning about jpype to see how much it accomplishes for you for free. It already can do some conversion between numpy arrays and java arrays, and you can program implicit type conversion to customize how things should be converted.

https://jpype.readthedocs.io/en/latest/

With jpype, you will be able to import the Processing Sound classes and interact with them as if they are regular Python classes. When you work with jpype through IPython it will have some typehints provided for you. I believe it works well with Jedi (mentioned in the docs).

I've had no luck writing *.pyi files, they never seemed to work for me. All of py5's type hints are embedded in the *.py files.

At some point I need to document how py5's meta-programming works, but I'm already far behind on the py5 documentation as it is. Let me at least give you an overview if you want to take a look. All of the relevant code for this is in the generate_py5.py file and the generate directory. The javap.py file does inspection of the code using javap. After that, the most important code file is codebuilder, which compiles the data in the templates, found in py5_resources/py5_module. The whole thing is like a custom templating engine for generating python code. Critically important are the data files in py5_resources/data, which tells it what methods to include and what to skip. Those also give the ability to add customizations to specific methods. Getting 90% of py5 working is easy; the remaining 10% had all kinds of issues and headaches.

I haven't used the Sound library before but do want to include some kind of support for sound some day. Looking at the docs, I see a ton of little classes. How familiar are you with the Sound library source code?

@hx2A
Copy link
Collaborator

hx2A commented Dec 6, 2021

Also note that none of py5 was built on the first pass. I somehow bootstrapped myself into this after multiple passes and re-writes. For example, those data files were first created with Python code that is no longer part of the framework. By far the greatest amount of time has been spent in py5_docs. The *.txt files form the basis for the docstrings and the website reference documentation. Parts of those text files such as the signatures are maintained by the Python code and get updated automatically if method signatures get added or removed from the Processing jars.

@arseniiv
Copy link
Author

arseniiv commented Dec 7, 2021

Thanks! I guess I need time to digest it all…

With jpype, you will be able to import the Processing Sound classes and interact with them as if they are regular Python classes.

Yeah, I already made a test spectrum visualizer using them, and it was seamless, though without static code completion. Haven’t try in a notebook.

@hx2A
Copy link
Collaborator

hx2A commented Dec 8, 2021

Yeah, I already made a test spectrum visualizer using them, and it was seamless, though without static code completion. Haven’t try in a notebook.

Here is the jpype documentation on code completion:

https://jpype.readthedocs.io/en/latest/userguide.html#code-completion

Please let me know how it goes!

@hx2A hx2A added the enhancement New feature or request label Jan 10, 2022
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