Skip to content
Snippets Groups Projects
Commit a11a8cac authored by Henrik tom Wörden's avatar Henrik tom Wörden
Browse files

ENH: add converter that checks input against enum

When importing a table using the TableImporter, it is now possible to
use the enum_converter to assure that values are part of a supplied enum.
parent 99c979d9
No related branches found
No related tags found
1 merge request!22Release 0.3
......@@ -145,6 +145,19 @@ def win_path_converter(val):
return path.as_posix()
def enum_converter(val, options, ignore_case=True):
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()
......
......@@ -33,6 +33,7 @@ from caosadvancedtools.table_importer import (XLSImporter, assure_name_format,
incomplete_date_converter,
win_path_converter,
win_path_list_converter,
enum_converter,
yes_no_converter)
......@@ -49,6 +50,15 @@ class ConverterTest(unittest.TestCase):
self.assertRaises(ValueError, yes_no_converter, "True")
self.assertRaises(ValueError, yes_no_converter, "true")
def test_enum(self):
self.assertEqual("false", enum_converter("false",
["FALSE", "TRUE"]))
self.assertEqual("FALSE", enum_converter("FALSE",
["FALSE", "TRUE"], False))
self.assertRaises(ValueError, enum_converter, "FALSE", [])
self.assertRaises(ValueError, enum_converter, "FALSE", ["fals"])
self.assertRaises(ValueError, enum_converter, "FALSE", ["false"], False)
def test_assure_name_format(self):
self.assertEqual(assure_name_format("Müstermann, Max"),
"Müstermann, Max")
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment