-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrimyears.py
executable file
·66 lines (50 loc) · 2.27 KB
/
trimyears.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#!/usr/bin/env python
# encoding: utf-8
__author__ = "Telmo Menezes (telmo@telmomenezes.com)"
__date__ = "Mar 2011"
import sys
import os
import sqlite3
import datetime
import time
def trimyears(dbpath, begin_year, end_year):
conn = sqlite3.connect(dbpath)
cur = conn.cursor()
y0 = int(begin_year)
y1 = int(end_year)
ts0 = time.mktime(datetime.date(y0, 1, 1).timetuple())
ts1 = time.mktime(datetime.date(y1, 12, 31).timetuple())
print('Trimming database to time interval between the years of %d and %d.' % (y0, y1))
cur.execute("SELECT count(id) FROM issues")
row = cur.fetchone()
issues = row[0]
cur.execute("SELECT count(id) FROM articles")
row = cur.fetchone()
articles = row[0]
cur.execute("SELECT count(id) FROM author_citations")
row = cur.fetchone()
author_citations = row[0]
cur.execute("SELECT count(id) FROM issues WHERE timestamp<? or timestamp>?", (ts0, ts1))
row = cur.fetchone()
discard_issues = row[0]
cur.execute("SELECT count(id) FROM articles WHERE timestamp<? or timestamp>?", (ts0, ts1))
row = cur.fetchone()
discard_articles = row[0]
cur.execute("SELECT count(id) FROM author_citations WHERE timestamp<? or timestamp>?", (ts0, ts1))
row = cur.fetchone()
discard_author_citations = row[0]
issue_per = float(discard_issues) / float(issues) * 100.0
article_per = float(discard_articles) / float(articles) * 100.0
author_cit_per = float(discard_author_citations) / float(author_citations) * 100.0
print('Of %d total issues, %d will be discarded. (%.2f%%)' % (issues, discard_issues, issue_per))
print('Of %d total articles, %d will be discarded. (%.2f%%)' % (articles, discard_articles, article_per))
print('Of %d total author citations, %d will be discarded. (%.2f%%)' % (author_citations, discard_author_citations, author_cit_per))
cur.execute("DELETE FROM issues WHERE timestamp<? or timestamp>?", (ts0, ts1))
cur.execute("DELETE FROM articles WHERE timestamp<? or timestamp>?", (ts0, ts1))
cur.execute("DELETE FROM author_citations WHERE timestamp<? or timestamp>?", (ts0, ts1))
conn.commit()
cur.close()
print("Done.")
conn.close()
if __name__ == '__main__':
trimyears(sys.argv[1], sys.argv[2], sys.argv[3])