def __init__(self, parent_window, caller_window, window_type): self.database_interface = DatabaseInterface() # Get the window of the caller self.caller_window = caller_window # Create the window project_names = self.database_interface.get_all_project_names() self.modify_project_names_window = ModifyProjectNamesWindow( parent_window, window_type, project_names) if window_type == "modify": self.update_is_hidden() # Add and delete buttons if window_type == "add": self.modify_project_names_window.set_modify_button_command( self.add_project) elif window_type == "modify": self.modify_project_names_window.set_update_is_hidden_command( self.update_is_hidden) self.modify_project_names_window.set_show_hide_button_command( self.show_hide_project) self.modify_project_names_window.set_modify_button_command( self.delete_project) # Click enter in the entry field if window_type == "add": self.modify_project_names_window.set_modify_project_entry_command( self.add_project)
def random(): message = '' all_body_part_from_db = generate_all_body_part_from_db_lst('exercise.db') if request.method == 'POST': '''all_body_part_from_db - список для всех body_part из базы, разделенный по одной и отсортированный''' body_parts_lst_from_checkbox = generate_body_parts_lst_from_checkbox_lst(all_body_part_from_db) '''body_parts_lst_from_checkbox - список с отмеченными чекбоксами body_part''' select_str = generate_select_string_for_random(body_parts_lst_from_checkbox) '''select_str - селект-запрос с отмеченными чекбоксами body_part''' '''Проверка на корректное amount''' amount = request.form.get('amount') if amount.isdigit() and amount != 0: connect_to_db = DatabaseInterface('exercise.db') table = connect_to_db.select_query(select_str) s = '' rand_lst = [] if int(amount) <= len(table): while len(rand_lst) != int(amount): rand_lst.append(choice(table)) rand_lst = set(rand_lst) rand_lst = list(rand_lst) return render_template('random.html', items=all_body_part_from_db, random_list=rand_lst, table=1) else: message = f'IN DATABASE YOU HAVE ONLY {len(table)} STRINGS. PLEASE ENTER LESS THAN {len(table) + 1}.' else: message = "You need to enter only positive digits" return render_template('random.html', items=all_body_part_from_db, message=message)
def __init__(self, parent_window, caller_window, modify_type, clicked_time): self.database_interface = DatabaseInterface() # Get the window of the caller self.caller_window = caller_window # Initial values of times if modify_type == "update_or_delete": self.init_begin_time = self.database_interface.get_begin_row_from_time( clicked_time) self.init_end_time = self.database_interface.get_end_row_from_time( clicked_time) current_project_name = self.init_begin_time["project_name"] elif modify_type == "add": current_project_name = None # Create the window begin_printed_time, end_printed_time = self.get_printed_times( clicked_time, modify_type) project_names = self.database_interface.get_project_names() self.modify_calendar_window = ModifyCalendarWindow( parent_window, modify_type, begin_printed_time, end_printed_time, project_names, current_project_name) # Modify buttons if modify_type == "update_or_delete": self.modify_calendar_window.set_update_button_command( self.update_time_row) self.modify_calendar_window.set_delete_button_command( self.delete_time_row) elif modify_type == "add": self.modify_calendar_window.set_add_button_command( self.add_time_row)
def __init__(self): self.TemperatureSetPoint = float(0.0) self.Temperature = 0.0 self.StepsPerRevolution = 0 self.brewageId = None self.db = DatabaseInterface(databaseName) # self.db.createDefaultConfiguration() # self.db.createDefaultMashing() # self.db.createDefaultBrewage() self.P = 10 self.I = 1 self.D = 1 self.PID_Output = 0 self.outputPV = 0 self.regelaarActive = False self.prevOutputPV = 0 self.pid = PID.PID(self.P, self.I, self.D) self.pid.setSampleTime(1) self.motor = StepperMotor() self.thread = threading.Thread(target=self.run, args=()) self.runGetTempThread = True self.thread.start() # Start the execution
def run(self): prevOutputPv = -1 dbInterface = DatabaseInterface(databaseName) while(self.runGetTempThread): self.Temperature = self.ReadDS18B20("28-000008717fea") if self.regelaarActive == True: self.pid.setKp (self.P) self.pid.setKi (self.I) self.pid.setKd (self.D) self.pid.SetPoint = self.TemperatureSetPoint self.pid.update(float(self.Temperature)) self.PID_Output = self.pid.output self.outputPV = max(min( int(self.PID_Output), 100 ),0) if self.prevOutputPV != self.outputPV: self.motor.setOutput(self.outputPV) # Only change motor when changed dbInterface.insertMeasurement(self.brewageId,self.TemperatureSetPoint,self.Temperature,self.outputPV) self.prevOutputPV = self.outputPV # print ( "Target: %.1f C | Current: %.1f C | OutputPV: %d" % (self.TemperatureSetPoint, self.Temperature, self.outputPV)) time.sleep(1)
def start_message(message): connect_to_db = DatabaseInterface('exercise.db') connect_to_db.select_from_db() s = '' for i in connect_to_db.select_from_db(): s = s + '\n' for j in i: s = s + str(j) + ' ' bot.send_message(message.chat.id, s)
def updatesignal(pair, timeframe, usedb, exchangename=None): db = DatabaseInterface(usedb) pair, timeframe = format_input(pair, timeframe, exchangename) db.db_updateone({ 'timeframe': timeframe, 'pair': pair }, {'sig': '1'}, 'DATAUPDATESIG', upserts=True)
def getdata(pair, bolllen): db = DatabaseInterface('data_bitmex') data = db.db_find([], 'KLINE5m', filter_dic={'pair': pair}, sort=[('time', -1)], limit=bolllen) data.index = [base.timestamp_toDatetime(float(t)) for t in data['time']] data = data.sort_values('time') return data
def generate_all_body_part_from_db_lst(db): connect_to_db = DatabaseInterface(db) a = connect_to_db.select_parts() all_body_part_from_db = [] for i in a: for j in i[0].split(','): all_body_part_from_db.append(j) all_body_part_from_db = set(all_body_part_from_db) all_body_part_from_db = list(all_body_part_from_db) all_body_part_from_db.sort() return all_body_part_from_db
def load_metadata(): di = DatabaseInterface() meta = di.getMetaData() meta['orphaned_groups'] = meta['groups_count'] % ( meta['matches_complete_count'] * 2) meta['orphaned_players'] = meta['players_count'] % (meta['groups_count'] * 5) li.dump_meta(meta) del di s.enter(schedule_time, 1, load_metadata)
def select_parts(): connect_to_db = DatabaseInterface('exercise.db') body_parts_lst = [] for i in connect_to_db.select_parts(): split_body_part = i[0].split(',') for j in split_body_part: body_parts_lst.append(j) body_parts_lst = set(body_parts_lst) body_parts_lst = list(body_parts_lst) body_parts_lst.sort() number_of_ids = [] for i in range(1, len(body_parts_lst) + 1): number_of_ids.append(i) a = list(zip(number_of_ids, body_parts_lst)) return render_template('index.html', table=2, items=a)
def delete_exercise(): message = '' if request.method == 'POST': id_number = request.form.get('id_number') connect_to_db = DatabaseInterface('exercise.db') connect_to_db.select_id() if id_number.isdigit(): if (int(id_number),) in connect_to_db.select_id(): connect_to_db.delete_exercise(id_number) message = f'String with ID = {id_number} deleted from DB' else: message = f'String with ID = {id_number} does not exists!' else: message = 'You need to enter positive digit!' return render_template('delete.html', message=message)
def initdata_from_okex(pair, timeframe, usedb): db = DatabaseInterface(usedb) api = OKCoinFuture() fpair, ftimeframe = format_input(pair, timeframe) kline = api.future_kline(pair, 'quarter', timeframe=timeframe, period={'months': -3}, size=5000) if not kline.empty: kline = kline[[ 'high', 'low', 'open', 'close', 'time', 'volume', 'pct_change' ]] kline['pair'] = fpair db.db_insertdataframe(kline, 'KLINE' + ftimeframe) return True else: return False
def __init__(self, parent_window): # Database interface self.database_interface = DatabaseInterface() # Create the window project_names = self.database_interface.get_project_names() times_df = self.database_interface.get_dataframe_times() self.calendar_window = CalendarWindow(parent_window, project_names, times_df) # Prev and next week buttons self.calendar_window.set_prev_week_button_command(self.set_prev_week) self.calendar_window.set_next_week_button_command(self.set_next_week) # Click inside the canvas self.calendar_window.set_project_blocks_command( self.update_or_delete_project_block) self.calendar_window.set_add_project_blocks_command( self.add_project_block)
def updateklinedata(pair, timeframe, exchangename, usedb, rl, tf_asminute): #取当前数据库中最晚的一条数据的时间加上一秒为起始,获取到当前时间的数据# db = DatabaseInterface(usedb) fpair, ftimeframe = format_input(pair, timeframe, exchangename) lastrow = db.db_findone('KLINE' + ftimeframe, filter_dic={'pair': fpair}, sel_fields=[], sort=[("time", -1)]) print(ftimeframe + fpair + usedb) if lastrow is None: if exchangename == 'okex': return initdata_from_okex(pair, timeframe, usedb) else: print(ftimeframe + fpair + usedb) assert False, '数据库中没有找到数据' print(type(tf_asminute)) if rl == 'l': shiftsec = 60 * int(tf_asminute) * 1000 else: shiftsec = 0 lasttime = base.timestamp_toStr(float(lastrow['time']) + 1000, dateformat="%Y%m%d %H:%M:%S") endtime = base.timestamp_toStr( time.time() * 1000 - shiftsec, dateformat="%Y%m%d %H:%M:%S") if rl == 'l' else None #交易所获取k线函数 # print(lasttime) print(lasttime + ',' + endtime) df = getkline(pair, timeframe, exchangename, lasttime, end=endtime) print(df.shape[0]) # df=None #没有新数据,不更新 if not df is None: #对于原始数据进行修正 df['pct_change'].iloc[0] = (df['close'].iloc[0] - lastrow['close']) * 100 / lastrow['close'] # if rl=='l': # df['time']=[t+shiftsec for t in df['time']] db.db_insertdataframe(df, 'KLINE' + ftimeframe) return True else: return False
def insert_exercise(): message = '' if request.method == 'POST': name = request.form.get('name') body_part = request.form.get('body_part') about = request.form.get('about') pic_link = request.form.get('pic_link') connect_to_db = DatabaseInterface('exercise.db') all_rows = connect_to_db.select_for_insert() if name == '' or body_part == '' or about == '' or pic_link == '': message = 'You need to enter all values' return render_template('insert.html', message=message) else: if (name, body_part, about, pic_link) not in all_rows: connect_to_db.add_exercise(name, body_part, about, pic_link) message = f'String with {name}, {body_part}, {about}, {pic_link} added to DB' return render_template('insert.html', message=message) else: message = 'This data is already in table' return render_template('insert.html', message=message) return render_template('insert.html', message=message)
def __init__(self, root): # Variables self.database_interface = DatabaseInterface() self.root = root # Time format self.time_format = '%Y-%m-%d %H:%M:%S' # Get variables to create the main window last_action = self.database_interface.get_last_action() if last_action is None: last_action = { "time": "N/A N/A", "project_name": "N/A", "action_type": "N/A" } project_names = self.database_interface.get_project_names() # Create the main window self.main_window = MainWindow(self.root, project_names, last_action) # Start and stop buttons self.main_window.set_start_button_command(self.add_work_time) self.main_window.set_stop_button_command(self.end_work_time) # Add and delete project buttons self.main_window.set_add_project_button_command( self.create_add_project_window) self.main_window.set_modify_project_button_command( self.create_modify_project_window) # self.main_window.set_delete_project_button_command(self.create_delete_project_window) # Summary button self.main_window.set_summary_button_command(self.create_summary_window) # Calendar button self.main_window.set_calendar_button_command( self.create_calendar_window)
def load_upcoming(): di = DatabaseInterface() try: up_matches = getUpcomingMatches(20) for m in up_matches: match_id = m[0] start_time = m[1] if(not di.checkUpcomingMatchInDatabase(match_id)): #li.log(match_id.split('/')[2] + " available", type="success") # weird that the except warrants success. That's fine though try: di.writeMatch(match_id) except LineupIncompleteException as err: #li.log(traceback.format_exc(), type='traceback') pass except WriteMatchException as err: li.log(traceback.format_exc(), type='traceback') pass else: li.log("already collected " + match_id) except Exception as err: li.log(traceback.format_exc(), type='traceback') li.log(type(err).__name__, type='error') del di s.enter(schedule_time, 1, load_upcoming)
#!/usr/bin/env python #!/usr/bin/python import cgi, cgitb import sys import json sys.path.append('/var/www/elo/database_interface') from DatabaseInterface import DatabaseInterface import elo form = cgi.FieldStorage() player_id = form.getvalue('player_id') league_id = form.getvalue('league_id') db = DatabaseInterface() playerId = db.addPlayerToLeague(player_id, league_id) print "Content-type:text/plain\n\n"
except IndexError: print('时间段数据不存在,起始时间向后推迟一个月,重试...') start = base.date_togapn(start, dateformat="%Y%m%d %H:%M:%S", months=1) time.sleep(10) continue break # res['time']=[str(base.datetime_toTimestamp(t)) for t in res.index] res = res[selectfields] res = res.reset_index(drop=True) db.db_insertdataframe(res, conf.collnam) if __name__ == '__main__': # api=BitfinexAPI() # api=BitmexAPI() db = DatabaseInterface(conf.usedb) pairs = [conf.pair] # allpairs=['t'+p.upper() for p in api.symbols() if ('usd' in p)] # allpairs=[p for p in allpairs if (not p in pairs)] selectfields = conf.selectfields timeframe = conf.timeframe contract_type = conf.contract_type period = conf.period start, end = conf.start, conf.end for p in pairs: crawldata(p, timeframe, start, end, selectfields, contract_type, period)
# dataId, clusterId # 1 1 # 2 1 # 3 2 # 4 2 # and we want {1:[1,2],2:[3,4]}, called self.groups for k, v in zip(self.labels, itemFeatures.index.tolist()): self.groups.setdefault(k, []).append(v) self.trained = True def predict(self, itemFeatures): centers = self.model.predict(itemFeatures) # based on the predicted centers, find the corresponding cluster members return centers, [self.groups[c] for c in centers] if __name__ == "__main__": from DatabaseInterface import DatabaseInterface db = DatabaseInterface("../DATA") db.startEngine() itemFeatureTable = db.extract( DatabaseInterface.ITEM_FEATURE_KEY).loc[:, "unknown":] model = ClusteringModel() model.train(itemFeatureTable) print model.predict(itemFeatureTable.loc[1].values.reshape(1, -1)) print itemFeatureTable.loc[[1, 422]] print model.labels[:20]
# X must be a dataframe, with the second key as itemID, and third key as ratings itemID = list(history)[1] ratings = list(history)[2] # what if only an item only got rated by one user, and the rating is 5, are we confident it is most popular? nLimit = int(history.shape[0] * self.N_Freq_limit) itemRatingGrouped = history.groupby(itemID) itemRatingGroupedCount = itemRatingGrouped[ratings].count() # print itemRatingGrouped[ratings].mean() self.mostPopular = itemRatingGrouped[ratings].mean()[ itemRatingGroupedCount > nLimit].sort_values(ascending=False) def predict(self, X): # X can only be a list of itemID's return [self.mostPopular.index.get_loc(x) for x in X] def provideRec(self): return self.mostPopular.index.tolist() if __name__ == "__main__": from DatabaseInterface import DatabaseInterface db = DatabaseInterface("DATA") db.startEngine() df = db.extract("history") print df.head() model = MostPopularModel() model.train(df) print model.mostPopular print model.predict([408]) print model.provideRec()
indices = self.clustering_model.predict(itemFeature) if rating >= self.THRESHOLD: self.recommendations = indices else: self.recommendations = [] def predict(self, itemFeature): # X should be item's category feature, only single record # return the similar items # itemFeature = itemFeature.values.reshape(1, -1) indices = self.clustering_model.predict(itemFeature) return indices def provideRec(self): return self.recommendations if __name__ == "__main__": connector = DatabaseInterface("../DATA") connector.startEngine() itemFeatures = connector.connTable["item_feature"] cluster_model = ClusteringModel() cluster_model.train(itemFeatures) simularity_item_model = SimilarItemModel(cluster_model) item_feature = itemFeatures[0:1] simularity_item_model.train(item_feature, 3) print simularity_item_model.provideRec()
def __init__(self, parent_window): # Database interface self.database_interface = DatabaseInterface() summary_table = self.create_summary_table() self.summary_window = SummaryWindow(parent_window, summary_table)
def __init__(self, configMap): self.db = DatabaseInterface(configMap['data_dir']) self.numberToServe = configMap['numberToServe'] self.log = logging.getLogger(__name__)
def show_all(): connect_to_db = DatabaseInterface('exercise.db') return render_template('index.html', table=1, items=connect_to_db.select_from_db())
if exchangename == 'bitmex' and pair == 'XBTUSD': pair = 'BTCUSD' else: pair = pair.replace('_', '').upper() tf1 = re.findall(r'\d+', timeframe)[0] tf2 = timeframe[timeframe.index(tf1[-1]) + 1] return pair, tf1 + tf2 time.sleep(15) pair = 'BTCUSD' bolllen = 6 timeframe = '30m' usedb = 'data_bitmex' data = getdata(pair, bolllen) db = DatabaseInterface(usedb) if data.index[-1].minute not in [55, 25]: print(data) assert False, str(data.index[-1].minute) else: insertdata = { 'close': float(data['close'].iloc[-1]), 'time': float(data['time'].iloc[0]), 'open': float(data['open'].iloc[0]), 'high': float(data['high'].max()), 'low': float(data['low'].min()), 'volume': float(data['volume'].sum()), 'pair': pair } lastdata = db.db_find([],
def __init__(self, configMap): self.db = DatabaseInterface(configMap['data_dir']) # numberToServe: the number of items finally served to the users self.numberToServe = configMap['numberToServe'] self.log = logging.getLogger(__name__)