Skip to content
Snippets Groups Projects
Commit 31c9d0f5 authored by Florian Spreckelsen's avatar Florian Spreckelsen
Browse files

Merge branch 'f-incomplete' into 'dev'

ENH: allow dates to be incomplete

See merge request caosdb/caosdb-advanced-user-tools!60
parents 980d0fa8 fd5c12a6
No related branches found
No related tags found
1 merge request!22Release 0.3
...@@ -91,6 +91,34 @@ def date_converter(val, fmt="%Y-%m-%d"): ...@@ -91,6 +91,34 @@ def date_converter(val, fmt="%Y-%m-%d"):
return datetime_converter(val, fmt=fmt).date() 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): def win_path_list_converter(val):
""" """
checks whether the value looks like a list of windows paths and converts checks whether the value looks like a list of windows paths and converts
......
...@@ -17,11 +17,11 @@ ...@@ -17,11 +17,11 @@
# along with this program. If not, see <https://www.gnu.org/licenses/>. # along with this program. If not, see <https://www.gnu.org/licenses/>.
import datetime
import os import os
import unittest import unittest
from functools import partial from functools import partial
from tempfile import NamedTemporaryFile from tempfile import NamedTemporaryFile
import datetime
import numpy as np import numpy as np
import pandas as pd import pandas as pd
...@@ -29,6 +29,7 @@ from caosadvancedtools.datainconsistency import DataInconsistencyError ...@@ -29,6 +29,7 @@ from caosadvancedtools.datainconsistency import DataInconsistencyError
from caosadvancedtools.table_importer import (XLSImporter, assure_name_format, from caosadvancedtools.table_importer import (XLSImporter, assure_name_format,
date_converter, date_converter,
datetime_converter, datetime_converter,
incomplete_date_converter,
win_path_converter, win_path_converter,
win_path_list_converter, win_path_list_converter,
yes_no_converter) yes_no_converter)
...@@ -87,6 +88,24 @@ class ConverterTest(unittest.TestCase): ...@@ -87,6 +88,24 @@ class ConverterTest(unittest.TestCase):
assert df.shape[0] == 2 assert df.shape[0] == 2
assert df.a.iloc[0] == df.b.iloc[0] == df.c.iloc[0] 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): class XLSImporterTest(unittest.TestCase):
def setUp(self): def setUp(self):
......
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