Created
May 15, 2018 19:31
-
-
Save peterbe/687f741f66ed1d3aaaf13b63713997ec to your computer and use it in GitHub Desktop.
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 datetime | |
import gzip | |
import csv | |
from glob import glob | |
import concurrent.futures | |
import ciso8601 | |
cutoff = datetime.datetime.utcnow().replace( | |
tzinfo=datetime.timezone.utc | |
) - datetime.timedelta(days=6 * 30) | |
def count(fn, i): | |
print(i, fn) | |
count = total = 0 | |
with gzip.open(fn, 'rt') as f: | |
reader = csv.reader(f) | |
for line in reader: | |
lastmodified = ciso8601.parse_datetime(line[3]) | |
if lastmodified > cutoff: | |
count += 1 | |
total += 1 | |
return total, count | |
def run(): | |
total = recent = 0 | |
futures = [] | |
with concurrent.futures.ProcessPoolExecutor() as executor: | |
for i, fn in enumerate(glob('*.csv.gz')): | |
if len(fn) == 39: | |
futures.append(executor.submit(count, fn, i)) | |
for future in concurrent.futures.as_completed(futures): | |
t, c = future.result() | |
total += t | |
recent += c | |
print(total) | |
print(recent) | |
print('{:.4f}%'.format(100 * recent / total)) | |
run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment