py5coding / py5generator

Meta-programming project that creates the py5 library code.
https://py5coding.org/
GNU General Public License v3.0
52 stars 13 forks source link

`set_pixel()` and `get_pixel()` methods #198

Closed hx2A closed 1 year ago

hx2A commented 1 year ago
  I know now we have advanced NumPy arrays of pixels and all, but the `get()`  function looks a bit lonely now.... 

http://py5coding.org/reference/sketch_get.html?highlight=get

I know it is highly inefficient comparing to access to pixels[] and np_pixels[] but maybe it should be the "entry level" way of doing old set() as set_pixel().

Maybe get() should be called get_pixel()?

Originally posted by @villares in https://github.com/py5coding/py5generator/discussions/197

hx2A commented 1 year ago

@villares, I just noticed that get() has several signatures, one of which returns a Py5Image object:

get(
    x: int,  # x-coordinate of the pixel
    y: int,  # y-coordinate of the pixel
    w: int,  # width of pixel rectangle to get
    h: int,  # height of pixel rectangle to get
    /,
) -> Py5Image

I don't think we can call it get_pixel() if it can return a grid of pixels. However, I do agree with your questioning why there is a get() method but no set() method. Can you think of something else to rename get() to besides get_pixel() that will resolve this?

villares commented 1 year ago

get_pixels() ?

Would it be ugly to have also have a get_pixel version that can only get a single pixel?

hx2A commented 1 year ago

So, splitting it into a get_pixel() and get_pixels()? I thought of that also but I thought it was ugly, and I was hoping you'd think of something more elegant. :)

Perhaps we could have just one method called get_pixels() because it can return pixels plural but if you give it the coordinates of just one pixel, you get just one. I can be comfortable with that. Two of the three get() signatures return Py5Image objects, so giving it a plural name is the better way to go.

villares commented 1 year ago

I think get_pixels() would be fine. Could you add to the NameError error message a hint in case someone tries get_pixel()?

hx2A commented 1 year ago

I wouldn't want to write a code for a special case like that but your suggestion inspires a different idea. How about this:

When I use git and mistype a command, it will guess at what I meant.

$ git stutas
git: 'stutas' is not a git command. See 'git --help'.

The most similar command is
        status
(py5) 21:25 jim@main ~/Projects/ITP/pythonprocessing/py5generator:$ git status
On branch splitsketch
Your branch is up to date with 'origin/splitsketch'.

nothing to commit, working tree clean

py5 could do this also. If my code includes py5.collor(100, 200, 300) it currently responds with AttributeError: py5 has no function or field named collor. What if it instead said it said AttributeError: py5 has no function or field named collor. Did you mean to type "color"?

This would be more general and would certainly cover the get_pixel() => get_pixels() case.

hx2A commented 1 year ago

Here's a simple algo to provide the suggestions:

http://norvig.com/spell-correct.html

villares commented 1 year ago

This is more or less what has been done on recent Python error messages improvements!

Have you heard of this on Python 3.10 onward? https://docs.python.org/3.10/whatsnew/3.10.html#attributeerrors

Beware you might be missing it, and you wouldn't want to reinvent it from scratch!

hx2A commented 1 year ago

No, I hadn't heard of that feature. That is useful! Thanks for the suggestion. The docs say it doesn't get used for IPython or Jupyter though, so it wouldn't cover all the necessary cases. Also, py5 currently requires 3.8, and I am not going to bump that to 3.10 just yet. I would rather wait until whenever jpype starts increasing their min version number.

villares commented 1 year ago

I'm running py5 on 3.10 mostly. Maybe you could check the Python version and call PyErr_Display() if it is available, or consume its output internally if it is available, maybe?

hx2A commented 1 year ago

Right, that's one way to do it. It is certainly a good feature to explore.

I will create a new issue for this new feature so I can track it separately from set_pixel() and get_pixel().

hx2A commented 1 year ago

I started working on this. It's pretty straightforward but I need to duplicate what I do here for the set and get methods in Py5Graphics and Py5Image.

hx2A commented 1 year ago

One unexpected challenge but this is now complete with #268