def db_config(): data = request.get_json() username = data.get("username") password = data.get("password") url = data.get("url") port = data.get("port") db_name = data.get("db_name") db_type = data.get("db_type") # Bad request if None in [username, password, url, port, db_name, db_type]: return {"error": "All required values not specified"}, 400 # Possible errors try: db.establish_connection(username, password, url, port, db_name, db_type) except OperationalError as e: return { "error": "Failed to establish connection with the DB, {0}".format(e.orig) }, 500 # On successful connection, return list of tables in the DB tables = db.get_tables_in_db() return {"tables": tables}, 200
def get_brewery(): b = True if 'brewery' in request.args: bry = '.*' + request.args['brewery'] + '.*' elif 'sid' in request.args: bry = request.args['sid'] b = False else: return "No brewery provided. You fool." conn = db.establish_connection() cur = db.create_cursor(conn) out = "" try: if b: cur.execute('SELECT * FROM Breweries WHERE name ~* %s', (bry, )) else: cur.execute('SELECT * FROM Breweries WHERE StateID=%s', (bry, )) for brew_data in cur: out += "<h1>Here's a brewery</h1><p>" + str( brew_data) + "</p><a href='/beer?id=" + str( brew_data[4]) + "'>BEERS</a>" # cur.execute('SELECT * FROM Beers WHERE BreweryId=%s',(brew_data[4],)) # for item in cur: # out+="<p>"+str(item)+"</p>" except Exception as e: out = "<h1>Here's an error</h1><p>" + str(e) + "</p>" finally: # conn.commit() db.close_connection(conn, cur) return out
def api_get_beer(): name = None abbr = None abv = None brew = None style = None results = 20 vals = [] if 'name' in request.args: name = ".*" + request.args['name'] + ".*" vals.append(name) if 'brew' in request.args: brew = ".*" + request.args['brew'] + ".*" vals.append(brew) if 'style' in request.args: style = ".*" + request.args['style'] + ".*" vals.append(style) if 'abv' in request.args: abv = request.args['abv'] vals.append(abv) if 'abbr' in request.args: abbr = request.args['abbr'] vals.append(abbr) if 'results' in request.args: try: results = int(request.args['abbr']) except Exception as e: return str(e) if len(vals) == 0: return jsonify( 'Usage: supply a beername, brewery, state abbreviation, style, or abv' ) conn = db.establish_connection() cur = db.create_cursor(conn) vals.append(results) try: qbase = 'SELECT * FROM Beers WHERE TRUE' abbrq = '' if abbr == None else ' AND stateid=(SELECT stateid FROM States WHERE abbr=%s)' brewq = '' if brew == None else ' AND breweryid= ANY (SELECT breweryid FROM Breweries WHERE name ~* %s)' beerq = '' if name == None else ' AND name ~* %s' abvq = '' if abv == None else ' AND abv=%s' styleq = '' if style == None else ' AND style ~* %s' qend = ' ORDER BY ratings DESC LIMIT %s' cur.execute(qbase + beerq + brewq + styleq + abvq + abbrq + qend, vals) # if name != None: # cur.execute(qbase+brewq+abbrq,vals) # elif beer != None: # cur.execute(qbase+beerq+abbrq,vals) # else: # cur.execute(qbase+'TRUE'+abbrq,vals) results = cur.fetchall() except Exception as e: results = str(e) finally: db.close_connection(conn, cur) return jsonify(results)
def get_all_bids(): conn = db.establish_connection() cur = db.create_cursor(conn) out = [] try: cur.execute('SELECT BreweryID FROM Breweries') for bid in cur: out.append(bid[0]) except Exception as e: logger.info(e) finally: db.close_connection(conn, cur) return out
def get_blinks_from_bid(bid): conn = db.establish_connection() cur = db.create_cursor(conn) out = [] try: cur.execute('SELECT Link FROM Beers WHERE BreweryID=%s', (bid, )) for link in cur: out.append(link[0]) except Exception as e: logger.info(e) finally: db.close_connection(conn, cur) return out
def get_beers(): if 'id' in request.args: id = request.args['id'] else: return "No brewery id provided. You fool." conn = db.establish_connection() cur = db.create_cursor(conn) out = "" try: cur.execute('SELECT * FROM Beers WHERE BreweryId=%s', (id, )) for item in cur: out += "<p>" + str(item) + "</p>" except Exception as e: out = "<h1>Here's an error</h1><p>" + str(e) + "</p>" finally: # conn.commit() db.close_connection(conn, cur) return out
def get_state(): if 'ab' in request.args: ab = request.args['ab'] else: ab = '__' conn = db.establish_connection() cur = db.create_cursor(conn) out = "" try: cur.execute('SELECT * FROM States WHERE Abbr LIKE %s', (ab, )) for item in cur: out += "<p>" + str(item) + "</p><a href='/brewery?sid=" + str( item[2]) + "'>BREWERIES</a>" except Exception as e: out = "<h1>Here's an error</h1><p>" + str(e) + "</p>" finally: # conn.commit() db.close_connection(conn, cur) return out
def main(): ts = time() s = requests.session() f = open("test.html", "a") #download_dir = setup_download_dir() # links = [] login(s) bids = get_all_bids() # for state in states: # l = get_links(state, s) # links += l # p_to_f(str(l)) # print(links) # logger.info("Total brew and beer data: %s",download_link(links[0],s)) # Create a queue to communicate with the worker threads queue = Queue() # # Create 8 worker threads bc = 0 for x in range(8): conn = db.establish_connection() cur = db.create_cursor(conn) worker = DownloadWorker(queue, cur, conn, bc) # # Setting daemon to True will let the main thread exit even though the workers are blocking worker.daemon = True worker.start() # # Put the tasks into the queue as a tuple for bid in bids: links = get_blinks_from_bid(bid) for link in links: # logger.info('Queueing {}'.format(link)) queue.put((link, s)) # # Causes the main thread to wait for the queue to finish processing all the tasks queue.join() # conn.commit() # db.close_connection(conn,cur) f.close() print(bids) print(ld) logging.info('Took %s', time() - ts)
def api_get_brewery(): name = None abbr = None beer = None vals = [] if 'name' in request.args: name = ".*" + request.args['name'] + ".*" vals.append(name) if 'beer' in request.args: beer = ".*" + request.args['beer'] + ".*" vals.append(beer) if 'abbr' in request.args: abbr = request.args['abbr'] vals.append(abbr) results = None if name == None and abbr == None and beer == None: return jsonify( 'Usage: supply a brewery name: ?name=brewname, and or a state abbreviation: ?abbr=stabbr, and or a beer name: ?beer=bname' ) conn = db.establish_connection() cur = db.create_cursor(conn) try: qbase = 'SELECT * FROM Breweries WHERE ' abbrq = '' if abbr == None else ' AND stateid=(SELECT stateid FROM States WHERE abbr=%s)' beerq = '' if beer == None else 'breweryid= ANY (SELECT breweryid FROM Beers WHERE name ~* %s)' brewq = '' if name == None else 'name ~* %s' if name != None: cur.execute(qbase + brewq + abbrq, vals) elif beer != None: cur.execute(qbase + beerq + abbrq, vals) else: cur.execute(qbase + 'TRUE' + abbrq, vals) results = cur.fetchall() except Exception as e: results = str(e) finally: return jsonify(results)
prob1 = clf1.predict_proba(data) prob2 = clf2.predict_proba(data) pre1 = clf1.predict(data) pre2 = clf2.predict(data) result = [None] * len(data) for i in range(len(prob1)): if max(prob1[i]) > max(prob2[i]): result[i] = pre1[i] else: result[i] = pre2[i] return result if __name__ == "__main__": conn = establish_connection() cur = create_cursor(conn) data, style = grab_rand_data(cur, length) svd = TruncatedSVD(n_components=dim, n_iter=7, random_state=42) print(len(data)) # print(vals) # print(len(data[999])) data = check_array(data, accept_sparse=True) # print("d1: ",data[0]) svd.fit(data) d = svd.fit_transform(data) # print("d1: ",data[0]) #DummyClassifier(strategy='most_frequent').fit(d,[styledict[s] for s in style]) # classifier1 = MLPClassifier(random_state=1,max_iter=1000).fit(d,[styledict[s] for s in style]) classifier = GradientBoostingClassifier( n_estimators=100,