async def is_game_valid(game): # Check if any words are games games, errs = get_game_list() if errs: games, errs = get_game_list_from_cache() normalized_games = [normalize_name(g) for g in games] normalized_game = normalize_name(game) return normalized_game in normalized_games
def parse_genotypes(): genotypeRegex = re.compile(r'((?:rs|i)[0-9]+)\(([^\)]+)\)') for page in utils.iter_dump('Is a genotype'): ptitle = page.title.cdata ptext = page.revision.text.cdata name = utils.normalize_name(ptitle) # Parse genotype matches = genotypeRegex.match(name) if not matches: print('Genotype {} invalid'.format(name)) continue # Extract info paramMap = { 'repute': 'r', 'magnitude': 'm', 'summary': 's', } parsed = mwparserfromhell.parse(ptext) snp = matches.group(1) genotype = matches.group(2) if ptext.startswith('#REDIRECT'): target = utils.normalize_name(parsed.filter_wikilinks()[0].title) targetgt = genotypeRegex.match(target) if not targetgt: print('Target genotype {} invalid'.format(target)) continue snpinfo = {} snpinfo[genotype] = targetgt.group(2) yield (snp, snpinfo) continue genotypeinfo = utils.extract_parameters(parsed, 'genotype', paramMap, delete=True) if 'D' in genotype or ':' in genotype: genotypeinfo['o'] = genotype genotype = genotype.replace('D', '-').replace(':', ';') if ';' not in genotype: print('Ignoring {}'.format(name)) if 'm' in genotypeinfo: if genotypeinfo['m'] == '0': genotypeinfo.pop('m', None) else: genotypeinfo['m'] = utils.filter_value(float, genotypeinfo['m']) snpinfo = {} snpinfo[genotype] = genotypeinfo yield (snp, snpinfo)
def get_bga_alias(game_name): # BGA uses different names *in game* than for game creation, so recognize this. aliases = { "redsevengame": "red7", "sechsnimmt": "6nimmt", "sevenwonders": "7wonders", "sevenwondersduel": "7wondersduel", "yatzy": "yahtzee", # `yatzy` is due to it initially using the French name due to copyright concerns "arnak": "lostruinsofarnak", } if normalize_name(game_name) in aliases: return aliases[normalize_name(game_name)] return normalize_name(game_name)
def parse(): for page in utils.iter_dump('Is a genoset'): ptitle = page.title.cdata ptext = page.revision.text.cdata name = utils.normalize_name(ptitle.split('/')[0]) if '/criteria' in ptitle: ptext = ptext.replace(' ', '') gsinfo = { 'c': ptext, } yield (name, gsinfo) else: paramMap = { 'repute': 'r', 'magnitude': 'm', 'summary': 's', } parsed = mwparserfromhell.parse(ptext) gsinfo = utils.extract_parameters(parsed, 'genoset', paramMap, delete=True) # gsinfo['d'] = str(parsed) if 'm' in gsinfo: gsinfo['m'] = utils.filter_value(float, gsinfo['m']) yield (name, gsinfo)
def get(self, **kwargs): name = kwargs.get('name', None) results = [] for dataset_name in os.listdir(self.datasets_dir): dataset_path = self.datasets_dir + dataset_name + '/' labels = get_labels(dataset_path) dataset = { "name": dataset_name, "labels": labels, "path": dataset_path, "trained": is_trained(dataset_path), "last_trained_on": last_trained_date(dataset_path), "trainable": is_trainable(labels) } if name: name = normalize_name(name) if dataset_name == name: return dataset results.append(dataset) return results
def find(self, location, dictionary=None, key_index=1): """Returns the most similar location stored in locations dict to location passed into the function.""" # dictionary is self object if no dictionary is provided if not dictionary: dictionary = self # take first value field to be found value = normalize_name(location[key_index]) # extract matched value from value_matched = process.extractOne(value, dictionary.keys()) if value_matched and value_matched[1] > self.THRESHOLD_RATIO: key = value_matched[0] # if there are more values to evaluate, call recursively if len(location) > key_index + 1: # print value_matched[1], return self.find(location, dictionary[key], key_index + 1) else: # print value_matched[1], return dictionary[key] else: return None
def create(self, name): result = False name = normalize_name(name) dataset_path = self.datasets_dir + name subdirs = ['/bottlenecks', '/labels'] if make_dir(dataset_path): for folder in subdirs: make_dir(dataset_path + folder) result = True return result
def add(self, location, location_matched): """Adds normalized location based on location_matched way to write it, with its correspondence ids from both.""" # print "location", location # print "location matched", location_matched # start with the two matched ids normalized_location = [location.id, location_matched.id] # extend with location and location_matched normalized_location.extend(location) normalized_location.extend(location_matched) # extend with location matched normalized location_matched_norm = [normalize_name(i) for i in location_matched] normalized_location.extend(location_matched_norm) # print normalized_location self.append(normalized_location)
def _add_location(self, dictionary, location, key_index): """Adds location using hierarchical dictionaries and storing id as the value in the last dictionary.""" # takes value of key_index location field key_value = normalize_name(location[key_index]) # if is the last value, add key_value with id, as value if len(location) == key_index + 1: dictionary[key_value] = location # else, add key to dictionary and recursively move on to next key_value else: # if key is not already, add a dictionary for it if key_value not in dictionary: dictionary[key_value] = {} # recursively add location with next key_index inside subdictionary self._add_location(dictionary[key_value], location, key_index + 1)
async def setup_bga_game(message, p1_discord_id, game, players, options): """Setup a game on BGA based on the message. Return a text error or "" """ account, errs = await get_active_session(p1_discord_id) if errs: return errs # Use user prefs set in !setup if set logins = get_all_logins() if (str(message.author.id) in logins and ("username" in logins[str(message.author.id)] and len(logins[str(message.author.id)]["username"]) > 0) and ("password" in logins[str(message.author.id)] and len(logins[str(message.author.id)]["username"]) > 0)): user_data = logins[str(message.author.id)] else: return "Need BGA credentials to setup game. Run !setup." user_prefs = {} all_game_prefs = {} # bga options and bga game options aren't necessarily defined if "bga options" in user_data: user_prefs = user_data["bga options"] if "bga game options" in user_data: all_game_prefs = user_data["bga game options"] if "players" not in options: # play with exactly as many players as specified author_num = 1 num_players = len(players) + author_num options["players"] = f"{num_players}-{num_players}" game_name = normalize_name(game) if game_name in all_game_prefs: # game prefs should override global prefs user_prefs.update(all_game_prefs[game_name]) options.update(user_prefs) table_msg = await message.channel.send("Creating table...") await create_bga_game(message, account, game, players, p1_discord_id, options) await table_msg.delete() account.logout() # Probably not necessary account.close_connection() return ""
def add_files(self, request, dataset_name, label_name): result = {"new_files": []} request_json = {} # if url passed to json body try: if isinstance(request.json, dict): request_json = request.json except Exception as e: print(e) dataset_path = self.datasets_dir + normalize_name(dataset_name) label_dir = dataset_path + '/' + 'labels/' + label_name # decode url format label_dir = unquote(label_dir) # make label dir if not exists make_dir(label_dir) new_files = [] new_hashs = [] hashs = get_hashs(dataset_path, label_name) if 'urls' in request_json.keys(): new_files, new_hashs = save_from_urls(request_json['urls'], label_dir, hashs) # if file passed in body else: if len(request.body) > 32: new_files, new_hashs = save_from_bytes(request.body, label_dir, hashs) update_hashs(dataset_path, label_name, hashs, new_hashs) result['new_files'] = new_files return result
def parse_snps(): # TODO: {{ClinVar}} for page in utils.iter_dump('Is a snp'): ptitle = page.title.cdata ptext = page.revision.text.cdata name = ptitle[0].lower() + ptitle[1:] paramMap = { 'stabilizedOrientation': 'orientation', 'chromosome': 'chromosome', 'position': 'position', 'referenceAllele': 'referenceAllele', 'missenseAllele': 'missenseAllele', 'assembly': 'assembly', 'genomeBuild': 'genomeBuild', 'dbSNPBuild': 'dbSNPBuild', 'summary': 'summary', 'gene_s': 'genes', 'gene': 'gene', } parsed = mwparserfromhell.parse(ptext) snpinfo = {} snpinfo.update( utils.extract_parameters(parsed, ['rsnum', '23andMe SNP'], paramMap)) if 'position' in snpinfo: snpinfo['position'] = utils.filter_value(int, snpinfo['position']) if 'gene_s' in snpinfo: snpinfo['genes'] = [ g.strip() for g in snpinfo['gene_s'].split(',') ] snpinfo.pop('gene_s', None) snpinfo.pop('gene', None) elif 'gene' in snpinfo: snpinfo['genes'] = [snpinfo['gene'].strip()] snpinfo.pop('gene', None) for template in parsed.ifilter_templates(recursive=False): if utils.normalize_name(template.name.strip_code()) not in [ 'rsnum', '23andMe SNP' ]: continue snpinfo['genotypes'] = [] for n in range(1, 9): param = 'geno' + str(n) if template.has(param): g = template.get(param).value.strip_code().strip().replace( ':', ';') matches = re.match('\(([AGCTDIN-]*) *\;([AGCTDIN-]*) *\)', g) if not matches: print('{}: Genotype {} invalid'.format(name, g)) continue allele1 = matches.group(1).replace('D', '-') allele2 = matches.group(2).replace('D', '-') if allele1 == '': allele1 = '-' if allele2 == '': allele2 = '-' # genotypePage = '{}({};{})'.format(name, allele1, allele2) snpinfo['genotypes'].append(allele1 + allele2) parsed.remove(template, recursive=False) break # snpinfo['details'] = str(parsed) yield (name, snpinfo)
def run(): team_abbreviation = [ 'Dal', 'Was', 'NYG', 'Phi', 'NE', 'NYJ', 'Mia', 'Buf', 'TB', 'Atl', 'NO', 'Car', 'Ind', 'Hou', 'Jac', "Ten", 'GB', 'Det', 'Min', 'Chi', 'Cin', 'Pit', 'Bal', 'Cle', 'LAR', 'Ari', 'Sea', 'SF', 'LAC', 'Den', 'Oak', 'KC' ] draftedTeams = list() draftedPlayers = list() allPlayers = dict() playerNames = list() with open( r"C:\Users\patri\Google Drive\CBML\2020\rotoworld_2020_rankings.csv" ) as f: lines = f.readlines() for l in lines: if ("Player" not in l): csvs = l.split(',') rank_number = csvs[0].replace('"', '') rank_player_name = csvs[1].replace('"', '') rank_postion = csvs[2].replace('"', '') rank_nfl_team = csvs[3].replace('"', '') rank_player_name = normalize_name(rank_player_name) playerNames.append(rank_player_name) rankedPlayer = Player(rank_player_name, rank_postion, rank_nfl_team) allPlayers[rank_player_name] = rankedPlayer teamNameSpillter = ",," with open(r"C:\Users\patri\Google Drive\CBML\2020\2019-Draft.csv") as f: lines = f.readlines() team_name = '' for l in lines: if (teamNameSpillter in l): team_name = re.sub(teamNameSpillter, '', l) team_name = team_name.replace('\n', '') team_name = team_name.replace(',N', '') team_name = team_name.strip() else: if ("BID AMOUNT" not in l): csvs = l.split(',') if len(csvs) > 3: name = csvs[1].replace('"', '') if any(name[-3] in t for t in team_abbreviation): team = name[-4:] if any(name[-2] in t for t in team_abbreviation): team = name[-3:] team = team.replace(' ', '') name = name.replace(team, '') name = name.strip() position = csvs[2].replace('"', '') position = position.replace(' ', '') cost = csvs[3].replace('$', '') cost = cost.replace('\n', '') is_keeper_symbol = csvs[4].replace('"', '') is_keeper = False if ("K" in is_keeper_symbol): is_keeper = True position = position.replace('"', '') name = normalize_name(name) p = allPlayers.get(name) if p is not None: draftee = allPlayers[name] draftee.set_2019_draft(int(cost), team_name, is_keeper) draftedPlayers.append(draftee) team = TeamDraft(team_name, draftedPlayers) draftedTeams.append(team) draftedPlayers = list() with open(r"C:\Users\patri\Google Drive\CBML\2020\airyards_2019.csv") as f: lines = f.readlines() team_name = '' for l in lines: if ("full_name" not in l): csvs = l.split(',') player_name = csvs[1].replace('"', '') player_position = csvs[2].replace('"', '') player_team = csvs[3].replace('"', '') player_air_yards = csvs[7].replace('"', '') player_tds = csvs[9].replace('"', '') player_wopr = csvs[14].replace('"', '') player_name = normalize_name(player_name) p = allPlayers.get(player_name) if p is not None: airYards_player = allPlayers[player_name] airYards_player.set_ay(float(player_air_yards), player_wopr) airYards_player.set_tds(player_tds) allPlayers[player_name] = airYards_player with open(r"C:\Users\patri\Google Drive\CBML\2020\runningback_workload.csv" ) as f: lines = f.readlines() for l in lines: if ("Name" not in l): l = re.sub(r'(?!(([^"]*"){2})*[^"]*$),', "", l) csvs = l.split(',') player_name = csvs[0].replace('"', '') player_team = csvs[1].replace('"', '') player_position = csvs[2].replace('"', '') player_rush_attemps = csvs[4].replace('"', '') player_ypa = csvs[6].replace('"', '') player_td = csvs[7].replace('"', '') player_name = normalize_name(player_name) p = allPlayers.get(player_name) if p is not None: rush_player = allPlayers[player_name] rush_player.set_rush_yards(float(player_rush_attemps), float(player_ypa)) rush_player.set_tds(player_td) allPlayers[player_name] = rush_player with open(r"C:\Users\patri\Google Drive\CBML\2020\2020_keepers.csv") as f: lines = f.readlines() for l in lines: csvs = l.split(",") keeper_name = csvs[1].replace('"', '') keeper_name = normalize_name(keeper_name) p = allPlayers.get(keeper_name) if p is not None: keeper_player = allPlayers[keeper_name] keeper_player.set_2020_is_keeper(True) allPlayers[keeper_name] = keeper_player with open(r"C:\Users\patri\Google Drive\CBML\2020\2020_adp.csv") as f: lines = f.readlines() for l in lines: csvs = l.split(",") adp_name = csvs[0].replace('"', '') adp = csvs[13].replace('"', '') p = allPlayers.get(adp_name) if p is not None: adp_player = allPlayers[adp_name] adp_player.set_adp(adp) allPlayers[adp_name] = adp_player with open(r"C:\Users\patri\Google Drive\CBML\2020\notes.csv") as f: lines = f.readlines() for l in lines: csvs = l.split(",") note_player_name = csvs[0] note_player_note = csvs[1].replace('\n', '') note_player = allPlayers[note_player_name] note_player.set_note(note_player_note) notes[note_player_name] = note_player_note for k, v in allPlayers.items(): note = notes.get(k) if note is not None: note_player = allPlayers[k] note_player.set_note(note) with open(r"C:\Users\patri\Google Drive\CBML\2020\notes.csv", 'w') as f: for key in notes: f.writelines('%s,%s\n' % (key, notes[key])) json_string = "[" for k, v in allPlayers.items(): p = allPlayers[k] json_string += '{"player_name" : "%s" , "nfl_team" : "%s" , "position" : "%s" , "adp" : %s , "cost" : %d , "drafted_by" : "%s" , "is_2019_keeper" : "%s", "is_2020_keeper" : "%s", "air_yards" : %d , "wopr" : %s, "yards_per_carry" : %s, "rush_attempts" : %s, "TDs" : %s, "note": "%s" , "is_available" : true},' % ( p.player_name.replace('\n', ''), p.team, p.position, p.adp, p.cost, p.drafted_team.replace(' ', '').replace( '\n', ''), p.is_2019_keeper, p.is_2020_keeper, p.ay, p.wopr, p.ypa_rush, p.rush_attempts, p.tds, p.note) json_string = json_string[:-1] json_string += "]" with open( r"C:\Users\patri\Documents\Sources\fantasy-football-client\client\src\data.json", 'w', encoding='utf-8') as f: json.dump(json_string, f, ensure_ascii=False, indent=4) print(json_string) create_note()
async def get_tables_by_players(players, message, send_running_tables=True, game_target=""): """Send running tables option is for integration where people don't want to see existing tables.""" bga_ids = [] tables = {} bga_account = BGAAccount() sent_messages = [] for player in players: if player.startswith("<@"): await message.channel.send("Not yet set up to read discord tags.") bga_account.close_connection() return bga_id = bga_account.get_player_id(player) if bga_id == -1: await message.channel.send( f"Player {player} is not a valid bga name.") bga_account.close_connection() return bga_ids.append(bga_id) player_tables = bga_account.get_tables(bga_id) found_msg = await message.channel.send( f"Found {str(len(player_tables))} tables for {player}") sent_messages += [found_msg] tables.update(player_tables) bga_games, err_msg = get_game_list() if len(err_msg) > 0: await message.channel.send(err_msg) return normalized_bga_games = [normalize_name(game) for game in bga_games] player_tables = [] for table_id in tables: table = tables[table_id] table_player_ids = table[ "player_display"] # Table.player_display is the player Ids at this table if set(bga_ids).issubset(table_player_ids): # match the game if a game was specified normalized_game_name = get_bga_alias(table["game_name"]) if len(game_target) == 0 or normalized_game_name == normalize_name( game_target): player_tables.append(table) for table in player_tables: sent_messages += [ await message.channel.send("Getting table information...") ] logger.debug( f"Checking table {table_id} for bga_ids {str(bga_ids)} in table {str(table)}" ) # Check for game name by id as it may differ from name (i.e. 7 vs 'seven') game_name_list = [ game for game in bga_games if table["game_id"] == str(bga_games[game]) ] if len(game_name_list) == 0: game_name = table["game_name"] new_game = {table["game_name"]: table["game_id"]} normalized_bga_games.append(normalize_name(table["game_name"])) update_games_cache(new_game) else: game_name = game_name_list[0] if normalize_name(game_name) not in normalized_bga_games: bga_account.close_connection() await message.channel.send(f"{game_name} is not a BGA game.") return # Only add table status lines for games we care about if len(game_target) > 0 and normalize_name( game_name) != normalize_name(game_target): continue if send_running_tables: await send_active_tables_list(message, bga_account, table, game_name) for sent_message in sent_messages: # Only delete all status messages once we're done await sent_message.delete() if len(player_tables) == 0: # Try to convert bga names to discord names players_list = [] for player_name in players: is_player_added = False if message.guild: player_id = get_discord_id(player_name, message) if player_id != -1: players_list.append(f"<@!{player_id}>") is_player_added = True elif not is_player_added: players_list.append(player_name) await message.channel.send( f"No {game_target} tables found for players [{', '.join(players_list)}]." ) bga_account.close_connection()
async def ctx_setup(message, contexts, args): """Provide the menu to do things with status.""" context = contexts[str(message.author)]["context"] if context == "setup": if message.content.isdigit( ) and message.content >= "1" and message.content <= "5": await parse_setup_menu(message, contexts) else: await message.channel.send( "Enter 1, 2, 3, 4, or 5 for the option in the embed above.") return # Will run on first setup menu run elif context == "": await send_main_setup_menu(message, contexts) elif context == "bga username": save_data(message.author.id, username=message.content) await message.channel.send(f"Username set to `{message.content}`") await send_main_setup_menu(message, contexts) elif context == "bga password": logins = get_all_logins() if not logins[str(message.author.id)]["username"]: await message.channel.send( "You must first enter your username before entering a password." ) contexts[str(message.author)]["context"] = "setup" return account = BGAAccount() login_successful = await account.login( logins[str(message.author.id)]["username"], message.content) await account.logout() if login_successful: save_data(message.author.id, password=message.content) await message.channel.send( "BGA username/password verified and password saved.") await send_main_setup_menu(message, contexts) else: await message.channel.send( "BGA did not like that username/password combination. Not saving password." ) contexts[str(message.author)]["context"] = "" await send_main_setup_menu(message, contexts) elif context == "bga global prefs": await ctx_bga_parse_options(message, contexts) elif context == "bga choose game prefs": game_name = message.content if await is_game_valid(game_name): contexts[str( message.author)]["bga prefs for game"] = normalize_name( game_name) await ctx_bga_options_menu(message, contexts, option_name=game_name + " option") else: await message.channel.send( f"{game_name} is not a valid game. Spelling matters, but not spaces, captilazition, or punctuation. Try again.", ) # BGA options/TFM options menu. Not checking input yet. else: async def save_pref_data(message, context, new_value, platform, game_prefs_name): options = {context: new_value} ret_msg = f"{context} successfully set to {new_value}" if platform == "bga" and game_prefs_name: ret_msg += f" for game {game_name}" save_data(message.author.id, bga_game_options={game_prefs_name: options}) elif platform == "bga": save_data(message.author.id, bga_global_options=options) elif platform == "tfm": save_data(message.author.id, tfm_global_options=options) await message.channel.send(ret_msg) game_prefs_name = "" if "bga prefs for game" in contexts[str(message.author)]: game_prefs_name = contexts[str( message.author)]["bga prefs for game"] is_interactive_session_over = True if context in ["presentation", "players", "restrictgroup", "lang"]: options = {context: message.content} await save_pref_data(message, options, message.content, "bga", game_prefs_name) elif context == "mode": new_value = MODE_VALUES[int(message.content) - 1] await save_pref_data(message, context, new_value, "bga", game_prefs_name) elif context == "speed": new_value = SPEED_VALUES[int(message.content) - 1] await save_pref_data(message, context, new_value, "bga", game_prefs_name) elif context == "karma": new_value = KARMA_VALUES[int(message.content) - 1] await save_pref_data(message, context, new_value, "bga", game_prefs_name) elif context == "min level": new_value = LEVEL_VALUES[int(message.content) - 1] await save_pref_data(message, context, new_value, "bga", game_prefs_name) elif context == "max level": new_value = LEVEL_VALUES[int(message.content) - 1] await save_pref_data(message, context, new_value, "bga", game_prefs_name) elif context == "tfm choose game prefs": pref_name = AVAILABLE_TFM_OPTIONS[int(message.content) - 1] await save_pref_data(message, pref_name, True, "tfm", game_prefs_name) else: is_interactive_session_over = False if is_interactive_session_over: # Keep on going until user hits cancel reset_context(contexts, message.author) await send_main_setup_menu(message, contexts)
def save_vote(red, green, blue, name): vote = Vote(red, green, blue, utils.normalize_name(name)) panchromious.db.session.add(vote) panchromious.app.logger.error('Saving vote to databse: %s', vote) panchromious.db.session.commit()
def test_name_normalisation(self): self.assertEqual(normalize_name('Ivan Petrov'), 'Иван Петров') self.assertEqual(normalize_name('ivan petrov'), 'Иван Петров') self.assertEqual(normalize_name('iVaN pEtRoV'), 'Иван Петров') self.assertEqual(normalize_name(' iVaN pEtRoV '), 'Иван Петров')
def delete(self, name): name = normalize_name(name) dataset_path = self.datasets_dir + name delete_dir(dataset_path) return