From e5a4d5bd65765b549d68fc16643f90726c3c1df4 Mon Sep 17 00:00:00 2001 From: "Mayr, Hannes" <hannes.mayr@stud.tu-darmstadt.de> Date: Tue, 27 Sep 2022 11:38:35 +0200 Subject: [PATCH] Fix bug in validate_list func which didnt overwrite self.figure_ids so that publish iterated over a string instead of a list. --- plotid/plotoptions.py | 55 ++++++++++++++++++++++++++++++++++++++++- plotid/publish.py | 57 ++++--------------------------------------- plotid/save_plot.py | 2 +- tests/test_publish.py | 5 +++- 4 files changed, 64 insertions(+), 55 deletions(-) diff --git a/plotid/plotoptions.py b/plotid/plotoptions.py index 41e4ba8..c17b66c 100644 --- a/plotid/plotoptions.py +++ b/plotid/plotoptions.py @@ -2,6 +2,8 @@ # -*- coding: utf-8 -*- """Contains the PlotOptions and PlotIDTransfer classes.""" +import os + class PlotOptions: """ @@ -10,7 +12,10 @@ class PlotOptions: Methods ------- __init__ - validate_input : Check if input is correct type. + validate_input + Check if input is correct type. + validate_list + Check if all elements of a given list are of certain type. Attributes ---------- @@ -100,7 +105,55 @@ class PlotIDTransfer: def __init__(self, figs, figure_ids): self.figs = figs self.figure_ids = figure_ids + self.figure_ids = validate_list(self.figure_ids) def __str__(self): """Representation if an object of this class is printed.""" return str(self.__class__) + ": " + str(self.__dict__) + + +def validate_list(list_var, elt_type=str, is_file=False): + """ + Validate if contents of a list are of specific type. + + Parameters + ---------- + list_var : list or str + List or single string which contents will be validated. + elt_type : datatype, optional + Datatype of which the list elements must be type of. Otherwise + an Error will be raised. The default is str. + is_file : boolean, optional + Flag to indicate if the list contains paths to files. If True the + strings will be checked if they correspond to an existing file. + The default is False. + + Raises + ------ + TypeError + If one of the list elements is not of type elt_type. + FileNotFoundError + If strings are also checked for existing files and one of the files + does not exist. + + Returns + ------- + list_var as list + + """ + if isinstance(list_var, elt_type): + list_var = [list_var] + if isinstance(list_var, list): + for elt in list_var: + if not isinstance(elt, elt_type): + raise TypeError(f'The list of {list_var} contains an ' + f'object which is not of type {elt_type}.') + if is_file: + # Check if directory and files exist + if not os.path.exists(elt): + raise FileNotFoundError('The specified directory' + f'/file {elt} does not exist.') + else: + raise TypeError(f'The specified {list_var} are neither a ' + f'{elt_type} nor a list of {elt_type}.') + return list_var diff --git a/plotid/publish.py b/plotid/publish.py index d4d830a..c99a748 100644 --- a/plotid/publish.py +++ b/plotid/publish.py @@ -17,7 +17,7 @@ import shutil import sys import warnings from plotid.save_plot import save_plot -from plotid.plotoptions import PlotIDTransfer +from plotid.plotoptions import PlotIDTransfer, validate_list class PublishOptions: @@ -29,8 +29,6 @@ class PublishOptions: __init__ validate_input Check if input is correct type. - validate_list - Check if all elements of a given list are of certain type. export Export the plot and copy specified files to the destiantion folder. """ @@ -53,51 +51,6 @@ class PublishOptions: """Representation if an object of this class is printed.""" return str(self.__class__) + ": " + str(self.__dict__) - def validate_list(self, list_var, elt_type=str, is_file=False): - """ - Validate if contents of a list are of specific type. - - Parameters - ---------- - list_var : list - List which contents will be validated. - elt_type : datatype, optional - Datatype of which the list elements must be type of. Otherwise - an Error will be raised. The default is str. - is_file : boolean, optional - Flag to indicate if the list contains paths to files. If True the - strings will be checked if they correspond to an existing file. - The default is False. - - Raises - ------ - TypeError - If one of the list elements is not of type elt_type. - FileNotFoundError - If strings are also checked for existing files and one of the files - does not exist. - - Returns - ------- - None. - - """ - if isinstance(list_var, elt_type): - list_var = [list_var] - if isinstance(list_var, list): - for elt in list_var: - if not isinstance(elt, elt_type): - raise TypeError(f'The list of {list_var} contains an ' - f'object which is not of type {elt_type}.') - if is_file: - # Check if directory and files exist - if not os.path.exists(elt): - raise FileNotFoundError('The specified directory' - f'/file {elt} does not exist.') - else: - raise TypeError(f'The specified {list_var} are neither a ' - f'{elt_type} nor a list of {elt_type}.') - def validate_input(self): """ Validate if input for PublishOptions is correct type. @@ -116,10 +69,10 @@ class PublishOptions: """ # Check if IDs are str - self.validate_list(self.figure_ids) + self.figure_ids = validate_list(self.figure_ids) # Check if plot_name is a string or a list of strings - self.validate_list(self.plot_names) + self.plot_names = validate_list(self.plot_names) if not os.path.isfile(sys.argv[0]): raise FileNotFoundError('Cannot copy original python script. ' @@ -127,7 +80,8 @@ class PublishOptions: 'possible.') # Check if self.src_datapaths are strings and existing files. - self.validate_list(self.src_datapaths, is_file=True) + self.src_datapaths = validate_list(self.src_datapaths, + is_file=True) # Check if destination directory is allowed path if not os.path.exists(self.dst_path_head): @@ -157,7 +111,6 @@ class PublishOptions: """ # Export plot figure to picture. plot_paths = save_plot(self.figure, self.plot_names) - print(plot_paths) match self.data_storage: case 'centralized': self.centralized_data_storage() diff --git a/plotid/save_plot.py b/plotid/save_plot.py index 5cf6f3b..1fa7063 100644 --- a/plotid/save_plot.py +++ b/plotid/save_plot.py @@ -27,7 +27,7 @@ def save_plot(figures, plot_names, extension='png'): Returns ------- - plot_path : str or list of str + plot_path : list of str Names of the created pictures. """ # Check if figs is a valid figure or a list of valid figures diff --git a/tests/test_publish.py b/tests/test_publish.py index 3abb10e..7d2ff49 100644 --- a/tests/test_publish.py +++ b/tests/test_publish.py @@ -47,7 +47,10 @@ class TestPublish(unittest.TestCase): 'from a Python script. Therefore, the script cannot be ' 'copied.') def test_publish(self): - """ Test publish and check if an exported picture file exists. """ + """ + Test publish and check if an exported picture file exists. + The destination path is given with trailing slash. + """ publish(PlotIDTransfer(FIG, 'testID'), SRC_DIR, DST_PATH + '/', PIC_NAME, data_storage='individual') assert os.path.isfile(os.path.join(DST_PATH, 'testID', -- GitLab