-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtool.py
More file actions
62 lines (50 loc) · 1.79 KB
/
tool.py
File metadata and controls
62 lines (50 loc) · 1.79 KB
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
#!/usr/bin/env python2
# database code for newsdata
import psycopg2
DBNAME = "news"
def connect(query):
"""Connect to database and run query."""
conn = psycopg2.connect(database=DBNAME)
c = conn.cursor()
c.execute(query)
results = c.fetchall()
conn.close()
return results
def top_three():
"""Return top three articles and how many views they have in
descending order."""
results = connect("""select title, num from articles, top_three
where top_three.path = concat('/article/',articles.slug)
order by num desc;""")
print 'The top three articles are:'
print '---'
for result in results:
print '"{}" -- {} views'.format(result[0], result[1])
print '---'
def top_authors():
"""Return authors in order of most popular to least popular."""
results = connect("""select author_titles.name, sum(article_count.num)
from author_titles, article_count
where article_count.path like concat('/article/',author_titles.slug)
group by author_titles.name order by sum desc;""")
print 'The most popular authors are:'
print '---'
for result in results:
print '{} -- {} views'.format(result[0], result[1])
print '---'
def high_error_days():
"""Return days on which over 1% of requests resulted in errors and
percentage of errors there were.
"""
results = connect("""select * from error_comparisons
where percentage > 1;""")
print 'Dates when errors higher than 1%:'
print '---'
for result in results:
print '{} -- {}% errors'.format(result[0], result[1])
print '---'
if __name__ == '__main__':
"""Print results of all three queries."""
top_three()
top_authors()
high_error_days()