Hey, hello all.
I’m new here; let me know if this isn’t the correct location for this question and I’ll move or remove it.
I’m interfacing with mpl
from geopandas
. The issue I’m working right now requires typechecking, and I’m wondering if you have something that could help me.
Situation is the following: we have a list-like object, and we need to check if this is, e.g., a valid color, or rather a list of colors. For this, we from matplotlib.colors import is_color_like
, which makes sure we don’t mistake a single rgb-tuple for a list. This works perfectly, no issues there.
There are, however, other plotting attributes which may also appear list-like. For example, linestyle
which may be a tuple. As far as I can see, there is no dedicated is_linestyle_like
function.
So, my questions:
(a) is there a preferred way of checking if a value is a valid single value for a certain style argument? I’m thinking of something like
def is_valid_value(attr:str, value) -> bool
which can be called like so:
is_valid_value('linewidth', '--') #True
is_valid_value('linewidth', (0, (1, 1)))) #True
is_valid_value('linewidth', [0,1,2,3]) #False
is_valid_value('edgecolor', 'red') #True
(b) Assuming such a thing does not exist, is there a try/except
way to catch incorrect values by trying them out? Without actually creating the plotting objects, which is what I do here:
from matplotlib.lines import Line2D
l = Line2D([0],[0],linestyle=[0,1,2,3]) #ValueError
Many thanks!