From fd5c12a6d5a9d2796701b75f84e5d471aef2a6fe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20tom=20W=C3=B6rden?= <h.tomwoerden@indiscale.com> Date: Thu, 8 Oct 2020 09:03:11 +0000 Subject: [PATCH] ENH: Allow dates to be incomplete --- src/caosadvancedtools/table_importer.py | 28 +++++++++++++++++++++++++ unittests/test_table_importer.py | 21 ++++++++++++++++++- 2 files changed, 48 insertions(+), 1 deletion(-) diff --git a/src/caosadvancedtools/table_importer.py b/src/caosadvancedtools/table_importer.py index 55b1d1a1..04c8ea23 100755 --- a/src/caosadvancedtools/table_importer.py +++ b/src/caosadvancedtools/table_importer.py @@ -91,6 +91,34 @@ def date_converter(val, fmt="%Y-%m-%d"): return datetime_converter(val, fmt=fmt).date() +def incomplete_date_converter(val, fmts={"%Y-%m-%d": "%Y-%m-%d", + "%Y-%m": "%Y-%m", "%Y": "%Y"}): + """ if the value is already a datetime, it is returned otherwise it + converts it using format string + + Parameters + ---------- + val : str + Candidate value for one of the possible date formats. + fmts : dict, optional + Dictionary containing the possible (incomplete) date formats: + keys are the formats into which the input value is tried to be + converted, values are the possible input formats. + """ + + for to, fro in fmts.items(): + try: + date = datetime.strptime(val, fro) + + return date.strftime(to) + + except ValueError: + pass + raise RuntimeError( + "Value {} could not be converted with any format string".format( + val)) + + def win_path_list_converter(val): """ checks whether the value looks like a list of windows paths and converts diff --git a/unittests/test_table_importer.py b/unittests/test_table_importer.py index 02321288..69983017 100644 --- a/unittests/test_table_importer.py +++ b/unittests/test_table_importer.py @@ -17,11 +17,11 @@ # along with this program. If not, see <https://www.gnu.org/licenses/>. +import datetime import os import unittest from functools import partial from tempfile import NamedTemporaryFile -import datetime import numpy as np import pandas as pd @@ -29,6 +29,7 @@ from caosadvancedtools.datainconsistency import DataInconsistencyError from caosadvancedtools.table_importer import (XLSImporter, assure_name_format, date_converter, datetime_converter, + incomplete_date_converter, win_path_converter, win_path_list_converter, yes_no_converter) @@ -87,6 +88,24 @@ class ConverterTest(unittest.TestCase): assert df.shape[0] == 2 assert df.a.iloc[0] == df.b.iloc[0] == df.c.iloc[0] + def test_inc_date(self): + incomplete_date_converter("2020", fmts={"%Y": "%Y"}) == "2020" + incomplete_date_converter("02/2020", + fmts={"%Y": "%Y", "%Y-%m": "%m/%Y"} + ) == "2020-02" + incomplete_date_converter("02/02/2020", + fmts={"%Y": "%Y", "%Y-%m": "%m/%Y", + "%Y-%m-%d": "%d/%m/%Y"} + ) == "2020-02-02" + incomplete_date_converter("2020", + fmts={"%Y": "%Y", "%Y-%m": "%m/%Y", + "%Y-%m-%d": "%d/%m/%Y"} + ) == "2020" + self.assertRaises(RuntimeError, + incomplete_date_converter, + "2020e", + fmts={"%Y": "%Y"}) + class XLSImporterTest(unittest.TestCase): def setUp(self): -- GitLab