def main(): # questions = utils.path_for(train=True, question=True) # answers = utils.path_for(train=True, answer=True) # explanations = utils.path_for(train=True,explanation=True) # captions = utils.path_for(train=True,explanation=True) # data_path = utils_gqa.path_for(train=True, test=False, val=False) with open(data_path, 'r') as fd: data = json.load(fd) # with open(captions,'r') as fd: # captions = json.load(fd) questions = [data_item['question'] for qid, data_item in data.items()] answers = [data_item['answer'] for qid, data_item in data.items()] explanations = [data_item['fullAnswer'] for qid, data_item in data.items()] questions = list(data_gqa.prepare_questions(questions)) answers = list(data_gqa.prepare_answers(answers)) explanations = list(data_gqa.prepare_explanation(explanations)) #captions = data.prepare_captions(caption) question_vocab = extract_vocab(questions, start=0) answer_vocab = extract_vocab(answers, top_k=config.max_answers) explanations_vocab = extract_vocab(explanations, start=0) #captions_vocab = extract_vocab(captions) # vocabs = { # 'question': question_vocab, #} with open(config.gqa_question_vocabulary_path, 'w') as fd: json.dump(question_vocab, fd) with open(config.gqa_answer_vocabulary_path, 'w') as fd: json.dump(answer_vocab, fd) with open(config.gqa_explanation_vocabulary_path, 'w') as fd: json.dump(explanations_vocab, fd)
def parse(cmd: str): ''' parse a command ''' if 'debug.parse' in data.config: print('debug.parse: parsing', cmd) # judge exit if cmd == data.config['system.shell.exitCmd']: import os os._exit(0) # normal parse import translator cmd = cmd.split() # traverse actions for itemID in itemActions: for action in itemActions[itemID]: # judge cmd length if len(cmd) != len(action['name']): continue # match each part match = True params = {} for i in range(len(cmd)): if action['name'][i]['type'] == 'THIS': if cmd[i] != data.items[itemID]['name']: # can not match `this` match = False break elif action['name'][i]['type'].startswith('OBJECT'): # match params className = '' if len(action['name'][i]['type'].split('.')) > 1: # class name exists className = action['name'][i]['type'].split('.')[1] targetID = data.findItem(cmd[i], className) if targetID: # target exists, assign item to params params[action['name'][i]['value']] = data.items( targetID) else: match = False break elif action['name'][i]['type'] == 'ANY': params[action['name'][i]['value']] = cmd[i] else: # match literal text, action['name'][i]['type'] == 'LITERAL' if action['name'][i]['value'] != cmd[i]: match = False break if match: if 'debug.parse' in data.config: print('debug.parse: matching', action['name'], 'of', itemID) # process `this` params['this'] = data.items(itemID) return translator.run(action['code'], params) return False
def __init__(self, data): self._raw_data = data year = 2018 data_frame = {} for ticker, quantity in data.items(): data_frame[ticker] = Stock(ticker).adjuseted_pct_change(year-5, year) self._data_frame = pd.DataFrame(data_frame)
def loadCount(data): priors = {} for size, row in data.items(): row_info = makeStats(row) priors[size] = row_info return priors
def InsertData(self, data): items = data.items() for key, i in items: index = self.InsertStringItem(sys.maxint, i[0]) for k in range(1, len(i)): self.SetStringItem(index, k, i[k]) self.SetItemData(index, key)
def unmount(*items): ''' unmount `items` from shell so that shell can not parse their commands `items` can be a list of: - `str` as item ID - `list` as a list of item ID - `tuple` as a list of item ID - `dict` as an existing item ''' import translator for itemID in items: if isinstance(itemID, list) or isinstance(itemID, tuple): # not a str, process list or tuple recursively if len(itemID): unmount(*itemID) continue if isinstance(itemID, dict): unmount(itemID['id']) continue # itemID is a str, judge existence if itemID.startswith('@'): itemID = itemID[1:] if itemID not in itemActions: if 'debug.unmount' in data.config: print('debug.unmount:', itemID, 'not found in items') continue if 'debug.unmount' in data.config: print('debug.unmount: unloading', itemID) itemActions.pop(itemID) if 'onUnmount' in data.items[itemID]: translator.run(data.items[itemID]['onUnmount'], {'this': data.items(itemID)}) return True
def get_ids_from_property_value(data: dict, property_name: str, property_value: str, fix_data_delegate: Callable = None, match_exact: bool = False) -> list: # data structure: {id: content} # fixed_data structure: {description: id} if not data or not property_name or not property_value: print( f'- get_ids_from_property_value: invalid data or property info. Return empty list.' ) return [] if not fix_data_delegate: fix_data_delegate = _fix_property_value fixed_value = fix_data_delegate(property_value) fixed_data = { entry_id: fix_data_delegate(entry_data[property_name]) for entry_id, entry_data in data.items() if entry_data[property_name] } if match_exact: results = [ key for key, value in fixed_data.items() if value == property_value ] else: similarity_map = {} for entry_id, entry_property in fixed_data.items(): if entry_property.startswith( fixed_value) or fixed_value in entry_property: similarity_value = util.get_similarity(entry_property, fixed_value) if similarity_value in similarity_map.keys(): similarity_map[similarity_value].append( (entry_id, entry_property)) else: similarity_map[similarity_value] = [(entry_id, entry_property)] for similarity_value, entries in similarity_map.items(): similarity_map[similarity_value] = sorted( entries, key=lambda entry: entry[1]) similarity_values = sorted(list(similarity_map.keys()), reverse=True) results = [] for similarity_value in similarity_values: if not match_exact or (match_exact is True and similarity_value.is_integer()): entry_ids = [ entry_id for (entry_id, _) in similarity_map[similarity_value] ] results.extend(entry_ids) return results
async def vote_start(self): self.votes = {} reacts = random.choices(self.emojis, k=len(self.plays)) data = { reacts[i]: (user, fmt.phrase(phrase)) for i, (user, phrase) in enumerate(self.plays.items()) } table = [(f'{emoji}', phrase) for emoji, (user, phrase) in data.items()] return table, data, reacts
def populate(self, data, subtype = None): if not subtype: try: for key, value in data.items(): # key is subtype, value is data # ensure we don't pass a key of 'None' or will # loop forever (maybe?) if key is not None: self.populate(value, key) else: return False except ValueError as e: print("Invalid data passed, populate operation failed.") print("Error: {0} on data supplied \n {1}".format(e, data)) return False else: try: for key, ability in data.items(): # Built-in types only?; try: a = ATYPE_MAP[subtype](ability["name"]) except TypeError as e: # @TODO use correct error outputting print("subtype index is %s" % subtype) # Populate the Abilities properties from the JSON data. a.create(ability) # Add the ability to the appropriate collection self.add(a, subtype) except AttributeError as e: print("Invalid data passed, operation failed.") print("Error: {0} \n".format(e)) print(data) return False return True
def group_data_dict(data: dict, by_key, ignore_case: bool = False): """Parameter 'data': - A dict with entity ids as keys and entity info as values. """ if data: result = {} for key, entry in data.items(): entry_value = entry[by_key] if ignore_case: entry_value = str(entry_value).lower() if entry_value in result.keys(): result[entry_value][key] = entry else: new_group = {key: entry} result[entry_value] = new_group return result else: return data
def _filter_data_dict(data: dict, by_key, by_value, ignore_case: bool): """Parameter 'data': - A dict with entity ids as keys and entity info as values. """ if data: result = {} for key, entry in data.items(): entry_value = entry[by_key] value = by_value if ignore_case: entry_value = str(entry_value).lower() value = str(value).lower() if isinstance(by_value, list): if entry_value in value: result[key] = entry elif entry_value == value: result[key] = entry return result else: return data
def mount(*items): ''' mount `items` to shell so that shell can parse their commands `items` can be a list of: - `str` as item ID - `list` as a list of item ID - `tuple` as a list of item ID - `dict` as an existing item ''' import translator for itemID in items: if isinstance(itemID, list) or isinstance(itemID, tuple): # not a str, process list or tuple recursively if len(itemID): mount(*itemID) continue if isinstance(itemID, dict): mount(itemID['id']) continue # itemID is a str, judge `@` if itemID.startswith('@'): itemID = itemID[1:] # judge existence if itemID not in data.items: if 'debug.mount' in data.config: print('debug.mount:', itemID, 'not exist in items') continue if 'actions' not in data.items[itemID]: continue # add actions if 'debug.mount' in data.config: print('debug.mount: loading', itemID) itemActions[itemID] = data.items[itemID]['actions'] if 'onMount' in data.items[itemID]: translator.run(data.items[itemID]['onMount'], {'this': data.items(itemID)})
def run(alist, player = Player()): # Build wheel. This should be auto-matic when synergy starts, so # potentially global? wheel = game.abilities.AbilityList() # populate this ability list with everything wheel.populate(data.load()) # debugging; checking wheel #print (wheel) # Test (remove to actual wheel creation test later...) if len(wheel.actives) == 0: print("Empty Wheel") return 0 # fail ## Set All variables @TODO make external variables # test variables @TODO make part of test object COMBAT_TIME = 30 CRIT_CHANCE = 20 # set to a number to default back to CRIT_POWER = 40 for a in alist: # Load ability from wheel elitePassive = wheel.get(a) # Fail if we don't retrieve the ability if elitePassive is None: # throw TestFailException print("Test Failed: Could not retrieve ability from wheel [%s]\n" % a) return 0 print("Testing with: {0}".format(elitePassive.name)) # Create player object and add the elitePassive - this will replace # any other elite passive as there can only be one. player.deck().add(elitePassive) # Calculate theoretical base DPS # ...No procs or bleeds? base_dps = player.base_tdps() base_damage = player.base_tdps() * COMBAT_TIME if player.stats() is not None: crit_chance = player.stats().crit crit_power = player.stats().crit_power else: crit_chance = CRIT_CHANCE crit_power = CRIT_POWER # if player stats aren't initialized we have to add # extra crit chance from rapid getaway if a == "Rapid Getaway": crit_chance += 10 if player.signets() and player.signets("laceration"): v = data.items( "signet", "laceration", player.signets("laceration").quality) #uptime = formulae.buffs.laceration_uptime(player) uptime = 1 # assume for the moment @TODO create laceration_uptime crit_power += (v * uptime) # the below assumes one hit = one sec. Not always true # @TODO adjust below to account for multi hit attacks (use furmulae) total_crits = COMBAT_TIME * (crit_chance/100) # @TODO replace below with damage formulae total_crit_damage = base_damage * ((crit_chance/100.0) * crit_power) # @TODO all proc damage calculation should be done in a formulae # subroutine: # game.formulae.proc_damage_per_crit(player) # would work something like - # if (passive)ability.requires()["attack"]["critical"] # and provide()["damage"] # add to AbilityList # check each item in list against player Deck, keep only # those that exist proc_dmg_per_crit = 0 if player.using_ability("One in the Chamber"): proc_dmg_per_crit = PROCS.ONE_IN_THE_CHAMBER if player.using_ability("Thunderstruck"): proc_dmg_per_crit = int(wheel.get("Thunderstruck").provides["damage"]["amount"]) total_proc_damage = proc_dmg_per_crit * int(total_crits) print("Base Damage: %d \n" % base_damage) print("Crit Damage: {0} ({1}% Crit) \n".format(int(total_crit_damage), crit_chance)) print("Proc Damage: %d \n" % total_proc_damage) # end loop return 1
""" Family Management application - rschwalk 2018 The main file for the Flask application. """ from flask import Flask, render_template, flash, redirect, url_for, session, logging, request from wtforms import Form, TextAreaField, StringField, PasswordField, validators from passlib.hash import sha256_crypt from data import items, UserData app = Flask(__name__) DATA_ITEMS = items() # DATA_USERS = UserData().read_all() @app.route('/') def home(): """ Home page """ return render_template('home.html') @app.route('/about') def about(): """ About page """ return render_template('about.html') @app.route('/items') def items_page(): """ Items page """ return render_template('items.html', items=DATA_ITEMS)
if isinstance(prop.type, str): prop.type = objects[prop.type] if isinstance(prop.qualifier, str): prop.qualifier = objects[prop.qualifier] with open('projects.yml') as f: projects = list(yaml.load_all(f)) for project in projects: for thing, props in (project['data-needed'] or {}).items(): obj = objects[thing] for name in props: try: prop = obj[name] except AttributeError as e: raise Error(project, e) print(project['title'], obj.meta.title, name) if __name__ == "__main__": yaml.Dumper.add_representer( collections.OrderedDict, lambda dumper, data: dumper.represent_dict(data.items())) yaml.Loader.add_constructor( yaml.resolver.BaseResolver.DEFAULT_MAPPING_TAG, lambda loader, node: collections.OrderedDict( loader.construct_pairs(node))) main()
def get_ids_from_property_value(data: dict, property_name: str, property_value: str, fix_data_delegate: Callable = None, return_on_first: bool = True) -> list: # data structure: {id: content} # fixed_data structure: {description: id} if not data or not property_name or not property_value: print(f'- get_ids_from_property_value: invalid data or property info. Return empty list.') return [] if not fix_data_delegate: fix_data_delegate = fix_property_value fixed_value = fix_data_delegate(property_value) fixed_data = {entry_id: fix_data_delegate(entry_data[property_name]) for entry_id, entry_data in data.items() if entry_data[property_name]} results = [] results.extend([entry_id for entry_id, entry_property in fixed_data.items() if entry_property.startswith(fixed_value)]) results.extend([entry_id for entry_id, entry_property in fixed_data.items() if fixed_value in entry_property]) results = list(set(results)) if results and return_on_first: similarity_data = {key: fix_data_delegate(value[property_name]) for key, value in data.items() if key in results} similarity_map = util.get_similarity(similarity_data, fixed_value) max_similarity = max(similarity_map.values()) best_hits = [key for key, value in similarity_map.items() if value == max_similarity] return best_hits return results