Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
postgrestutils
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository 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
tools
postgrestutils
Commits
6465e070
Commit
6465e070
authored
3 years ago
by
Fynn Becker
Browse files
Options
Downloads
Patches
Plain Diff
Parse date strings as date
parent
4792c0e3
Branches
date-parsing
No related tags found
No related merge requests found
Pipeline
#7877
failed
3 years ago
Stage: test
Changes
1
Pipelines
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
postgrestutils/utils.py
+13
-2
13 additions, 2 deletions
postgrestutils/utils.py
with
13 additions
and
2 deletions
postgrestutils/utils.py
+
13
−
2
View file @
6465e070
import
importlib.util
import
importlib.util
import
logging
import
logging
import
re
import
re
from
datetime
import
datetime
,
timedelta
,
timezone
from
datetime
import
date
,
datetime
,
timedelta
,
timezone
from
typing
import
Dict
,
Union
from
typing
import
Dict
,
Union
_DJANGO
=
importlib
.
util
.
find_spec
(
"
django
"
)
is
not
None
_DJANGO
=
importlib
.
util
.
find_spec
(
"
django
"
)
is
not
None
...
@@ -71,6 +71,13 @@ def _try_python_parse_dt(value: str) -> Union[datetime, str]:
...
@@ -71,6 +71,13 @@ def _try_python_parse_dt(value: str) -> Union[datetime, str]:
_try_parse_dt
=
_try_django_parse_dt
if
_DJANGO
else
_try_python_parse_dt
_try_parse_dt
=
_try_django_parse_dt
if
_DJANGO
else
_try_python_parse_dt
def
_try_parse_date
(
value
:
str
)
->
Union
[
date
,
str
]:
try
:
return
datetime
.
strptime
(
value
,
"
%Y-%m-%d
"
).
date
()
except
ValueError
:
return
value
def
datetime_parser
(
json_dict
:
dict
)
->
dict
:
def
datetime_parser
(
json_dict
:
dict
)
->
dict
:
"""
"""
A function to use as `object_hook` when deserializing JSON that parses
A function to use as `object_hook` when deserializing JSON that parses
...
@@ -78,7 +85,11 @@ def datetime_parser(json_dict: dict) -> dict:
...
@@ -78,7 +85,11 @@ def datetime_parser(json_dict: dict) -> dict:
:param json_dict: the original `json_dict` to process
:param json_dict: the original `json_dict` to process
:return: the modified `json_dict`
:return: the modified `json_dict`
"""
"""
str_parsers
=
(
_try_parse_dt
,
_try_parse_date
)
for
key
,
value
in
json_dict
.
items
():
for
key
,
value
in
json_dict
.
items
():
if
isinstance
(
value
,
str
):
if
isinstance
(
value
,
str
):
json_dict
[
key
]
=
_try_parse_dt
(
value
)
for
parser
in
str_parsers
:
json_dict
[
key
]
=
parser
(
value
)
# json_dict[key] = _try_parse_dt(value)
return
json_dict
return
json_dict
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