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

set_pixel() and get_pixel() methods #198

Closed
hx2A opened this issue Nov 23, 2022 · 12 comments
Closed

set_pixel() and get_pixel() methods #198

hx2A opened this issue Nov 23, 2022 · 12 comments
Assignees

Comments

@hx2A
Copy link
Collaborator

hx2A commented Nov 23, 2022

  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 #197

@hx2A
Copy link
Collaborator Author

hx2A commented Jan 18, 2023

@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
Copy link
Collaborator

get_pixels() ?

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

@hx2A
Copy link
Collaborator Author

hx2A commented Jan 18, 2023

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
Copy link
Collaborator

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

@hx2A
Copy link
Collaborator Author

hx2A commented Jan 23, 2023

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
Copy link
Collaborator Author

hx2A commented Jan 23, 2023

Here's a simple algo to provide the suggestions:

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

@villares
Copy link
Collaborator

villares commented Jan 23, 2023

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
Copy link
Collaborator Author

hx2A commented Jan 24, 2023

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
Copy link
Collaborator

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
Copy link
Collaborator Author

hx2A commented Jan 25, 2023

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
Copy link
Collaborator Author

hx2A commented Mar 19, 2023

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 hx2A self-assigned this Mar 19, 2023
@hx2A
Copy link
Collaborator Author

hx2A commented Mar 25, 2023

One unexpected challenge but this is now complete with #268

@hx2A hx2A closed this as completed Mar 25, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants