def plot(sql, title, headers): connection.run_sql(sql) results = connection.results() html = _build_table(results, title, headers) _save_table(html, results, title)
def plot_transpose(sql, title, headers): connection.run_sql(sql) results = connection.results() columns = connection.columns() transposed = [] for index, value in enumerate(results[0]): transposed.append((columns[index], str(value))) html = _build_table(transposed, title, headers) _save_table(html, transposed, title)
def insert_link(self, link): connection.run_sql( 'SELECT count(*) FROM cited_links WHERE post = %s AND link = %s', [self.post_id, link]) previous_results = connection.results() if previous_results[0][0] > 0: return location = urlparse(link).netloc print(location) connection.run_sql( 'INSERT INTO cited_links (post, link, location) VALUES (%s, %s, %s)', [self.post_id, link, location])
def plot(sql, title, legend=False): connection.run_sql(sql) results = connection.results() x, y, total = _separate(results) N = len(x) ind = range(N) # the x locations for the groups width = 0.50 # the width of the bars fig, ax = plt.subplots() rects = [] bottom = [0 for element in y[0]] for i, data in enumerate(y): rects.append(ax.bar(ind, data, width, color=colors[i], bottom=bottom)) bottom = [element + y[i][j] for j, element in enumerate(bottom)] # add some text for labels, title and axes ticks ax.set_title(title) ax.set_xticks([i + width / 2 for i in ind]) ax.set_xticklabels([label for label in x]) if (legend != False): plt.legend(rects, legend) # ax.legend(rects1[0], ('Men')) if (len(rects) == 1): label_top(rects[0], ax) else: label_middle(rects, ax) plt.axis([0 - width, len(x), 0, float(max(total)) * 1.618]) plt.savefig(utils.filename_from_title(title, 'png')) plt.clf()
import connection from html.parser import HTMLParser from urllib.parse import urlparse connection.run_sql('select body, id from post') posts = connection.results() class LinkFinder(HTMLParser): def set_post(self, post_id): self.post_id = post_id def handle_starttag(self, tag, attrs): if (tag == 'a'): for attr in attrs: if (attr[0] == 'href'): self.insert_link(attr[1]) def insert_link(self, link): connection.run_sql( 'SELECT count(*) FROM cited_links WHERE post = %s AND link = %s', [self.post_id, link]) previous_results = connection.results() if previous_results[0][0] > 0: return location = urlparse(link).netloc print(location)
def get_question_ids(): sql = """ select id from post where post_type_id = 1; """ connection.run_sql(sql) return connection.results()