-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #100 from ssi-dk/skp/update-submodules-bugfix
fix: update to packages and fix in recursive replace.
- Loading branch information
Showing
5 changed files
with
71 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Submodule microreact-server
updated
5 files
+2 −0 | migrations/manual/20210301140222-3-user-accounts.js | |
+2 −2 | package-lock.json | |
+1 −1 | package.json | |
+28 −1 | pages/api/files/proxy.js | |
+1 −1 | services/proxy-service.js |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
python-dateutil==2.8.1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import os, sys | ||
import datetime | ||
from dateutil import parser | ||
|
||
def recursive_replace(data, replacement_fn, filter_list=None, filtered_parent=False): | ||
# If no filter_list is provided, then assume all leaf nodes in tree must be replaced | ||
do_filter = not filter_list or filtered_parent | ||
if isinstance(data, (dict, list)): | ||
for k, v in data.items() if isinstance(data, dict) else enumerate(data): | ||
# If a key in the filter_list is seen at any node in the tree, leaf values | ||
# underneath that node must be replaced | ||
if k in filter_list: | ||
do_filter = True | ||
else: | ||
do_filter = False | ||
if (not (isinstance(v, (dict, list)))) and do_filter: | ||
try: | ||
replacement_text = replacement_fn(v) | ||
data[k] = replacement_text | ||
except: | ||
pass | ||
else: | ||
data[k] = recursive_replace(v, replacement_fn, filter_list, do_filter) | ||
return data | ||
|
||
def coerce_date(dayfirst): | ||
def parse_value(v): | ||
try: | ||
if isinstance(v, datetime.datetime): | ||
return v.isoformat() | ||
return parser.parse(v, dayfirst=dayfirst).isoformat() if v else None | ||
except: | ||
return None | ||
return parse_value | ||
|
||
def coerce_dates(val, dayfirst=None): | ||
filter_list = list(filter(lambda k: k.startswith("date_"), val.keys())) | ||
print (filter_list) | ||
return recursive_replace( | ||
val, | ||
coerce_date(dayfirst), | ||
filter_list, | ||
) | ||
|
||
data = { | ||
'date_sample': datetime.datetime(2024, 5, 19, 2, 56), | ||
'date_received': '2024-02-19', | ||
'cpr_nr': '2301232629', | ||
'name': 'KNUDSEN, ROSA', | ||
'primary_isolate': False, | ||
'date_received_kma': None, | ||
'kma': None, | ||
'gender': 'K', | ||
'age': 1, | ||
'travel': 'Nej', | ||
'travel_country': None, | ||
'region': 'MIDTJYLLAND' | ||
} | ||
|
||
print(coerce_dates(data)) |