Skip to content

Commit

Permalink
Merge pull request #100 from ssi-dk/skp/update-submodules-bugfix
Browse files Browse the repository at this point in the history
fix: update to packages and fix in recursive replace.
  • Loading branch information
sjkp authored Sep 10, 2024
2 parents 601cfde + 2135d74 commit 2e11799
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 6 deletions.
12 changes: 8 additions & 4 deletions bifrost/bifrost_queue_broker/common/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,20 +135,22 @@ def get_collection(collection: str):

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
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
if (not (isinstance(v, (dict, list)))) and do_filter:
try:
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:
else:
data[k] = recursive_replace(v, replacement_fn, filter_list, do_filter)
return data

Expand All @@ -170,6 +172,8 @@ def encrypt_dict(encryption_client: ClientEncryption, val, filter_list=None):
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
Expand Down
1 change: 1 addition & 0 deletions test/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
python-dateutil==2.8.1
60 changes: 60 additions & 0 deletions test/test.py
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))
2 changes: 1 addition & 1 deletion web/microreact_integration

0 comments on commit 2e11799

Please sign in to comment.