Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
C
caosdb-advanced-user-tools
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
caosdb
Software
caosdb-advanced-user-tools
Commits
fd5c12a6
Commit
fd5c12a6
authored
4 years ago
by
Henrik tom Wörden
Committed by
Florian Spreckelsen
4 years ago
Browse files
Options
Downloads
Patches
Plain Diff
ENH: Allow dates to be incomplete
parent
980d0fa8
No related branches found
No related tags found
1 merge request
!22
Release 0.3
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/caosadvancedtools/table_importer.py
+28
-0
28 additions, 0 deletions
src/caosadvancedtools/table_importer.py
unittests/test_table_importer.py
+20
-1
20 additions, 1 deletion
unittests/test_table_importer.py
with
48 additions
and
1 deletion
src/caosadvancedtools/table_importer.py
+
28
−
0
View file @
fd5c12a6
...
@@ -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
...
...
This diff is collapsed.
Click to expand it.
unittests/test_table_importer.py
+
20
−
1
View file @
fd5c12a6
...
@@ -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
):
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment