Last active
October 26, 2020 11:02
-
-
Save Ash-Crow/08bededf6a2d87a0a06453ff7803f355 to your computer and use it in GitHub Desktop.
Test script to compare the dateparser python module and the ad-hoc date parsing of feedparser
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
import pkg_resources | |
from datetime import timedelta | |
import time | |
from pprint import pprint | |
import dateparser | |
from feedparser import datetimes | |
print(f"dateparser version: {pkg_resources.get_distribution('dateparser').version}") | |
print(f"feedparser version: {pkg_resources.get_distribution('feedparser').version}") | |
print("\n=========\n") | |
samples = [ | |
"Thu, 01 Jan 04 19:48:21 GMT", | |
"Thu, 01 Jan 2004 19:48:21 GMT", | |
"01 Jan 2004", | |
"01 Jan 2004 00:00 GMT", | |
"2003-12-31T10:14:55-08:00", | |
"2003-12-31T10:14:55Z", | |
"2003", | |
"2003-12", | |
"2003-12-31", | |
"20031231", | |
"-03-12", | |
"-0312", | |
"-03-12-31", | |
"031231", | |
"2003-335", | |
"03335", | |
"Sun Jan 4 16:29:06 PST 2004", | |
"Thu, 31 Jun 2004 19:48:21 GMT", | |
"Mon, 26 January 2004", | |
"Mon, 26 Jan 2004 16:31:00 ET", | |
"2003-12-31T25:14:55Z", | |
"2003-12-31T10:61:55Z", | |
"2003-12-31T10:14:61Z", | |
"2004-07-08 23:56:58.0", | |
"2004-07-08 23:56:58", | |
"2004-05-25 오 11:23:17", | |
"Κυρ, 11 Ιούλ 2004 12:00:00 EST", | |
"július-13T9:15-05:00", | |
"Mardi, 8. Septembre 2020 - 10:04", | |
] | |
# Result counters | |
results = { | |
'same_result': 0, | |
'no_date_found': 0, | |
'different_dates': 0, | |
'dateparser_only': 0, | |
'feedparser_only': 0 | |
} | |
for s in samples: | |
# With dateparser | |
try: | |
dp_parsed_date = dateparser.parse(s).utctimetuple() | |
except: | |
dp_parsed_date = None | |
# With feedparser datetimes | |
fpdt_parsed_date = datetimes._parse_date(s) | |
# Comparisons | |
if not dp_parsed_date and not fpdt_parsed_date: | |
print(f"NOK for {s}: Neither module could interpret this as a date") | |
results['no_date_found'] += 1 | |
elif dp_parsed_date == fpdt_parsed_date: | |
results['same_result'] += 1 | |
print(f"OK for {s}: {dp_parsed_date}") | |
else: | |
if dp_parsed_date and fpdt_parsed_date: | |
delta = f"{timedelta(seconds=time.mktime(dp_parsed_date)-time.mktime(fpdt_parsed_date))} off" | |
results['different_dates'] += 1 | |
else: | |
if dp_parsed_date: | |
correct_module = "dateparser" | |
results['dateparser_only'] += 1 | |
else: | |
correct_module = "feedparser" | |
results['feedparser_only'] += 1 | |
delta = f" only {correct_module} could interpred this as a date" | |
print(f"NOK for {s}: {delta}.\n dateparser: {dp_parsed_date}\n feedparser: {fpdt_parsed_date}") | |
print("\n=========\n") | |
print(f"Total: {len(samples)} date formats tested") | |
pprint(results) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Results: