def search(cls, words): doc_array = None words = [to_ascii(w).lower() for w in words] for word in words: kw = Keyword.get(word, readonly=True) if not kw: return if doc_array is None: doc_array = kw.documents else: doc_array &= kw.documents limit, offset = limit_offset() ids = list(ranks(doc_array)) ctx.cursor.execute( 'SELECT id, uri, content from document ' \ 'WHERE content is not null and id in (%s) ' \ 'ORDER BY score desc limit %s offset %s' % ( ','.join(str(i) for i in ids), limit, offset) ) for doc_id, uri, content in ctx.cursor: match = None for line in decompress(content).splitlines(): idx = to_ascii(line).lower().find(words[0]) if idx < 0: continue match = get_match_context(idx, line) break yield doc_id, uri, match, content
def fields_from_json(src_json, linetrans = True): parsed = True valid = True fields = {} # we hardcode in what the things are called in the mtgjson format if 'name' in src_json: name_val = src_json['name'].lower() name_orig = name_val name_val = transforms.name_pass_1_sanitize(name_val) name_val = utils.to_ascii(name_val) fields[field_name] = [(-1, name_val)] else: name_orig = '' parsed = False if 'cost' in src_json: cost_val = str(src_json['cost']) fields[field_cost] = [(-1, cost_val)] if 'cardClass' in src_json: fields[field_cardClass] = [(-1, src_json['cardClass'])] if 'type' in src_json: fields[field_type] = [(-1, src_json['type'])] else: parsed = False noRarity = 'N' if 'rarity' in src_json: if src_json['rarity'] in utils.json_rarity_map: fields[field_rarity] = [(-1, utils.json_rarity_map[src_json['rarity']])] else: fields[field_rarity] = [(-1, src_json['rarity'])] parsed = False else: fields[field_rarity] = [(-1, noRarity)] a_h = '' parsed_ah = True if 'attack' in src_json: a_h = str(src_json['attack']) + '/' # hardcoded parsed_ah = False if 'health' in src_json: a_h = a_h + str(src_json['health']) parsed_ah = True elif 'health' in src_json: a_h = '/' + str(src_json['health']) # hardcoded parsed_ah = False if a_h: fields[field_ah] = [(-1, a_h)] parsed = parsed and parsed_ah if 'text' in src_json: text_val = utils.to_ascii(src_json['text']) fields[field_text] = [(-1, text_val)] # we don't need to worry about bsides because we handle that in the constructor return parsed, valid and fields_check_valid(fields), fields
def test_to_ascii(self): """Test to_ascii method.""" parsed_str = utils.to_ascii('abc') self.assertEqual(parsed_str, 'abc') parsed_str = utils.to_ascii(u'¡Hola!') self.assertEqual(parsed_str, 'Hola!') parsed_str = utils.to_ascii( u'Klüft skräms inför på fédéral électoral große') self.assertEqual( parsed_str, 'Kluft skrams infor pa federal electoral groe') parsed_str = utils.to_ascii('') self.assertEqual(parsed_str, '')
def test_to_ascii(self): """Test to_ascii method.""" parsed_str = utils.to_ascii('abc') self.assertEqual(parsed_str, 'abc') parsed_str = utils.to_ascii(u'¡Hola!') self.assertEqual(parsed_str, 'Hola!') parsed_str = utils.to_ascii( u'Klüft skräms inför på fédéral électoral große') self.assertEqual(parsed_str, 'Kluft skrams infor pa federal electoral groe') parsed_str = utils.to_ascii('') self.assertEqual(parsed_str, '')
def get(self, exploration_id): """Handles GET requests.""" exploration = exp_fetchers.get_exploration_by_id(exploration_id) version = self.normalized_request.get('v') output_format = self.normalized_request.get('output_format') if version is None: version = exploration.version # If the title of the exploration has changed, we use the new title. if not exploration.title: init_filename = 'oppia-unpublished_exploration-v%s.zip' % version else: init_filename = 'oppia-%s-v%s.zip' % (exploration.title.replace( ' ', ''), version) filename = utils.to_ascii(init_filename) if output_format == feconf.OUTPUT_FORMAT_ZIP: self.render_downloadable_file( exp_services.export_to_zip_file(exploration_id, version=version), filename, 'text/plain') elif output_format == feconf.OUTPUT_FORMAT_JSON: self.render_json( exp_services.export_states_to_yaml(exploration_id, version=version))
def get(self, exploration_id): """Handles GET requests.""" try: exploration = exp_services.get_exploration_by_id(exploration_id) except: raise self.PageNotFoundException version = self.request.get('v', default_value=exploration.version) output_format = self.request.get('output_format', default_value='zip') width = int(self.request.get('width', default_value=80)) # If the title of the exploration has changed, we use the new title. filename = 'oppia-%s-v%s' % (utils.to_ascii( exploration.title.replace(' ', '')), version) if output_format == feconf.OUTPUT_FORMAT_ZIP: self.response.headers['Content-Type'] = 'text/plain' self.response.headers['Content-Disposition'] = ( 'attachment; filename=%s.zip' % str(filename)) self.response.write( exp_services.export_to_zip_file(exploration_id, version)) elif output_format == feconf.OUTPUT_FORMAT_JSON: self.render_json( exp_services.export_states_to_yaml(exploration_id, version=version, width=width)) else: raise self.InvalidInputException('Unrecognized output format %s' % output_format)
def create_rule_name(rule): name = rule.name for key in rule.inputs: left_paren = name.index('(') name = name[0:left_paren] + name[left_paren:].replace( key, utils.to_ascii(rule.inputs[key])) return name
def get(self, exploration_id): """Handles GET requests.""" exploration = exp_fetchers.get_exploration_by_id(exploration_id) version_str = self.request.get('v', default_value=exploration.version) output_format = self.request.get('output_format', default_value='zip') try: version = int(version_str) except ValueError: version = exploration.version # If the title of the exploration has changed, we use the new title. if not exploration.title: init_filename = 'oppia-unpublished_exploration-v%s.zip' % version else: init_filename = 'oppia-%s-v%s.zip' % ( exploration.title.replace(' ', ''), version) filename = utils.to_ascii(init_filename).decode('utf-8') if output_format == feconf.OUTPUT_FORMAT_ZIP: self.render_downloadable_file( exp_services.export_to_zip_file( exploration_id, version=version), filename, 'text/plain') elif output_format == feconf.OUTPUT_FORMAT_JSON: self.render_json(exp_services.export_states_to_yaml( exploration_id, version=version)) else: raise self.InvalidInputException( 'Unrecognized output format %s' % output_format)
def get(self, exploration_id): """Handles GET requests.""" try: exploration = exp_services.get_exploration_by_id(exploration_id) except: raise self.PageNotFoundException if not rights_manager.Actor(self.user_id).can_view( rights_manager.ACTIVITY_TYPE_EXPLORATION, exploration_id): raise self.PageNotFoundException version = self.request.get('v', default_value=exploration.version) output_format = self.request.get('output_format', default_value='zip') width = int(self.request.get('width', default_value=80)) # If the title of the exploration has changed, we use the new title filename = 'oppia-%s-v%s' % ( utils.to_ascii(exploration.title.replace(' ', '')), version) if output_format == feconf.OUTPUT_FORMAT_ZIP: self.response.headers['Content-Type'] = 'text/plain' self.response.headers['Content-Disposition'] = ( 'attachment; filename=%s.zip' % str(filename)) self.response.write( exp_services.export_to_zip_file(exploration_id, version)) elif output_format == feconf.OUTPUT_FORMAT_JSON: self.render_json(exp_services.export_states_to_yaml( exploration_id, version=version, width=width)) else: raise self.InvalidInputException( 'Unrecognized output format %s' % output_format)
def __str__(self): """Returns a string representation of a rule (for the stats log).""" if self.definition['rule_type'] == rule_domain.DEFAULT_RULE_TYPE: return 'Default' else: # TODO(sll): Treat non-atomic rules too. param_list = [utils.to_ascii(val) for (key, val) in self.definition['inputs'].iteritems()] return '%s(%s)' % (self.definition['name'], ','.join(param_list))
def load(uri): """ return: content, words, links. """ content_type = None charset = None if uri == ':firefox': links = load_firefox_places() return None, None, list(links) scheme, *_ = urlsplit(uri) if scheme: try: uri = to_ascii(uri).decode() f = request.urlopen(uri, timeout=5) except (URLError, socket.error) as e: log('Unable to download %s' % uri, color='red') return None, None, None info = f.info() content_type = info.get_content_type() if content_type == 'application/pdf': log('PDF dowload not yet supported (%s)' % uri, color='brown') return None, None, None charset = info.get_charset() or 'ISO-8859-1' data = f.read().decode(charset) else: data = read_data(uri) if uri.endswith('.html'): content_type = 'text/html' elif uri.endswith('.pdf'): content_type = 'application/pdf' else: content_type = 'text/plain' if not data: log('No content in %s' % uri, color='red') return None, None, None if content_type == 'text/html': return parse_html(data, uri) elif content_type == 'text/plain': return parse_text(data) elif content_type == 'application/pdf': return parse_pdf_file(uri) else: log('Content Type "%s" not supported (%s)' % (content_type, uri), color='brown') return None, None, None
def get(self, exploration): """Handles GET requests.""" filename = 'oppia-%s' % utils.to_ascii(exploration.title) if not filename: filename = feconf.DEFAULT_FILE_NAME self.response.headers['Content-Type'] = 'text/plain' self.response.headers['Content-Disposition'] = ( 'attachment; filename=%s.txt' % filename) self.response.write(exp_services.export_to_yaml(exploration.id))
def get(self, exploration_id): """Handles GET requests.""" exploration = exp_services.get_exploration_by_id(exploration_id) filename = 'oppia-%s-v%s' % ( utils.to_ascii(exploration.title), exploration.version) self.response.headers['Content-Type'] = 'text/plain' self.response.headers['Content-Disposition'] = ( 'attachment; filename=%s.yaml' % filename) self.response.write(exp_services.export_to_yaml(exploration_id))
def get(self, unused_user, exploration): """Handles GET requests.""" filename = 'oppia-%s' % utils.to_ascii(exploration.title) if not filename: filename = feconf.DEFAULT_FILE_NAME self.response.headers['Content-Type'] = 'text/plain' self.response.headers['Content-Disposition'] = ( 'attachment; filename=%s.txt' % filename) # TODO(sll): Cache the YAML file. self.response.write(exploration.as_yaml())
def get_first_and_last_name(self): info = self.get_soup().find('dl') headings = [x.text.encode('utf-8') for x in info.findAll('dt')] values = [x.text.encode('utf-8') for x in info.findAll('dd')] attribs = dict(zip(headings, values)) req_strs = {'First name', 'Last name'} for x in req_strs: if not x in attribs.keys(): return 'Nil', 'Nil' return [utils.to_ascii(attribs[x]) for x in req_strs]
def visualizar_arquivo(request, arquivo_id): arquivo = Arquivo.objects.all().get(id=arquivo_id) arquivo_tmp = arquivo.load() if arquivo_tmp is not None: response = HttpResponse(arquivo_tmp.read(), content_type=arquivo.content_type()) response['Content-Disposition'] = 'filename=%s' % \ to_ascii(arquivo.nome.replace(u'º', '.').replace('/', '-').replace(' ', '')) arquivo_tmp.close() os.remove(arquivo_tmp.name) return response else: return HttpResponse("O arquivo solicitado foi adulterado ou não existe.")
def get(self, exploration_id): """Handles GET requests.""" exploration = exp_services.get_exploration_by_id(exploration_id) version = self.request.get('v', default_value=exploration.version) # If the title of the exploration has changed, we use the new title filename = 'oppia-%s-v%s' % (utils.to_ascii( exploration.title.replace(' ', '')), version) self.response.headers['Content-Type'] = 'text/plain' self.response.headers['Content-Disposition'] = ( 'attachment; filename=%s.zip' % str(filename)) self.response.write( exp_services.export_to_zip_file(exploration_id, version))
def get(self, exploration_id): """Handles GET requests.""" exploration = exp_services.get_exploration_by_id(exploration_id) version = self.request.get('v', default_value=exploration.version) # If the title of the exploration has changed, we use the new title filename = 'oppia-%s-v%s' % ( utils.to_ascii(exploration.title.replace(' ', '')), version) self.response.headers['Content-Type'] = 'text/plain' self.response.headers['Content-Disposition'] = ( 'attachment; filename=%s.zip' % str(filename)) self.response.write( exp_services.export_to_zip_file(exploration_id, version))
def format_csv(self, f): """ Custom generator to prevent bad lines from being imported. """ f.seek(0) acc_regex = re.compile('^[a-zA-Z]([0-9]{9})$') for line in f: data = self.clean_line(line) if len(data) == 4: try: account = data[1] data[1] = to_ascii(data[1]) if acc_regex.match(account): yield dict(zip(self.COLUMNS, tuple([self.fix_sign(c) for c in data]))) except KeyError: pass
def neighbours(cls, words): kw_array = None for word in words: word = to_ascii(word).lower() kw = Keyword.get(word, readonly=True) if not kw: return if kw_array is None: kw_array = kw.neighbours else: kw_array &= kw.neighbours ids = list(ranks(kw_array)) ctx.cursor.execute( 'SELECT score, word from keyword ' \ 'WHERE id in (%s) '\ 'ORDER BY score asc limit 30' % \ ','.join(str(i) for i in ids)) yield from ctx.cursor
def store_plaintxt(): with open(filename) as f, open(check_filename, "w+") as g: ciphertxt = b64decode(f.read()) plaintxt = to_ascii(cbc_mode(ciphertxt, KEY, IV)) print(f"writing... {plaintxt}") g.write(plaintxt)
def fields_from_json(src_json, linetrans=True): parsed = True valid = True fields = {} # we hardcode in what the things are called in the mtgjson format if 'name' in src_json: name_val = src_json['name'].lower() name_orig = name_val name_val = transforms.name_pass_1_sanitize(name_val) name_val = utils.to_ascii(name_val) fields[field_name] = [(-1, name_val)] else: name_orig = '' parsed = False # return the actual Manacost object if 'manaCost' in src_json: cost = Manacost(src_json['manaCost'], fmt='json') valid = valid and cost.valid parsed = parsed and cost.parsed fields[field_cost] = [(-1, cost)] if 'supertypes' in src_json: fields[field_supertypes] = [(-1, map(lambda s: utils.to_ascii(s.lower()), src_json['supertypes']))] if 'types' in src_json: fields[field_types] = [(-1, map(lambda s: utils.to_ascii(s.lower()), src_json['types']))] else: parsed = False if 'subtypes' in src_json: fields[field_subtypes] = [( -1, map( lambda s: utils.to_ascii(s.lower()) # urza's lands... .replace('"', "'").replace('-', utils.dash_marker), src_json['subtypes']))] if 'rarity' in src_json: if src_json['rarity'] in utils.json_rarity_map: fields[field_rarity] = [ (-1, utils.json_rarity_map[src_json['rarity']]) ] else: fields[field_rarity] = [(-1, src_json['rarity'])] parsed = False else: parsed = False if 'loyalty' in src_json: fields[field_loyalty] = [(-1, utils.to_unary(str(src_json['loyalty']))) ] p_t = '' parsed_pt = True if 'power' in src_json: p_t = utils.to_ascii(utils.to_unary( src_json['power'])) + '/' # hardcoded parsed_pt = False if 'toughness' in src_json: p_t = p_t + utils.to_ascii(utils.to_unary(src_json['toughness'])) parsed_pt = True elif 'toughness' in src_json: p_t = '/' + utils.to_ascii(utils.to_unary( src_json['toughness'])) # hardcoded parsed_pt = False if p_t: fields[field_pt] = [(-1, p_t)] parsed = parsed and parsed_pt # similarly, return the actual Manatext object if 'text' in src_json: text_val = src_json['text'].lower() text_val = transforms.text_pass_1_strip_rt(text_val) text_val = transforms.text_pass_2_cardname(text_val, name_orig) text_val = transforms.text_pass_3_unary(text_val) text_val = transforms.text_pass_4a_dashes(text_val) text_val = transforms.text_pass_4b_x(text_val) text_val = transforms.text_pass_5_counters(text_val) text_val = transforms.text_pass_6_uncast(text_val) text_val = transforms.text_pass_7_choice(text_val) text_val = transforms.text_pass_8_equip(text_val) text_val = transforms.text_pass_9_newlines(text_val) text_val = transforms.text_pass_10_symbols(text_val) if linetrans: text_val = transforms.text_pass_11_linetrans(text_val) text_val = utils.to_ascii(text_val) text_val = text_val.strip() mtext = Manatext(text_val, fmt='json') valid = valid and mtext.valid fields[field_text] = [(-1, mtext)] # we don't need to worry about bsides because we handle that in the constructor return parsed, valid and fields_check_valid(fields), fields
def fields_from_json(src_json, linetrans = True, addspaces = False): parsed = True valid = True fields = {} # we hardcode in what the things are called in the mtgjson format if 'name' in src_json: name_val = src_json['name'].lower() name_orig = name_val name_val = transforms.name_pass_1_sanitize(name_val) name_val = utils.to_ascii(name_val) #RMM: Testing out the idea of chopping up names. if addspaces: def intersperse(lst, item): result = [item] * (len(lst) * 2 - 1) result[0::2] = lst return result name_val = intersperse(name_val, ' ') fields[field_name] = [(-1, name_val)] else: name_orig = '' parsed = False # return the actual Manacost object if 'manaCost' in src_json: cost = Manacost(src_json['manaCost'], fmt = 'json') valid = valid and cost.valid parsed = parsed and cost.parsed fields[field_cost] = [(-1, cost)] if 'supertypes' in src_json: fields[field_supertypes] = [(-1, [utils.to_ascii(s.lower()) for s in src_json['supertypes']])] if 'types' in src_json: fields[field_types] = [(-1, [utils.to_ascii(s.lower()) for s in src_json['types']])] else: parsed = False if 'subtypes' in src_json: fields[field_subtypes] = [(-1, [utils.to_ascii(s.lower()) # urza's lands... .replace('"', "'").replace('-', utils.dash_marker) for s in src_json['subtypes']])] if 'rarity' in src_json: if src_json['rarity'] in utils.json_rarity_map: fields[field_rarity] = [(-1, utils.json_rarity_map[src_json['rarity']])] else: fields[field_rarity] = [(-1, src_json['rarity'])] parsed = False else: parsed = False if 'loyalty' in src_json: fields[field_loyalty] = [(-1, utils.to_unary(str(src_json['loyalty'])))] p_t = '' parsed_pt = True if 'power' in src_json: p_t = utils.to_ascii(utils.to_unary(src_json['power'])) + ' / ' # hardcoded parsed_pt = False if 'toughness' in src_json: p_t = p_t + utils.to_ascii(utils.to_unary(src_json['toughness'])) parsed_pt = True elif 'toughness' in src_json: p_t = ' / ' + utils.to_ascii(utils.to_unary(src_json['toughness'])) # hardcoded parsed_pt = False if p_t: fields[field_pt] = [(-1, p_t)] parsed = parsed and parsed_pt # similarly, return the actual Manatext object if 'text' in src_json: text_val = src_json['text'].lower() text_val = transforms.text_pass_1_strip_rt(text_val) text_val = transforms.text_pass_2_cardname(text_val, name_orig) text_val = transforms.text_pass_3_unary(text_val) text_val = transforms.text_pass_4a_dashes(text_val) text_val = transforms.text_pass_4b_x(text_val) text_val = transforms.text_pass_5_counters(text_val) text_val = transforms.text_pass_6_uncast(text_val) text_val = transforms.text_pass_7_choice(text_val) text_val = transforms.text_pass_8_equip(text_val) text_val = transforms.text_pass_9_newlines(text_val) text_val = transforms.text_pass_10_symbols(text_val) if linetrans: text_val = transforms.text_pass_11_linetrans(text_val) if addspaces: text_val = transforms.text_pass_12_addspaces(text_val) text_val = utils.to_ascii(text_val) text_val = text_val.strip() mtext = Manatext(text_val, fmt = 'json') valid = valid and mtext.valid fields[field_text] = [(-1, mtext)] # we don't need to worry about bsides because we handle that in the constructor return parsed, valid and fields_check_valid(fields), fields
def __is_same_league(self, season_tag, league_name): link = season_tag('a')[2] if utils.to_ascii(link.get('title').encode('utf-8')) == league_name: return True return False
def get_words(data): for w in word_re.split(data): w = to_ascii(w).lower() w.strip() if 1 < len(w) < 20: yield w
def test_challenge_13(): from c13 import change_role from utils import to_ascii, depad assert "role=admin" in to_ascii(depad(change_role()))
return ''.join([f"{item}={profile[item]}&" for item in profile])[:-1] def test_parse(): profile = parse_profile("foo=bar&baz=qux&zap=zazzle") assert profile == {"foo": "bar", "baz": "qux", "zap": "zazzle"} assert unparse_profile(profile) == "foo=bar&baz=qux&zap=zazzle" def profile_for(email_addr: str) -> (str, bytes): assert type(email_addr) == str email_addr = email_addr.strip("&").strip("=") uid = 10 role = "user" key = generate_key() profile = parse_profile(f"email={email_addr}&uid={uid}&role={role}") a = AES.new(key, AES.MODE_ECB) return key, a.encrypt(pad(bytes(unparse_profile(profile), "utf-8"))) def change_role() -> str: key, ciphertext = profile_for("*****@*****.**") a = AES.new(key, AES.MODE_ECB) plaintext = to_ascii(depad(a.decrypt(ciphertext))) plaintext = plaintext.replace("user", "admin") return a.decrypt(a.encrypt(pad(bytes(plaintext, "utf-8")))) if __name__ == "__main__": print(to_ascii(depad(change_role())))
def change_role() -> str: key, ciphertext = profile_for("*****@*****.**") a = AES.new(key, AES.MODE_ECB) plaintext = to_ascii(depad(a.decrypt(ciphertext))) plaintext = plaintext.replace("user", "admin") return a.decrypt(a.encrypt(pad(bytes(plaintext, "utf-8"))))
def fields_from_json(src_json, linetrans=True): parsed = True valid = True fields = {} # we hardcode in what the things are called in the mtgjson format if "name" in src_json: name_val = src_json["name"].lower() name_orig = name_val name_val = transforms.name_pass_1_sanitize(name_val) name_val = utils.to_ascii(name_val) fields[field_name] = [(-1, name_val)] else: name_orig = "" parsed = False # return the actual Manacost object if "manaCost" in src_json: cost = Manacost(src_json["manaCost"], fmt="json") valid = valid and cost.valid parsed = parsed and cost.parsed fields[field_cost] = [(-1, cost)] if "supertypes" in src_json: fields[field_supertypes] = [(-1, map(lambda s: utils.to_ascii(s.lower()), src_json["supertypes"]))] if "types" in src_json: fields[field_types] = [(-1, map(lambda s: utils.to_ascii(s.lower()), src_json["types"]))] else: parsed = False if "subtypes" in src_json: fields[field_subtypes] = [ ( -1, map( lambda s: utils.to_ascii(s.lower()) # urza's lands... .replace('"', "'").replace("-", utils.dash_marker), src_json["subtypes"], ), ) ] if "rarity" in src_json: if src_json["rarity"] in utils.json_rarity_map: fields[field_rarity] = [(-1, utils.json_rarity_map[src_json["rarity"]])] else: fields[field_rarity] = [(-1, src_json["rarity"])] parsed = False else: parsed = False if "loyalty" in src_json: fields[field_loyalty] = [(-1, utils.to_unary(str(src_json["loyalty"])))] p_t = "" parsed_pt = True if "power" in src_json: p_t = utils.to_ascii(utils.to_unary(src_json["power"])) + "/" # hardcoded parsed_pt = False if "toughness" in src_json: p_t = p_t + utils.to_ascii(utils.to_unary(src_json["toughness"])) parsed_pt = True elif "toughness" in src_json: p_t = "/" + utils.to_ascii(utils.to_unary(src_json["toughness"])) # hardcoded parsed_pt = False if p_t: fields[field_pt] = [(-1, p_t)] parsed = parsed and parsed_pt # similarly, return the actual Manatext object if "text" in src_json: text_val = src_json["text"].lower() text_val = transforms.text_pass_1_strip_rt(text_val) text_val = transforms.text_pass_2_cardname(text_val, name_orig) text_val = transforms.text_pass_3_unary(text_val) text_val = transforms.text_pass_4a_dashes(text_val) text_val = transforms.text_pass_4b_x(text_val) text_val = transforms.text_pass_5_counters(text_val) text_val = transforms.text_pass_6_uncast(text_val) text_val = transforms.text_pass_7_choice(text_val) text_val = transforms.text_pass_8_equip(text_val) text_val = transforms.text_pass_9_newlines(text_val) text_val = transforms.text_pass_10_symbols(text_val) if linetrans: text_val = transforms.text_pass_11_linetrans(text_val) text_val = utils.to_ascii(text_val) text_val = text_val.strip() mtext = Manatext(text_val, fmt="json") valid = valid and mtext.valid fields[field_text] = [(-1, mtext)] # we don't need to worry about bsides because we handle that in the constructor return parsed, valid and fields_check_valid(fields), fields
def symbol(out): state = out.splitlines()[1].split(" ")[0] if state in symbols: return "<icon=%s.xbm/>" % symbols[state] else: return "" def position(out): split = out.splitlines()[1].split(" ") found = 0 for s in split: if not s == "": if found == 2: return s else: found += 1 return "" if __name__ == "__main__": out, success = utils.call_command(["mpc", "-h", host, "-f", format]) out = utils.to_ascii(out) if not success: print(error_message) elif len(out.splitlines()) == 1: print(stopped_message) else: formatted = out.splitlines()[0] print("%s %s (%s)" % (symbol(out), formatted, position(out)))
while (ret_unicode != "y" and ret_unicode != "n"): ret_unicode = raw_input("Voulez vous les accents ? (y/n)\n") # ret_tiret = None # while(ret_tiret != "y" and ret_tiret != "n") : # ret_tiret = raw_input("Voulez vous compléter les espaces par des tirets ? (y/n)\n") string = "" for c in ret_classes: classe = dico.get_classe(tab[int(c)][1]) if classe != None: string += classe.to_string("default") if ret_unicode == "n": #on ne veut pas les accents string = utils.to_ascii(string) # if ret_tiret == "y" : # #on veut les tirets # string = utils.with_underscore(string) fichier = open(NOM_FILE_OUT, 'w') fichier.write(string) fichier.close() else: print "Error nombre entré" exit(1) with open(PATH_DICO, 'wb') as fichier: pickle.dump(dico, fichier)
def fields_from_json(src_json): parsed = True valid = True fields = {} # we hardcode in what the things are called in the mtgjson format if 'name' in src_json: name_val = src_json['name'].lower() name_orig = name_val name_val = transforms.name_pass_1_sanitize(name_val) name_val = utils.to_ascii(name_val) fields[field_name] = [(-1, name_val)] else: name_orig = '' parsed = False # return the actual Manacost object if 'manaCost' in src_json: cost = Manacost(src_json['manaCost'], fmt = 'json') valid = valid and cost.valid parsed = parsed and cost.parsed fields[field_cost] = [(-1, cost)] if 'supertypes' in src_json: fields[field_supertypes] = [(-1, map(lambda s: utils.to_ascii(s.lower()), src_json['supertypes']))] if 'types' in src_json: fields[field_types] = [(-1, map(lambda s: utils.to_ascii(s.lower()), src_json['types']))] else: parsed = False if 'subtypes' in src_json: fields[field_subtypes] = [(-1, map(lambda s: utils.to_ascii(s.lower()), src_json['subtypes']))] if 'rarity' in src_json: if src_json['rarity'] in utils.json_rarity_map: fields[field_rarity] = [(-1, utils.json_rarity_map[src_json['rarity']])] else: fields[field_rarity] = [(-1, src_json['rarity'])] parsed = False else: parsed = False if 'loyalty' in src_json: fields[field_loyalty] = [(-1, utils.to_unary(str(src_json['loyalty'])))] p_t = '' parsed_pt = True if 'power' in src_json: p_t = utils.to_ascii(utils.to_unary(src_json['power'])) + '/' # hardcoded parsed_pt = False if 'toughness' in src_json: p_t = p_t + utils.to_ascii(utils.to_unary(src_json['toughness'])) parsed_pt = True elif 'toughness' in src_json: p_t = '/' + utils.to_ascii(utils.to_unary(src_json['toughness'])) # hardcoded parsed_pt = False if p_t: fields[field_pt] = [(-1, p_t)] parsed = parsed and parsed_pt # similarly, return the actual Manatext object if 'text' in src_json: text_val = src_json['text'].lower() text_val = transforms.text_pass_1_strip_rt(text_val) text_val = transforms.text_pass_2_cardname(text_val, name_orig) text_val = transforms.text_pass_3_unary(text_val) text_val = transforms.text_pass_4a_dashes(text_val) text_val = transforms.text_pass_4b_x(text_val) text_val = transforms.text_pass_5_counters(text_val) text_val = transforms.text_pass_6_uncast(text_val) text_val = transforms.text_pass_7_choice(text_val) text_val = transforms.text_pass_8_equip(text_val) text_val = transforms.text_pass_9_newlines(text_val) text_val = transforms.text_pass_10_symbols(text_val) text_val = utils.to_ascii(text_val) text_val = text_val.strip() mtext = Manatext(text_val, fmt = 'json') valid = valid and mtext.valid fields[field_text] = [(-1, mtext)] # we don't need to worry about bsides because we handle that in the constructor return parsed, valid and fields_check_valid(fields), fields
def row_iterator(self, row, *args, **kwargs): return StudentEnrollment(student=to_ascii(row[self.STUDENT_INDEX]), course=row[self.COURSE_INDEX], grade=to_grade_number(row[self.GRADE_INDEX]))
def get_username(self): return to_ascii( re.sub(r'\w\. ', '', self.firstname).split(' ')[0].lower() + '.' + re.sub(r'\w\. ', '', self.sirname.replace('von ', '')).split( ' ')[0].lower()).lower()
def implement_cbc_mode(): with open(filename) as f: ciphertxt = b64decode(f.read()) plaintxt = to_ascii(cbc_mode(ciphertxt, KEY, IV)) return plaintxt
def row_iterator(self, row): return Student(shortname=to_ascii(row[self.SHORTNAME_INDEX]), sirname=row[self.SIRNAME_INDEX], firstname=row[self.FIRSTNAME_INDEX], grade=to_grade_number(row[self.GRADE_INDEX]))
players = cur.execute('''SELECT {} FROM "{}" ORDER BY {} DESC;'''.format( columns_string, league_name, svar)).fetchall() cur.close() conn.close() for player in players: player_data = dict(zip(columns, player)) if player_data[svar] < least or player_data[svar] == 0: continue for row in columns: val = player_data[row] col_text = '{:15} : \t'.format(row.capitalize()) if row == svar: color = 'green' if least > 0 else 'yellow' val_text = utils.colored(val, color) else: val_text = utils.to_ascii(val) print col_text + val_text print '\n' skip_rest = raw_input( 'Enter: continue, Others characters: Skip rest of the league, Ctrl+D: Exit : ' ) if not skip_rest == '': break