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

How to deal with `vertices()`, and other functions that expect collections, when arguments provided are empty? #195

Closed villares closed 1 year ago

villares commented 1 year ago

According to the Zen o Python, Errors should never pass silently.\Unless explicitly silenced.

How could we deal with the errors that occur when passing empty collections to the drawing functions that expect collections? The for loop that it replaces would have been just skipped...

Should we check for empty collections ourselves (look before you jump), use exception handling (feels overkill)? Should it emit a warning only? Should we have a a flag that we turn off to silence those errors, like rect_mode(CENTER) ?

def draw_elements(element):
    if isinstance(element, (MultiPolygon, GeometryCollection)):
        for p in element.geoms:
            draw_elements(p)
    elif isinstance(element, Polygon):
        with begin_closed_shape():
            #if element.exterior.coords:
                vertices(element.exterior.coords)
                for hole in element.interiors:
                    with begin_contour():
                        vertices(hole.coords) 

image

claudefalbriard commented 1 year ago

This might help: if element.exterior.coords is not None:

villares commented 1 year ago

This might help: if element.exterior.coords is not None:

I think this might not work, because an empty collection is not None. But an empty collection is falsy. So if el.exterior.coords: works! My more general questions is, should py5 check for us?

hx2A commented 1 year ago

Welcome, @claudefalbriard !

This might be a bug or shortcoming in py5. @villares , can you run that code again using prune_tracebacks(False) and then post the exception?

https://py5coding.org/reference/py5functions_prune_tracebacks.html

py5 will remove some of the java stuff and other complexity from stack traces to provide better errors to users. That can be deactivated though. For diagnosing this particular error, some relevant information is being lost.

Normally list(element.exterior.coords) gives you a list of lists, ie [[1, 2], [3, 4], ...]. When the error takes place, is it [] or [[]]?

py5 should not be presenting the user with a java exception, so this is a bug of some kind. I think calling vertices() with zero vertices should be allowed. Internally the code needs to convert the coordinates to a java array, and it could be that java array has the wrong dimensions and that is what is causing the error.

hx2A commented 1 year ago

I just looked at the relevant code and I see the error. This will be fixed in the next release.

hx2A commented 1 year ago

Fixed! On a roll today fixing bugs.