-
Notifications
You must be signed in to change notification settings - Fork 3
/
document.py
45 lines (31 loc) · 1.12 KB
/
document.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
from NIPTool.models.constants import SAMPLE_KEYS, BATCH_KEYS
from typing import Optional
def empty_str_to_none(x: str) -> Optional[str]:
"""Convert empty string to None"""
x = x.strip()
if not x:
return None
return x
def build_document(csv_data: dict, document_keys: list) -> dict:
"""Build a general document"""
document = {}
for key in document_keys:
value = csv_data.get(key)
if isinstance(value, str):
value = empty_str_to_none(value)
if value is None:
continue
document[key] = value
return document
def build_sample(sample_data: dict) -> dict:
"""Builds a document for the sample collection"""
sample = build_document(sample_data, SAMPLE_KEYS)
if sample.get("SampleProject"):
sample["SampleProject"] = str(sample["SampleProject"])
sample["_id"] = sample_data.get("SampleID")
return sample
def build_batch(batch_data: dict) -> dict:
"""Builds a document for the batch collection"""
batch = build_document(batch_data, BATCH_KEYS)
batch["_id"] = str(batch_data.get("SampleProject"))
return batch