def view_all(): """ View list of students. """ db = DBHandler(DBNAME) students = db.get_all_students() db.close_connection() students = merge_sort(students) return render_template('view_all.html', student_list=students)
def move_graph_users_to_db(graph_path=GRAPH_PATH): start_time = time.time() graph = nx.read_gpickle(graph_path) u_ids = list(graph.nodes()) db_handler = DBHandler() db_handler.save_users_ids(u_ids) elapsed_time = time.time() - start_time print("Done moving graph users. Took %.1f secs to save %d users" % (elapsed_time, len(u_ids))) return 0
def __init__(self): ''' Initializes variables: numProtocols: number of open protocols protocols: maps { protocol: team_id } teams: maps { team_id: Team object } passwords: maps { team_id : password } requestCallIds = { request_id : requestCallId } ''' self.db = DBHandler(k.DB_NAME, k.DB_USER, k.DB_PASSWORD) self.numProtocols = 0 self.protocols = {} self.requestCallIds = {} self._loadTeams()
def __init__(self) -> None: self.conf = parse_env_vars() self.db_handler = DBHandler(self.conf) self.init_provision = False self.citus_master_nodes: typing.Set[str] = set() self.citus_worker_nodes: typing.Set[str] = set() self.start_web_server() self.pod_interactions: typing.Dict[ str, typing.Dict[str, typing.Callable[[str], None]] ] = { "ADDED": { self.conf.master_label: self.add_master, self.conf.worker_label: self.add_worker, }, "DELETED": { self.conf.master_label: self.remove_master, self.conf.worker_label: self.remove_worker, }, } self.config_monitor = self.create_provision_monitor() self.config_monitor.start_watchers()
def market_file_handler(self, filepath, spend_limit): ''' This function takes in a market.json file and updates the players current location and the cargo values. ''' # Get the json into a dict while 1: try: with open(filepath, 'r') as f: js = json.load(f) star_system = js['StarSystem'] station_name = js['StationName'] break except: # Try again until the file actually exists pass # Create a DBHandler instance to get the location of the star system db_conn = DBHandler() out = db_conn.request("SELECT x, y, z FROM systems WHERE name = ?", (star_system,)) #print('Market db request returned {}'.format(out)) if self.location != list(out[0]): self.location = list(out[0]) self.dump_info() print('[Market Check] New location, rebuilding distance table. . .') ldb.build_distances(self.location) print('[Market Check] Done.') # Setup the database thread to run in the background future = pool.submit(ldb.planets_in_range, self.ship_range, self.location, self.cargo_size, station_name, spend_limit) # Set up the call back for the future. future.add_done_callback(con.next_location_to_buy) print('[Market Check] Looking for goods to trade. . .')
def add_student(): """ View add student form. """ form = StudentForm(request.form) if request.method == 'POST' and form.validate(): student = Student(form.roll_number.data, form.name.data, form.rank.data) try: # Try to add student record to database db = DBHandler(DBNAME) db.add_student(student) db.close_connection() except sqlite3.IntegrityError as e: # Primary key constraint: Roll number already exists flash('Roll number already exists.') return render_template('add.html', form=form) return redirect(url_for('view_all')) return render_template('add.html', form=form)
class MainFactory(ServerFactory): def __init__(self): ''' Initializes variables: numProtocols: number of open protocols protocols: maps { protocol: team_id } teams: maps { team_id: Team object } passwords: maps { team_id : password } requestCallIds = { request_id : requestCallId } ''' self.db = DBHandler(k.DB_NAME, k.DB_USER, k.DB_PASSWORD) self.numProtocols = 0 self.protocols = {} self.requestCallIds = {} self._loadTeams() def _loadTeams(self): ''' Load team info from DB and initialize team objects. ''' with self.db: teams = self.db.query_list('SELECT * FROM Teams;') self.db.query_list('UPDATE Teams SET is_logged_in = FALSE;' ) # reset all login states to logged out. self.teams = { team['team_id']: Team(team['team_id'], team['password'], self.db) for team in teams } # ---------------TWISTED FACTORY METHODS----------------------------------# # This will be used by the default buildProtocol to create new protocols: protocol = TeamServerSideProtocol def startFactory(self): ''' Called when factory starts running. Sets isRunning to true for logging. Opens files. ''' print('[SERVER] Factory started. Waiting for connections.') self.isRunning = True self.log = open(LOG_NAME, 'a') self.log.write('\nStarting new log: ' + str(datetime.datetime.now()) + '\n') self.startNextBroadcastTimer() def stopFactory(self): ''' Called when factory is shutting down. Set isRunning to false so we can log without errors, and closes opened files. ''' self.isRunning = False self.log.write('Stopping factory. \n') self.log.close() # ----------------REQUEST BROADCASTING------------------------------------# def startNextBroadcastTimer(self): try: # Retrieve request. request = su.getNextRequest(self, self.db) if request is None: lu.writeToLog(self, 'No WAITING requests left.') return # Start timer (by use of callback) time_til_broadcast = (request['time_requested'] - datetime.datetime.now()).seconds lu.writeToLog( self, 'Time until next broadcast: {}'.format(time_til_broadcast)) reactor.callLater(time_til_broadcast, self.broadcastNextRequest, (request)) except Exception as e: lu.writeToLog( self, '[ERROR] Failed at broadcastTimer. Error: {}'.format(str(e))) print(traceback.format_exc()) def broadcastNextRequest(self, request): ''' Query request from DB and set a timer to broadcast the given request at the time specified in the request. ''' try: # Create request message for broadcast. request_msg = mt.REQUEST_MSG(request) # Send to all teams that are logged in and set timeout. lu.writeToLog( self, 'Brodcasting REQ# {} to Teams: {}'.format( request['request_id'], ','.join([ self.protocols[protocol] for protocol in self.protocols ]))) for protocol in self.protocols: tu.writeToTeam(protocol, request_msg, verbose=False) callId = reactor.callLater(REQUEST_TIMEOUT, self.bidTimeOut, request['request_id']) self.requestCallIds[request['request_id']] = callId # Update request state to sent. with self.db: self.db.query_one( 'UPDATE Requests SET state = %s WHERE request_id = %s;', ('SENT', request['request_id'])) sent_to = len(self.protocols) self.db.query_one( 'UPDATE Requests SET sent_to = %s WHERE request_id = %s;', (sent_to, request['request_id'])) self.startNextBroadcastTimer() except Exception as e: lu.writeToLog( self, '[ERROR] Failed broadcast requests. Error: {}'.format(str(e))) print(traceback.format_exc()) def bidTimeOut(self, request_id): ''' Call when bid request times out. ''' try: lu.writeToLog(self, "[BID TIMEOUT] [REQ# {}]".format(request_id)) with self.db: # Check if someone accepted. If not, update state. result = self.db.query_one( 'SELECT state FROM Requests WHERE request_id = %s', (request_id, )) if result is not None: state = result[0] else: raise Exception('Failed to query request state.') if state == 'SENT': self.db.query_one( 'UPDATE Requests SET state = %s WHERE request_id = %s;', ('NOT_ACCEPTED', request_id)) lu.writeToLog( self, '[Request NOT_ACCEPTED] [REQ# {}]'.format(request_id)) return # If bid was accepted, collect bids. self.sendBidResults(request_id) except Exception as e: lu.writeToLog(self, '[ERROR] On bid timeout. Error: {}'.format(str(e))) print(traceback.format_exc()) # ----------------BID HANDLING--------------------------------------------# def selectBestBid(self, bids): # dummy function now TODO implement once we know how to calculate the best bid. return bids[0] def sendBidResults(self, request_id): ''' Collects bids for REQ# request_id and figures out what is the best bid. Then we proceed to send the best bid to the given drone/team. If this function is called it means at least one bid was submitted. ''' try: # Get bids bid and request from DB. with self.db: bids_accepted = self.db.query_list( 'SELECT * FROM Bids WHERE request_id = %s AND accepted = TRUE;', (request_id, )) if bids_accepted is None or len(bids_accepted) == 0: raise ('Failure collecting bids.') request = self.db.query_one( 'SELECT * FROM Requests WHERE request_id = %s', (request_id, )) if request is None: raise ('Failure collecting bids.') # Send result to winning bid best_bid = self.selectBestBid(bids_accepted) bid_winning_team = best_bid['team_id'] now = datetime.datetime.now() time_expected = now + datetime.timedelta( seconds=best_bid['seconds_expected']) with self.db: self.db.query_one( 'UPDATE Requests SET state = %s WHERE request_id = %s;', ('ASSIGNED', request_id)) self.db.query_one( 'UPDATE Requests SET time_assigned = %s WHERE request_id = %s;', (now, request_id)) self.db.query_one( 'UPDATE Requests SET time_expected = %s WHERE request_id = %s;', (time_expected, request_id)) tu.writeToTeam( self.teams[bid_winning_team].protocol, mt.WINNING_BID_RESULT(request, best_bid, str(time_expected))) # Send result to losing teams bid_losing_teams = [ bid['team_id'] for bid in bids_accepted if bid['team_id'] != bid_winning_team ] for blt in bid_losing_teams: protocol = self.teams[blt].protocol if protocol is not None: tu.writeToTeam(self.teams[blt].protocol, mt.LOSING_BID_RESULT(request)) except Exception as e: lu.writeToLog( self, '[ERROR] On sendBidResults. Error: {}'.format(str(e))) print(traceback.format_exc())
class Manager: config_path = "/etc/citus-config/" def __init__(self) -> None: self.conf = parse_env_vars() self.db_handler = DBHandler(self.conf) self.init_provision = False self.citus_master_nodes: typing.Set[str] = set() self.citus_worker_nodes: typing.Set[str] = set() self.start_web_server() self.pod_interactions: typing.Dict[ str, typing.Dict[str, typing.Callable[[str], None]] ] = { "ADDED": { self.conf.master_label: self.add_master, self.conf.worker_label: self.add_worker, }, "DELETED": { self.conf.master_label: self.remove_master, self.conf.worker_label: self.remove_worker, }, } self.config_monitor = self.create_provision_monitor() self.config_monitor.start_watchers() def create_provision_monitor(self) -> ConfigMonitor: master_config = PodMonitorConfig( self.citus_master_nodes, self.config_path + "master.setup", self.conf.master_service, ) worker_config = PodMonitorConfig( self.citus_worker_nodes, self.config_path + "worker.setup", self.conf.worker_service, ) return ConfigMonitor(self.db_handler, master_config, worker_config) @staticmethod def get_citus_type(pod: V1Pod) -> str: labels = pod.metadata.labels log.info("Retrieved labels: %s", labels) if not labels: return "" return labels.get("citusType", "") def run(self) -> None: log.info("Starting to watch citus db pods in {}".format(self.conf.namespace)) config.load_incluster_config() # or load_kube_config for external debugging api = client.CoreV1Api() w = watch.Watch() for event in w.stream(api.list_namespaced_pod, self.conf.namespace): citus_type, pod_name, event_type = self.parse_event(event) if not citus_type or event_type not in self.pod_interactions: continue handler = self.pod_interactions[event_type] if citus_type not in handler: log.error("Not recognized citus type %s", citus_type) try: handler[citus_type](pod_name) except ReadinessError as e: log.error(e) def parse_event(self, event: dict) -> typing.Tuple[str, str, str]: event_type = event["type"] pod = event["object"] citus_type = self.get_citus_type(pod) pod_name = pod.metadata.name if citus_type: log.info( "New event %s for pod %s with citus type %s", event_type, pod_name, citus_type, ) return citus_type, pod_name, event_type def check_pod_readiness(self, pod_name: str) -> None: @retrying.retry( wait_fixed=5000, retry_on_exception=lambda x: not isinstance(x, client.rest.ApiException), ) def request_pod_readiness(): api = client.CoreV1Api() pod = api.read_namespaced_pod_status(pod_name, self.conf.namespace) status = pod.status readiness = [state.ready for state in status.container_statuses] log.info("Status: %s, %s", pod_name, readiness) assert all(readiness) log.info("Pod %s ready", pod_name) try: request_pod_readiness() except client.rest.ApiException as e: log.info("Error while waiting for pod readiness: %s", pod_name) raise ReadinessError(e) def start_web_server(self) -> None: app = Flask(__name__) @app.route("/registered") def registered_workers() -> str: pods = { "workers": list(self.citus_worker_nodes), "masters": list(self.citus_master_nodes), } return json.dumps(pods) Thread(target=app.run).start() def add_master(self, pod_name: str) -> None: self.check_pod_readiness(pod_name) log.info("Registering new master %s", pod_name) self.citus_master_nodes.add(pod_name) if len(self.citus_worker_nodes) >= self.conf.minimum_workers: self.config_monitor.provision_master(pod_name) for worker_pod in self.citus_worker_nodes: self.add_worker(worker_pod) def remove_master(self, pod_name: str) -> None: self.citus_master_nodes.discard(pod_name) log.info("Unregistered: %s", pod_name) def add_worker(self, pod_name: str) -> None: self.check_pod_readiness(pod_name) log.info("Registering new worker %s", pod_name) self.citus_worker_nodes.add(pod_name) self.exec_on_masters("SELECT master_add_node(%(host)s, %(port)s)", pod_name) if len(self.citus_worker_nodes) >= self.conf.minimum_workers: if not self.init_provision: self.config_monitor.provision_all_nodes() self.init_provision = True else: self.config_monitor.provision_worker(pod_name) def remove_worker(self, worker_name: str) -> None: log.info("Worker terminated: %s", worker_name) self.citus_worker_nodes.discard(worker_name) self.exec_on_masters( """DELETE FROM pg_dist_shard_placement WHERE nodename=%(host)s AND nodeport=%(port)s; SELECT master_remove_node(%(host)s, %(port)s)""", worker_name, ) log.info("Unregistered: %s", worker_name) def exec_on_masters(self, query: str, worker_name: str) -> None: for master in self.citus_master_nodes: worker_host = self.db_handler.get_host_name( worker_name, self.conf.worker_service ) query_params = {"host": worker_host, "port": self.conf.pg_port} self.db_handler.execute_query( master, self.conf.master_service, query, query_params )
class BaseRepository: def __init__(self, table): self.table = table self._db = DBHandler() self.db_conn = self._db.db_connection() def raw_select(self, query): #Utiliza do atributo da classe BaseRepository para abertura de cursor com o banco e manipulação dos dados. cursor = self.db_conn.cursor() #Executa a query no banco com o cursor aberto cursor.execute(query) #Aloca o resultado da query na variável result para expor o resultado result = cursor.fetchall() #Fecha o cursor cursor.close() #Retorna o resultado da query return result def raw_delete(self, table, statement): #Utiliza do atributo da classe BaseRepository para abertura de cursor com o banco e manipulação dos dados. cursor = self.db_conn.cursor() query = "DELETE FROM {0} where {1}".format(table, statement) #Executa a query no banco com o cursor aberto cursor.execute(query) #Faz o commit pro banco para executar as mudanças self.db_conn.commit() return 'OK' def raw_insert(self, query): """ Método da classe pai BaseRepository que todas as classes filhas herdam em que faz um insert no banco. """ #Utiliza do atributo da classe BaseRepository para abertura de cursor com o banco e manipulação dos dados. cursor = self.db_conn.cursor() try: #Executa a query no banco com o cursor aberto cursor.execute(query) #Faz o commit pro banco para executar as mudanças self.db_conn.commit() except: #Caso algo dê errado, faz rollback no banco para o estado anterior da execução dessa query self.db_conn.rollback() #Fecha o cursor cursor.close() #Retorna o resultado da query return 'OK' def select(self, fields="*"): #Utiliza do atributo da classe BaseRepository para abertura de cursor com o banco e manipulação dos dados. cursor = self.db_conn.cursor() field_masks = [] #Adiciona na lista field_masks declarada acima cada parametro passado no método select para montagem da query de select for field in fields: field_masks.append(field) #Formata a lista field masks adicionando um , após cada string para montagem da query. field_masks_format = ', '.join(field_masks) query = "SELECT {0} FROM {1};".format(field_masks_format, self.table) #Executa no db query acima com os valores substituídos pelos valores das variáveis no format. cursor.execute(query) #Aloca em uma var todos os resultados da query passada query_result = cursor.fetchall() #Fecha cursor aberto para manipulação dos dados do db, liberando o mesmo. cursor.close() #Retorna os resultados da query return query_result def update(self, fieldsValues, whereClause, table='product'): #Utiliza do atributo da classe BaseRepository para abertura de cursor com o banco e manipulação dos dados. cursor = self.db_conn.cursor() values_masks = [] values_where_masks = [] #Converte o valor em string para encaixe na query for field in fieldsValues: values_masks.append(f"{field} = \"{fieldsValues[field]}\"") for key, value in whereClause.items(): value = str(value) if self.get_type(value) == '%s': values_where_masks.append(f"{key} = \"{value}\"") #Monta a query com os parametros formatados query = "UPDATE {0} SET {1}, last_updated = now() WHERE {2};".format( table, ', '.join(values_masks), ' AND '.join(values_where_masks)) #Em early return, checa se está dando update em outra tabela e caso sim, altera a query, retirando o parametro last_updated if table != 'product': query = "UPDATE {0} SET {1} WHERE {2};".format( table, ', '.join(values_masks), ' AND '.join(values_where_masks)) #Executa no db query acima com os valores substituídos pelos valores das variáveis no format. cursor.execute(query) #Faz o commit pro banco para executar as mudanças self.db_conn.commit() #Aloca numa var o valor de rows afetadas pela alteração para explicitar no retorno af_rows = str(cursor.rowcount) #Fecha cursor aberto para manipulação dos dados do db, liberando o mesmo. cursor.close() #Retorna o valor de mudanças no banco return af_rows + ' affected rows no banco' def get_type(self, variable): if type(variable) is int: return "%i" elif type(variable) is str: return "%s" elif type(variable) is float: return "%f"
def __init__(self, bot, mongo_uri, mongo_dbname): self.bot = bot self.__logger = logger.get_logger("openai_logger") self.__db = DBHandler(mongo_uri, mongo_dbname, self.__logger) self.__api = ApiHandler(self.__logger)
class GPTBot(commands.Cog): def __init__(self, bot, mongo_uri, mongo_dbname): self.bot = bot self.__logger = logger.get_logger("openai_logger") self.__db = DBHandler(mongo_uri, mongo_dbname, self.__logger) self.__api = ApiHandler(self.__logger) @commands.Cog.listener() async def on_command_error(self, ctx, error): """ From: https://gist.github.com/EvieePy/7822af90858ef65012ea500bcecf1612 """ if hasattr(ctx.command, 'on_error'): return cog = ctx.cog if cog: if cog._get_overridden_method(cog.cog_command_error) is not None: return ignored = (commands.CommandNotFound, ) error = getattr(error, 'original', error) if isinstance(error, ignored): return if isinstance(error, commands.DisabledCommand): await ctx.send(f'{ctx.command} has been disabled.') elif isinstance(error, commands.NoPrivateMessage): try: await ctx.author.send( f'{ctx.command} can not be used in Private Messages.') except discord.HTTPException: pass elif isinstance(error, commands.PrivateMessageOnly): try: await ctx.author.send( f'{ctx.command} can only be used in Private Messages.') except discord.HTTPException: pass @commands.Cog.listener() async def on_guild_join(self, guild): owner_id = guild.owner_id guild_id = guild.id guild_name = guild.name if not self.__db.find_server(guild_id): self.__db.add_server({ "server_id": guild_id, "server_name": guild_name, "owner_id": owner_id, "api_token": None, "response_length": 100, "daily_allowance": 100, "vips": [owner_id], "users": [] }) else: self.__db.update_server_name_id(guild_id, guild_name, owner_id) @commands.Cog.listener() async def on_guild_remove(self, guild): guild_id = guild.id self.__db.delete_server(guild_id) async def cog_before_invoke(self, ctx): await ctx.message.add_reaction(thumbs_up) async def cog_after_invoke(self, ctx): await ctx.message.remove_reaction(thumbs_up, self.bot.user) await ctx.message.add_reaction(complete) async def check_server_token(self, ctx): guild_id = ctx.guild.id return self.__db.check_server_token(guild_id) @staticmethod async def prompt_setup(ctx): await ctx.send( "Owner of this server hasn't configured the bot yet. If you're the owner, send this bot a DM " "with command !setup.") @staticmethod async def empty_warning(ctx): await ctx.send("Hey, no empty prompts!") @staticmethod async def openai_down_warning(ctx): await ctx.send( "OpenAI seems to be down. This sometimes happen and is usually resolved within minutes." ) @staticmethod async def credit_warning(ctx): user_id, user_name = user_parse(ctx) await ctx.send( "{}, your daily allowance is over. :cry:".format(user_name)) @staticmethod async def not_admin_warning(ctx): user_id, user_name = user_parse(ctx) await ctx.send( "{}, this command is only usable by admins.".format(user_name)) @commands.command() async def help(self, ctx): await ctx.send(help_message) @commands.command() @commands.dm_only() async def setup(self, ctx): user_id, user_name = user_parse(ctx) owned_servers = self.__db.find_owned_servers(user_id) if not len(owned_servers) > 0: await ctx.send( "You don't seem to own any servers that i'm on. If you think this is a mistake, kick the " "bot and add it again.") return if len(owned_servers) > 1: await ctx.send( "For the time being you cannot add me to more than one server. But the developers are " "working on it.") return await ctx.send( "Important note: For this bot to function and process requests, it has to save your api key in " "a database. This means that if the bad guys somehow access it, they can use your token and " "even cause incurring charges. We cannot be held responsible for that and suggest you to host " "on your own. You can do so by visiting the public repo on github(" "github.com/e4c6/DiscordGPT-3). If that's not a concern for " "you, feel free to proceed. If you ever doubt something is going fishy, you should quickly " "regenerate your api token through the developer portal on beta.openai.com." ) owned_server = owned_servers[0] server_id, server_name = owned_server["server_id"], owned_server[ "server_name"] await ctx.send( "Hi {}, i see that you're the owner of {}. Now please send me your OpenAI api key within 60 " "seconds. (and " "nothing else).\nIt should be visible to you on https://beta.openai.com/account/api-keys." .format(user_name, server_name)) def check_token(m): return m.channel == ctx.channel msg = await bot.wait_for("message", check=check_token, timeout=60) if not msg.content[0:3] == "sk-": await ctx.send( "Your api token should start with the characters sk-. You can restart this process when " "you find it.") return if len(msg.content) > 50: await ctx.send( "Your key seems abnormally long... You can restart this process when " "you find the right key.") return self.__db.set_server_token(server_id, msg.content) await ctx.send( "Successfully set the token, enjoy! If you like this bot, please consider donating to us via " "our BTC address: 14ozvJYfChmiXwqhfzH4yCqcYR7gzwfVYT") @commands.command() @commands.guild_only() async def config(self, ctx): user_id, user_name = user_parse(ctx) guild_id = ctx.guild.id owner_id, vips, _, length, allowance = self.__db.get_server_settings( guild_id) if user_id != owner_id: raise NotAdminError length_str = "# Length: {}\n".format(length) allowance_str = "# Allowance: {}\n".format(allowance) vips_str = """# Vips\n""" for i in range(len(vips)): vips_str += "{} - {}\n".format(i + 1, vips[i]) vips_str += "Note: Owners are automatically assigned a vip role." return await ctx.send(length_str + allowance_str + vips_str) @commands.command() @commands.guild_only() async def allowance(self, ctx, allowance: int): user_id, user_name = user_parse(ctx) guild_id = ctx.guild.id owner_id, vips, _, length, old_allowance = self.__db.get_server_settings( guild_id) if user_id != owner_id: raise NotAdminError self.__db.update_server_allowance(guild_id, allowance) return await ctx.send( "Successfully updated member allowance to {}".format(allowance)) @commands.command() @commands.guild_only() async def length(self, ctx, length: int): user_id, user_name = user_parse(ctx) guild_id = ctx.guild.id owner_id, vips, _, old_length, allowance = self.__db.get_server_settings( guild_id) if user_id != owner_id: raise NotAdminError self.__db.update_server_length(guild_id, length) return await ctx.send( "Successfully updated response length to {}".format(length)) @commands.command() @commands.guild_only() async def vip(self, ctx, *, member: discord.Member): user_id, user_name = user_parse(ctx) guild_id = ctx.guild.id owner_id, vips, _, length, allowance = self.__db.get_server_settings( guild_id) if user_id != owner_id: raise NotAdminError member_id, member_name = member.id, member.display_name self.__db.add_vip(guild_id, member_id) return await ctx.send( "Successfully added {} to vips.".format(member_name)) @commands.command() @commands.guild_only() async def remove_vip(self, ctx, *, member: discord.Member): user_id, user_name = user_parse(ctx) guild_id = ctx.guild.id owner_id, vips, _, length, allowance = self.__db.get_server_settings( guild_id) if user_id != owner_id: raise NotAdminError member_id, member_name = member.id, member.display_name self.__db.remove_vip(guild_id, member_id) return await ctx.send( "Successfully removed {} from vips.".format(member_name)) @commands.command() @commands.guild_only() async def credit(self, ctx): user_id, user_name = user_parse(ctx) guild_id = ctx.guild.id today_usage, _, _ = self.__db.get_user_settings(guild_id, user_id) _, _, _, _, allowance = self.__db.get_server_settings(guild_id) return await ctx.send( "{}, your remaining daily allowance is: {}.".format( user_name, allowance - today_usage)) @commands.command() @commands.guild_only() async def settings(self, ctx): user_id, user_name = user_parse(ctx) guild_id = ctx.guild.id _, language, temperature = self.__db.get_user_settings( guild_id, user_id) return await ctx.send( "{}, your language is set to: {} and temperature is set to: {}". format(user_name, language, temperature)) @commands.command() @commands.guild_only() async def language(self, ctx, lang: str): user_id, user_name = user_parse(ctx) guild_id = ctx.guild.id possible_languages = list(language_map.keys()) if lang not in possible_languages: return await ctx.send( "{}, chosen language must be within: {}".format( user_name, possible_languages)) today_usage, language, temperature = self.__db.get_user_settings( guild_id, user_id) self.__db.update_user_language(guild_id, user_id, lang) return await ctx.send( "Sucessfully updated your settings, {}. Old language: {}, New language: {}" .format(user_name, language, lang)) @commands.command() @commands.guild_only() async def temperature(self, ctx, temp: float): user_id, user_name = user_parse(ctx) guild_id = ctx.guild.id if not 0 <= temp <= 1: return await ctx.send( "{}, chosen temperature must equal or be within the range of 0 and 1. (e.g. 0.3) " "You can read more about temperature on " "https://towardsdatascience.com/how-to-sample-from-language-models-682bceb97277 or " "https://beta.openai.com/docs/api-reference/create-completion-via-get." .format(user_name)) today_usage, language, temperature = self.__db.get_user_settings( guild_id, user_id) self.__db.update_user_temperature(guild_id, user_id, temp) return await ctx.send( "Sucessfully updated your settings, {}. Old temperature: {}, New temperature: {}" .format(user_name, temperature, temp)) @commands.command() @commands.guild_only() @check_setup async def answer(self, ctx, *prompt: str): usage = len(list("".join(prompt))) if usage == 0: raise EmptyPromptError user_id, user_name = user_parse(ctx) guild_id = ctx.guild.id today_usage, language, temperature = self.__db.get_user_settings( guild_id, user_id) owner_id, vips, token, length, allowance = self.__db.get_server_settings( guild_id) if usage + today_usage > allowance and user_id not in vips: raise CreditExhaustedError answer = await self.__api.answer(prompt, length=length, api_key=token, language=language, temperature=temperature) self.__db.increment_member_usage(guild_id, user_id, usage) return await ctx.send(discord.utils.escape_mentions(answer)) @commands.command() @commands.guild_only() @check_setup async def complete(self, ctx, *prompt: str): usage = len(list("".join(prompt))) if usage == 0: raise EmptyPromptError user_id, user_name = user_parse(ctx) guild_id = ctx.guild.id today_usage, language, temperature = self.__db.get_user_settings( guild_id, user_id) owner_id, vips, token, length, allowance = self.__db.get_server_settings( guild_id) if usage + today_usage > allowance and user_id not in vips: raise CreditExhaustedError answer = await self.__api.complete(prompt, length=length, api_key=token, language=language, temperature=temperature) self.__db.increment_member_usage(guild_id, user_id, usage) return await ctx.send(discord.utils.escape_mentions(answer)) @commands.command() @commands.guild_only() @check_setup async def song(self, ctx, *prompt: str): usage = len(list("".join(prompt))) if usage == 0: raise EmptyPromptError user_id, user_name = user_parse(ctx) guild_id = ctx.guild.id today_usage, language, temperature = self.__db.get_user_settings( guild_id, user_id) owner_id, vips, token, length, allowance = self.__db.get_server_settings( guild_id) if usage + today_usage > allowance and user_id not in vips: raise CreditExhaustedError answer = await self.__api.song(song_name=prompt, user_name=user_name, length=length, api_key=token, language=language, temperature=temperature) self.__db.increment_member_usage(guild_id, user_id, usage) return await ctx.send(discord.utils.escape_mentions(answer)) @commands.command() @commands.guild_only() @check_setup async def foulmouth(self, ctx, *prompt: str): usage = len(list("".join(prompt))) if usage == 0: raise EmptyPromptError user_id, user_name = user_parse(ctx) guild_id = ctx.guild.id today_usage, language, temperature = self.__db.get_user_settings( guild_id, user_id) owner_id, vips, token, length, allowance = self.__db.get_server_settings( guild_id) if usage + today_usage > allowance and user_id not in vips: raise CreditExhaustedError answer = await self.__api.foulmouth_answer(prompt, length=length, api_key=token, language=language, temperature=temperature) self.__db.increment_member_usage(guild_id, user_id, usage) return await ctx.send(discord.utils.escape_mentions(answer)) @commands.command() @commands.guild_only() @check_setup async def sentiment(self, ctx, *prompt: str): usage = len(list("".join(prompt))) if usage == 0: raise EmptyPromptError user_id, user_name = user_parse(ctx) guild_id = ctx.guild.id today_usage, language, temperature = self.__db.get_user_settings( guild_id, user_id) owner_id, vips, token, length, allowance = self.__db.get_server_settings( guild_id) if usage + today_usage > allowance and user_id not in vips: raise CreditExhaustedError answer = await self.__api.sentiment(prompt, api_key=token, language=language) self.__db.increment_member_usage(guild_id, user_id, usage) return await ctx.send(discord.utils.escape_mentions(answer)) @commands.command() @commands.guild_only() @check_setup async def emojify(self, ctx, *prompt: str): usage = len(list("".join(prompt))) if usage == 0: raise EmptyPromptError user_id, user_name = user_parse(ctx) guild_id = ctx.guild.id today_usage, language, temperature = self.__db.get_user_settings( guild_id, user_id) owner_id, vips, token, length, allowance = self.__db.get_server_settings( guild_id) if usage + today_usage > allowance and user_id not in vips: raise CreditExhaustedError answer = await self.__api.emojify(prompt, length=length, api_key=token, language=language, temperature=temperature) self.__db.increment_member_usage(guild_id, user_id, usage) return await ctx.send(discord.utils.escape_mentions(answer)) @commands.command() @commands.guild_only() @check_setup async def sarcasm(self, ctx, *prompt: str): usage = len(list("".join(prompt))) if usage == 0: raise EmptyPromptError user_id, user_name = user_parse(ctx) guild_id = ctx.guild.id today_usage, language, temperature = self.__db.get_user_settings( guild_id, user_id) owner_id, vips, token, length, allowance = self.__db.get_server_settings( guild_id) if usage + today_usage > allowance and user_id not in vips: raise CreditExhaustedError answer = await self.__api.sarcastic_answer(prompt, length=length, api_key=token, language=language, temperature=temperature) self.__db.increment_member_usage(guild_id, user_id, usage) return await ctx.send(discord.utils.escape_mentions(answer)) @commands.command() @commands.guild_only() @check_setup async def headline(self, ctx, *prompt: str): usage = len(list("".join(prompt))) if usage == 0: raise EmptyPromptError user_id, user_name = user_parse(ctx) guild_id = ctx.guild.id today_usage, language, temperature = self.__db.get_user_settings( guild_id, user_id) owner_id, vips, token, length, allowance = self.__db.get_server_settings( guild_id) if usage + today_usage > allowance and user_id not in vips: raise CreditExhaustedError answer = await self.__api.headline(prompt, length=length, api_key=token, language=language, temperature=temperature) self.__db.increment_member_usage(guild_id, user_id, usage) return await ctx.send(discord.utils.escape_mentions(answer))
def get_and_save_tweets_for_all_users(): "Saves all posible tweets for all users" db_handler = DBHandler() db_handler.save_users_timelines(use_milestones=True)
def get_and_save_yesterdays_tweets_for_all_users(): db_handler = DBHandler() day = datetime.date.today() - datetime.timedelta(days=1) db_handler.save_users_timelines(dia=day)
def get_and_save_today_tweets_for_all_users(): db_handler = DBHandler() db_handler.save_users_timelines(dia=datetime.date.today())
def get_and_save_tweets_for_all_users(dia=None): db_handler = DBHandler() db_handler.save_users_timelines(dia=dia)
def __init__(self): self.db = DBHandler.getInstance() self.item_factory = ItemFactory()
async def _create_db(self) -> None: db = DBHandler(self.loop) await db.make_pool() self.db = db
from db import DBHandler import keys as k from demand import DemandGenerator import csv def loadRequests(handler): ''' Loads requests into DB from demand.csv file ''' with open('./demand/demand.csv', newline='') as csvfile: reader = csv.DictReader(csvfile) with handler: handler.query_one('DELETE From Bids;') handler.query_one('DELETE From Requests;') for row in reader: handler.query_one( 'INSERT INTO Requests(k_passengers,sent_to,expected_price,from_port,to_port,time_requested,state) \ VALUES (%s,%s,%s,%s,%s,%s,%s);', (row['k_passengers'], 0, row['expected_price'], row['from_port'], row['to_port'], row['datetime'], 'WAITING')) if __name__ == '__main__': DemandGenerator(start_delay=10).generate_file( filename='./demand/demand.csv') dbh = DBHandler(k.DB_NAME, k.DB_USER, k.DB_PASSWORD) loadRequests(dbh)
def cargo_file_handler(self, filepath, spend_limit, last_cargo): ''' This function takes in a filepath, spending limit, and a cargo and determines if the limit needs to increase or the cargo needs to be updated. ''' # For now, cut this out. We're not using it return spend_limit, last_cargo # From the cargo file, determine what will be sold and next_cargo = self.grab_current_cargo() # First case: No cargo -> Cargo # spend_limit remains the same, last_cargo updates. print('[Cargo Check] Last cargo is {}'.format(last_cargo)) print('[Cargo Check] Next cargo is {}'.format(next_cargo)) # If they are the same, nothing has changed. Return. if next_cargo == last_cargo: return spend_limit, last_cargo if last_cargo == None or last_cargo == []: return spend_limit, next_cargo # Second case: Cargo -> No Cargo # spend_limit will increase depending on what the value of the cargo # was at the location that it gets sold, last_cargo becomes next_cargo. if next_cargo == None or next_cargo == []: # Grab the station name that we are at db_conn = DBHandler() station_name = None while 1: try: print('[Cargo Check] Opening cargo.json. . .') with open(filepath, 'r') as f: js = json.load(f) station_name = js['StationName'] break except: # Try again until the file actually exists continue print('[Cargo Check] Station Name is {}'.format(station_name)) # Get the station ID station_id = db_conn.request("SELECT id FROM stations WHERE name = ?", (station_name,)) # Get the commodity id for the current cargo. cargo_ids = [] for item in last_cargo: cargo_ids.append(db_conn.request("SELECT id FROM commodities WHERE name = ?", (item,))) # Grab the sell value at for the cargo id's at the station id for item in cargo_ids: profit = db_conn.request( "SELECT sell_price FROM listings WHERE station_id = ? AND commodity_id = ?", (station_id, item,) ) spend_limit += profit # Return! return spend_limit, next_cargo
def __init__(self): self.db = DBHandler.getInstance()
def __init__(self, table): self.table = table self._db = DBHandler() self.db_conn = self._db.db_connection()
def __init__(self): self.db = DBHandler.getInstance() self.invoice_factory = InvoiceFactory() self.item_dao = ItemDAOImpl()