From 67ddc91a399325f536193b696cec307d9160a729 Mon Sep 17 00:00:00 2001 From: RustyC0der Date: Fri, 29 Apr 2022 21:36:40 +0300 Subject: [PATCH 1/2] add iterable obj processing as xs and ys arguments --- bashplotlib/scatterplot.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/bashplotlib/scatterplot.py b/bashplotlib/scatterplot.py index 76d20e4..9dc22fc 100644 --- a/bashplotlib/scatterplot.py +++ b/bashplotlib/scatterplot.py @@ -73,8 +73,10 @@ def plot_scatter(f, xs, ys, size, pch, colour, title): ys = [float(i[1]) for i in data] if len(data[0]) > 2: cs = [i[2].strip() for i in data] - elif isinstance(xs, list) and isinstance(ys, list): - pass + # try to convert any iterable data to list, so we can use any iterable object like pandas dataframe or numpy array + elif isiterable(xs) and isiterable(ys): + xs = [i for i in xs] + ys = [i for i in ys] else: with open(xs) as fh: xs = [float(str(row).strip()) for row in fh] From 1a53b2c6518978b2c1ab020103a157392e6a5218 Mon Sep 17 00:00:00 2001 From: RustyC0der Date: Fri, 29 Apr 2022 22:18:11 +0300 Subject: [PATCH 2/2] fix error with invalid path processing --- bashplotlib/scatterplot.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/bashplotlib/scatterplot.py b/bashplotlib/scatterplot.py index 9dc22fc..2523950 100644 --- a/bashplotlib/scatterplot.py +++ b/bashplotlib/scatterplot.py @@ -7,6 +7,7 @@ from __future__ import print_function import csv +import os import sys import optparse from .utils.helpers import * @@ -62,6 +63,7 @@ def plot_scatter(f, xs, ys, size, pch, colour, title): colour -- colour of the points title -- title of the plot """ + path_allowed_types = (str, bytes, os.PathLike) cs = None if f: if isinstance(f, str): @@ -74,14 +76,16 @@ def plot_scatter(f, xs, ys, size, pch, colour, title): if len(data[0]) > 2: cs = [i[2].strip() for i in data] # try to convert any iterable data to list, so we can use any iterable object like pandas dataframe or numpy array - elif isiterable(xs) and isiterable(ys): - xs = [i for i in xs] - ys = [i for i in ys] - else: + elif type(xs) in path_allowed_types and type(ys) in path_allowed_types: with open(xs) as fh: xs = [float(str(row).strip()) for row in fh] with open(ys) as fh: ys = [float(str(row).strip()) for row in fh] + elif isiterable(xs) and isiterable(ys): + xs = [i for i in xs] + ys = [i for i in ys] + else: + raise ValueError("Invalid data types {} or {} must be iterable, str, or pathlike".format(type(xs), type(ys))) _plot_scatter(xs, ys, size, pch, colour, title, cs)