Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow parsing less specific dates #1000

Merged
merged 3 commits into from
Jul 21, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions nmdc_server/ingest/biosample.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,51 @@ def coerce_date(cls, v):
return datetime.strptime(v, "%d-%b-%y %I.%M.%S.%f000 %p").isoformat()
return v

@validator("collection_date", pre=True)
def coerce_collection_date(cls, value):
# { "has_raw_value": ... }
raw_value = value["has_raw_value"]
expected_formats = [
"%d-%b-%y %I.%M.%S.%f000 %p",
"%y-%m-%dT%I:%M:%S",
"%y-%m-%dT%H:%M:%S",
"%Y-%m-%dT%I:%M:%S",
"%Y-%m-%dT%H:%M:%S",
"%Y-%m-%dT%H:%M:%S%z",
"%y-%m-%d %I:%M:%S",
"%y-%m-%d %H:%M:%S",
"%Y-%m-%d %I:%M:%S",
"%Y-%m-%d %H:%M:%S",
"%Y-%m-%d %H:%M:%S%z",
"%Y-%m-%dT%H:%MZ",
]
for date_format in expected_formats:
try:
dt = datetime.strptime(raw_value, date_format).isoformat()
return dt
except ValueError:
continue
if isinstance(raw_value, str) and date_fmt.match(raw_value):
return datetime.strptime(raw_value, "%d-%b-%y %I.%M.%S.%f000 %p").isoformat()
try:
dt = datetime.strptime(raw_value, "%Y-%m-%d").isoformat()
return dt
except ValueError:
try:
raw_value = raw_value + "-01"
dt = datetime.strptime(raw_value, "%Y-%m-%d").isoformat()
return dt
except ValueError:
try:
raw_value = raw_value + "-01"
dt = datetime.strptime(raw_value, "%Y-%m-%d").isoformat()
return dt
except ValueError:
# The raw value may be parseable by pydantic.
# If not, we will a validation error in the
# ingest output
return raw_value


def load_biosample(db: Session, obj: Dict[str, Any], omics_processing: Collection):
logger = get_logger(__name__)
Expand Down
Loading