From 52eabd48658a676ac4577d1c8da31df1fe58093e Mon Sep 17 00:00:00 2001 From: Martin Czygan Date: Fri, 20 Dec 2019 17:43:08 +0100 Subject: datacite: move common date patterns out of the loop Additionally, try the unspecific (%Y) pattern last. --- python/fatcat_tools/importers/datacite.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/python/fatcat_tools/importers/datacite.py b/python/fatcat_tools/importers/datacite.py index 9774e334..77ce1012 100644 --- a/python/fatcat_tools/importers/datacite.py +++ b/python/fatcat_tools/importers/datacite.py @@ -280,6 +280,10 @@ class DataciteImporter(EntityImporter): 'Created', 'Updated', ) + + # Before using (expensive) dateparser, try a few common patterns. + common_patterns = ('%Y-%m-%d', '%Y-%m', '%Y-%m-%dT%H:%M:%SZ', '%Y-%m-%dT%H:%M:%S', '%Y') + for prio in date_type_prio: dates = attributes.get('dates', []) or [] # Never be None. for item in dates: @@ -289,9 +293,6 @@ class DataciteImporter(EntityImporter): # Parse out date, use common patterns first, fallback to dateparser. result, value, year_only = None, item.get('date', ''), False - # Before using (expensive) dateparser, try a few common patterns. - common_patterns = ('%Y-%m-%d', '%Y', '%Y-%m', '%Y-%m-%dT%H:%M:%SZ', '%Y-%m-%dT%H:%M:%S') - for pattern in common_patterns: try: result = datetime.datetime.strptime(value, pattern) -- cgit v1.2.3