Repository navigation
Expand file tree
/
Copy pathdatabase_client.py
More file actions
143 lines (126 loc) · 4.48 KB
/
Copy pathdatabase_client.py
File metadata and controls
143 lines (126 loc) · 4.48 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import sqlite3
import pdb
class DatabaseClient:
def __init__(self):
# Initalize all tables if it doesn't already exist.
self.maybe_create_papers_table()
self.maybe_create_repos_table()
self.maybe_create_papers_stats_table()
self.maybe_create_conference_stats_table()
def execute_sql(self, sql):
try:
with sqlite3.connect("test.db") as conn:
cursor = conn.execute(sql)
return cursor
except Exception as e:
print e
return None
def maybe_create_papers_table(self):
sql = '''CREATE TABLE IF NOT EXISTS papers
(paper_title text primary key not null,
conference_name text default "NA")'''
self.execute_sql(sql)
def maybe_create_repos_table(self):
sql = '''CREATE TABLE IF NOT EXISTS repos
(repo_url text,
has_numpy integer,
has_scikit integer,
has_tensorflow integer,
has_pytorch integer,
has_matlab integer,
paper_title text,
PRIMARY KEY(repo_url),
FOREIGN KEY(paper_title) REFERENCES papers(paper_title))'''
self.execute_sql(sql)
def maybe_create_conference_stats_table(self):
sql = '''CREATE TABLE IF NOT EXISTS conference_stats
(conference_name text primary key not null,
num_numpy integer,
num_scikit integer,
num_tensorflow integer,
num_pytorch integer,
num_matlab integer,
num_papers integer)'''
self.execute_sql(sql)
def maybe_create_papers_stats_table(self):
sql = '''CREATE TABLE IF NOT EXISTS papers_stats
(paper_title text PRIMARY KEY NOT NULL,
conference_name text,
has_numpy integer,
has_scikit integer,
has_tensorflow integer,
has_pytorch integer,
has_matlab integer)'''
self.execute_sql(sql)
def update_conference_stats_table(self, values):
sql = '''REPLACE INTO conference_stats
VALUES (\"{}\", {},{},{},{},{},{})'''.format(
values['conference_name'],
values['num_numpy'],
values['num_scikit'],
values['num_tensorflow'],
values['num_pytorch'],
values['num_matlab'],
values['num_papers'])
self.execute_sql(sql)
def update_papers_stats_table(self):
# Joins the papers and repos table and updates papers_info table.
sql = '''REPLACE INTO papers_stats
SELECT papers.paper_title, conference_name, has_numpy,
has_scikit, has_tensorflow, has_pytorch, has_matlab
from papers
inner join repos
on papers.paper_title = repos.paper_title'''
self.execute_sql(sql)
def create_repo_row(self, url, paper_title, libraries):
numpy, scikit, tensorflow, pytorch, matlab = 0,0,0,0,0
if libraries:
for key,value in libraries.items():
if key == "numpy" and value:
numpy = 1
if key == "scikit" and value:
scikit = 1
if key == "tensorflow" and value:
tensorflow = 1
if key == "pytorch" and value:
pytorch = 1
if key == "matlab" and value:
matlab = 1
values = "(\"{}\",{},{},{},{},{},\"{}\")".format(
url, numpy, scikit, tensorflow, pytorch, matlab, paper_title)
return values
def maybe_update_github_object(self, github_object):
values = self.create_repo_row(
github_object['url'],
github_object['paper_title'],
github_object['libraries'])
sql = '''REPLACE INTO repos VALUES {}'''.format(values)
self.execute_sql(sql)
def create_paper_row(self, paper_title, conference_name):
values = "(\"{}\",\"{}\")".format(paper_title, conference_name)
return values
def maybe_update_paper_object(self, paper_object):
values = self.create_paper_row(
paper_object['paper_title'],
paper_object['conference_name'])
sql = '''REPLACE INTO papers VALUES {}'''.format(values)
self.execute_sql(sql)
def get_all_papers_stats(self):
# Returns the rows of papers_stats returned by the DB.
sql = '''SELECT * FROM papers_stats'''
cursor = self.execute_sql(sql)
result = []
for item in cursor:
result.append(item)
return result
def get_all_conference_stats(self):
# Returns the rows of conference_stats returned by the DB.
sql = '''SELECT * FROM conference_stats'''
cursor = self.execute_sql(sql)
result = []
for item in cursor:
result.append(item)
return result
if __name__=="__main__":
print "Staring DatabaseClient"
db = DatabaseClient()