MBravoS / splotch

Simple PLOTs, Contours and Histograms is a small package with wrapper functions designed to simplify plotting calls from matplotlib.
BSD 3-Clause "New" or "Revised" License
5 stars 0 forks source link

Duck-typing instead of using `isinstance` #71

Open MBravoS opened 1 year ago

MBravoS commented 1 year ago

As mentioned in #70, we should make sure that our code is as Pythonic as possible, which means checking that we are using duck-typing instead of isinstance (or hasattr) whenever possible.

AstroRobin commented 1 year ago

I think this applies only to the case of checking for iterables, using isinstance for checking most other types would still be considered pythonic.

AstroRobin commented 1 year ago

I think this issue comes up in the following code snippet for contourp

if not isinstance(percent, ndarray):
        percent = array([percent]).flatten()

if not isinstance(bin_type, (list, tuple, ndarray)):
        bin_type = [bin_type] * 2

if not isinstance(bins, (list, tuple)):
        if bins is None:
                bins = max([10, int(len(x)**0.4)])  # Defaults to min of 10 bins
        bins = [bins] * 2

Somehow, we are checking whether percent, bin_type and bins are list-like, but not even in the same way each time. Did we decide how we would rewrite this.

I've added the is_listlike() function in base_func.py, which checks whether a variable is iterable (but not a string). I've Implemented this list-like check in contourp() with the latest push here: https://github.com/MBravoS/splotch/commit/0a27e464e1d814428dd4406eb93078ca75914da6