diff --git a/CHANGELOG.md b/CHANGELOG.md index e0ec0b6b630e0310ceb81b955ceed7f0089a0e6e..04a5171184bb35417a6eb66aa0739f474adf8b7c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -29,6 +29,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Proof-of-concept integration with Bloxberg. - Introduce a cfood that can create a Record structure based on the contents of a hdf5 file h5py is now an optional dependency +- string-in-list check for table imports ### Changed ### diff --git a/src/caosadvancedtools/cfoods/h5.py b/src/caosadvancedtools/cfoods/h5.py index 9defe77115db7687d3a6c5f27bf7f3d268e605fc..6c68edd3668fec957126aa3234a830aab98fcd25 100644 --- a/src/caosadvancedtools/cfoods/h5.py +++ b/src/caosadvancedtools/cfoods/h5.py @@ -6,7 +6,7 @@ # Copyright (C) 2020 Daniel Hornung <d.hornung@indiscale.com> # Copyright (C) 2021 Henrik tom Wörden <h.tomwoerden@indiscale.com> # Copyright (C) 2021 Alexander Kreft -# Copyright (C) 2021 Laboratory for Fluid Physics and Biocomplexity, +# Copyright (C) 2021 Laboratory for Fluid Physics and Biocomplexity, # Max-Planck-Insitute für Dynamik und Selbstorganisation <www.lfpn.ds.mpg.de> # # This program is free software: you can redistribute it and/or modify diff --git a/src/caosadvancedtools/table_importer.py b/src/caosadvancedtools/table_importer.py index 10a4b87c11f74d91a625c88d6c521af600816a96..2f65a4249ce50394eff574abb6bf716602ad4aea 100755 --- a/src/caosadvancedtools/table_importer.py +++ b/src/caosadvancedtools/table_importer.py @@ -148,6 +148,43 @@ def win_path_converter(val): return path.as_posix() +def string_in_list(val, options, ignore_case=True): + """Return the given value if it is contained in options, raise an + error otherwise. + + Parameters + ---------- + val : str + String value to be checked. + options : list<str> + List of possible values that val may obtain + ignore_case : bool, optional + Specify whether the comparison of val and the possible options + should ignor capitalization. Default is True. + + Returns + ------- + val : str + The original value if it is contained in options + + Raises + ------ + ValueError + If val is not contained in options. + """ + if ignore_case: + val = val.lower() + options = [o.lower() for o in options] + + if val not in options: + raise ValueError( + "Field value is '{}', but it should be one of the following " + "values: {}.".format(val, ", ".join( + ["'{}'".format(o) for o in options]))) + + return val + + class TSVImporter(object): def __init__(self, converters, obligatory_columns=[], unique_columns=[]): raise NotImplementedError() diff --git a/unittests/test_table_importer.py b/unittests/test_table_importer.py index 7d44a701a5e2ee0cdbc4b4d061df4d5ac19ccbd0..b71d2f5a8af863c8840f6d930dc4c58b2cbccb5d 100644 --- a/unittests/test_table_importer.py +++ b/unittests/test_table_importer.py @@ -33,6 +33,7 @@ from caosadvancedtools.table_importer import (XLSImporter, assure_name_format, incomplete_date_converter, win_path_converter, win_path_list_converter, + string_in_list, yes_no_converter) @@ -49,6 +50,16 @@ class ConverterTest(unittest.TestCase): self.assertRaises(ValueError, yes_no_converter, "True") self.assertRaises(ValueError, yes_no_converter, "true") + def test_string_in_list(self): + self.assertEqual("false", string_in_list("false", + ["FALSE", "TRUE"])) + self.assertEqual("FALSE", string_in_list("FALSE", + ["FALSE", "TRUE"], False)) + self.assertRaises(ValueError, string_in_list, "FALSE", []) + self.assertRaises(ValueError, string_in_list, "FALSE", ["fals"]) + self.assertRaises(ValueError, string_in_list, + "FALSE", ["false"], False) + def test_assure_name_format(self): self.assertEqual(assure_name_format("Müstermann, Max"), "Müstermann, Max") @@ -62,7 +73,7 @@ class ConverterTest(unittest.TestCase): ["/this/computer"]) self.assertEqual(win_path_list_converter( r"\this\computer,\this\computer"), - ["/this/computer", "/this/computer"]) + ["/this/computer", "/this/computer"]) @pytest.mark.xfail(reason="To be fixed, see Issue #34") def test_datetime(self):