Skip to content
Snippets Groups Projects
Commit 938fb844 authored by Henrik tom Wörden's avatar Henrik tom Wörden Committed by Timm Fitschen
Browse files

Introduce date converter

parent 679bb902
No related branches found
No related tags found
1 merge request!22Release 0.3
......@@ -8,7 +8,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added ###
* `send_mail` function in `caosdbadvanced.serverside.helper` module
* `send_mail` function in `caosadvancedtools.serverside.helper` module
- New class to collect possible problems with the data model
- New class for checking and importing tables
- Function to get a file path to a shared resource directory
......@@ -19,6 +19,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Changed ###
* The `caosadvancedtools.table_importer.date_converter` now actually returns
`datetime.date` instance. A new
`caosadvancedtools.table_importer.datetime_converter` replaces the old
`date_converter` and returns a `datetime.datetime` instance.
- The suppression module is now a logging filter.
- The WebUIHandler is now a python logging formatter.
- instead of `get_entity`, type-specific functions are used in
......
......@@ -71,7 +71,7 @@ def yes_no_converter(val):
"Field should be 'Yes' or 'No', but is '{}'.".format(val))
def date_converter(val, fmt="%Y-%m-%d"):
def datetime_converter(val, fmt="%Y-%m-%d %H:%M:%S"):
""" if the value is already a datetime, it is returned otherwise it
converts it using format string
"""
......@@ -82,6 +82,14 @@ def date_converter(val, fmt="%Y-%m-%d"):
return datetime.strptime(val, fmt)
def date_converter(val, fmt="%Y-%m-%d"):
""" if the value is already a datetime, it is returned otherwise it
converts it using format string
"""
return datetime_converter(val, fmt=fmt).date()
def win_path_list_converter(val):
"""
checks whether the value looks like a list of windows paths and converts
......
No preview for this file type
......@@ -21,12 +21,14 @@ import os
import unittest
from functools import partial
from tempfile import NamedTemporaryFile
import datetime
import numpy as np
import pandas as pd
from caosadvancedtools.datainconsistency import DataInconsistencyError
from caosadvancedtools.table_importer import (XLSImporter, assure_name_format,
date_converter,
datetime_converter,
win_path_converter,
win_path_list_converter,
yes_no_converter)
......@@ -60,6 +62,17 @@ class ConverterTest(unittest.TestCase):
r"\this\computer,\this\computer"),
["/this/computer", "/this/computer"])
def test_datetime(self):
test_file = os.path.join(os.path.dirname(__file__), "date.xlsx")
self.importer = XLSImporter(converters={'d': datetime_converter,
}, obligatory_columns=['d'])
xls_file = pd.io.excel.ExcelFile(test_file)
df = xls_file.parse()
df = self.importer.read_xls(test_file)
assert df.shape[0] == 2
assert df.d.iloc[0] == datetime.datetime(1980,12,31,13,24,23)
def test_date(self):
test_file = os.path.join(os.path.dirname(__file__), "date.xlsx")
self.importer = XLSImporter(converters={'a': date_converter,
......
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