def replace_paper(c, paper, url, alljel): """Update a single paper record.""" blob = json.dumps(paper, ensure_ascii=False).encode(encoding='utf-8') paper = collect(paper) r = {} r['url'] = url r['handle'] = paper['handle'][0] r['template'] = parse_template(paper['template-type'][0]) for f in ['title', 'abstract', 'journal', 'volume', 'issue', 'pages']: r[f] = paper.get(f, [None])[0] for f in ['title', 'abstract', 'journal']: r[f] = sanitize(r[f]) r['language'] = paper.get('language', ['none'])[0].lower() r['language'] = r['language'] if len(r['language']) == 2 else None r['language'] = lang_and(r['title'], r['abstract'], default=r['language']) r['year'] = get_year(paper) r['redif'] = zlib.compress(blob, level=9) sql = 'REPLACE INTO papers (' + ', '.join(k for k in r.keys()) + ')' sql += ' VALUES (' + ', '.join(['?'] * len(r)) + ')' c.execute(sql, list(r.values())) pid = c.lastrowid if 'author-name' in paper: authors = [sanitize(n) for n in paper['author-name']] authors = [(pid, n) for n in authors if n] c.executemany('INSERT INTO authors (pid, name) VALUES (?, ?)', authors) if 'classification-jel' in paper: jel = parsejel(paper['classification-jel'][0], alljel) jel = [(pid, c) for c in jel] c.executemany('INSERT INTO papers_jel (pid, code) VALUES (?, ?)', jel)
def check(): connection = pymysql.connect(host='tsuts.tskoli.is', port=3306, user='******', passwd='mypassword', db='1604002850_vef2verk10') username = sanitize(request.forms.get("username")) password = str(sanitize(request.forms.get("password"))) with connection.cursor() as cursor: sql = "SELECT pass FROM user WHERE user = '******'" cursor.execute(sql) result = cursor.fetchone() if result: if str(result[0]) == str(password): response.set_cookie("account", username, secret=password) return template("leynisida.tpl", username=username) else: uttak = "Rangt lykilorð" else: uttak = "Notandinn er ekki til" connection.close() if uttak == "Rangt lykilorð": return template("indexAfterSignup.tpl", uttak=uttak) elif uttak == "Notandinn er ekki til": return template("indexAfterSignup.tpl", uttak=uttak)
def populate_query_model(self, first_artist, similar_artists): """Iterate through library and populate our playlist with similar artists""" first_artist_sanitized = sanitize(first_artist) self.qm = RB.RhythmDBQueryModel.new_empty(self.db) lst = [] for row in self.object.props.library_source.props.base_query_model: entry = row[0] artist = unicode(entry.get_string(RB.RhythmDBPropType.ARTIST), 'utf-8') if sanitize(artist) == first_artist_sanitized: if self.echonest_source.unique_artist.get_active() == True: # skip this artist pass else: lst.append(entry) elif sanitize(artist) in similar_artists: lst.append(entry) if self.echonest_source.scale_artists.get_active(): lst = self.scale(lst) for entry in lst: self.qm.add_entry(entry, -1) self.echonest_source.props.query_model = self.qm self.echonest_source.get_entry_view().set_model(self.qm)
def POST(self): user = web.input(username=None) password = web.input(password=None) resume = web.input(resume=None) # Data sanitizing c_user = sanitize(user.username) c_password = hashlib.md5(sanitize(password.password)).hexdigest() if not resume.resume: # New user if user.username and password.password: # Does the user already exist? if db_controllers.user_exists(c_user, c_password) is False: return render.intro(self.display, self.message_user_already_exists) else: # User creation db_controllers.create_user(c_user, c_password) session.logged = True session.user = c_user # We redirect the user web.seeother("/rooms") else: # Does the user exist? if db_controllers.user_exists(c_user, c_password) is False: session.logged = True session.user = c_user web.seeother("/rooms") else: return render.intro(self.display, self.message_user_not_found)
def nyskraning(): connection = pymysql.connect(host='tsuts.tskoli.is', port=3306, user='******', passwd='mypassword', db='1511003090_urbotvef', charset='utf8') global username username = sanitize(request.forms.get("username")) password = str(sanitize(request.forms.get("password"))) response.set_cookie('username', username) with connection.cursor() as cursor: sql = "SELECT pass FROM user WHERE user = '******'" cursor.execute(sql) result = cursor.fetchone() if result: print(result[0]) print(password) if str(result[0]) == str(password): response.set_cookie("account", username, secret=password) return template("redirect.tpl") else: uttak = "Rangt lykilorð" else: uttak = "Notandinn er ekki til" connection.close() if uttak == "Rangt lykilorð": return template("indexAfterSignup.tpl", uttak=uttak) elif uttak == "Notandinn er ekki til": return template("indexAfterSignup.tpl", uttak=uttak)
def new_work(): #{ if request.method == 'POST': try: new_work_data = \ { 'title': request.form['title'], 'fandom_tags': re.split('\s*,\s*', request.form['fandom_tags']), 'content_tags': re.split('\s*,\s*', request.form['content_tags']), 'char_tags': re.split('\s*,\s*', request.form['char_tags']), 'ship_tags': re.split('\s*,\s*', request.form['ship_tags']), 'summary': sanitize(request.form['summary']) } new_work = models.Work(**new_work_data) new_work.author_id = current_user.id db.session.add(new_work) db.session.commit() flash('New work created.', 'success') return redirect('/work/'+str(new_work.id)+'/add') except NameError as err: flash('There were missing fields in the data you submitted.', 'error') print(err) return redirect(url_for('new_work')) elif request.method == 'GET': return render_template('work/new.html')
def signup(): #{ if request.method == 'POST': # Form data submitted try: assert len(request.form['password']) >= 8 new_user_data = \ { 'name': request.form['name'], 'email': request.form['email'], 'password': bcrypt.generate_password_hash(request.form['password']), 'about': sanitize(request.form.get('about', "")) } new_user = models.User(**new_user_data) db.session.add(new_user) db.session.commit() flash('Your account has been created and you can now log in.', 'success') return redirect(url_for('login')) except NameError: flash('There were missing fields in the data you submitted.', 'error') return redirect(url_for('signup')) except AssertionError: flash('Password not long enough.', 'error') return redirect(url_for('signup')) # except IntegrityError: # flash('That e-mail address or display name is already in use.'); # not sure it was IntegrityError when uniqueness violated... elif request.method == 'GET': # Show sign up form if not current_user.is_authenticated: # but not if they're already logged in return render_template('user/new.html') else: return redirect(url_for('user_dash'))
def get_url_words(): ''' this should be refactored to give an array of word, number objects ''' errors = [] results = {} print "Angular communicated with backend using url service" # get url # turning request json into pyobject data = json.loads(request.data.decode()) print "Data: {}".format(data) url = data['url'] # getting url from python data dictionary print "Loaded json into data" if sanitize(url): r = requests.get(url) else: url = 'http://' + url; r = requests.get(url) print "Urls were properly sanitized" # Run process # errors incorporate errors at some point errors, results = process_text(r, errors, results) #print results results1 = [{'word':item[0], 'frequency': item[1]}for item in results] # This is the correct structure need for d3! #print results1 #json_object1 = json.dumps(dict(results)) json_object2 = json.dumps(results1) #print "Old json_object1\n{}".format(json_object1) #print "New json_object2\n {}".format(json_object2) return json_object2
def getTopTwitchStreams(credentials) -> List[Stream]: headers = getAuthorizedHeader(credentials.oauth) params = {"first": 99} topTwitchStreams = [] response = requests.get(URLConstants.TWITCH_LIVE_FOLLOWED, headers=headers, params=params) jsonStreams = json.loads(response.text) if jsonStreams['data'] is not None: gameIDs = [] for stream in jsonStreams['data']: gameIDs.append(stream['game_id']) gameIDs = list(set(gameIDs)) gameInformation = getGameInformation(credentials.oauth, gameIDs) for stream in jsonStreams['data']: try: game = [ game for game in gameInformation['data'] if game['id'] == stream['game_id'] ][0] gameTitle = game['name'] boxArtURL = game['box_art_url'].replace('{width}', '52').replace( '{height}', '72') except IndexError: gameTitle = "N/A" boxArtURL = "" gameTitle = sanitize(gameTitle) topTwitchStreams.append(Stream(stream, gameTitle, boxArtURL)) return topTwitchStreams
def index(): if request.forms.get("fastanr"): leitarnr = sanitize(request.forms.get("fastanr")).upper() connection = pymysql.connect(host='tsuts.tskoli.is', port=3306, user='******', passwd='mypassword', db='1604002850_vef2verk11') with connection.cursor() as cursor: sql = "SELECT skraningarnumer, Tegund, verksmidjunumer, skraningardagur, co2, þyngd, skodun, stada FROM bilar WHERE skraningarnumer = '" + leitarnr + "'" cursor.execute(sql) result = cursor.fetchone() if result: fastanr = result[0] tegund = result[1] verksmidjunr = result[2] skraningardagur = result[3] co2 = result[4] thyngd = result[5] skodun = result[6] stada = result[7] if str(result[0]) == str(leitarnr): return template("info.tpl", a=fastanr, b=tegund, c=verksmidjunr, d=skraningardagur, e=co2, f=thyngd, g=skodun, h=stada) else: return template("popup.tpl", text="Bíll ekki til") connection.close() return template("index.tpl")
def main(args): try: os.mkdir(args.output_folder) except FileExistsError: print("Output folder already exists") document = MailMerge(args.template) data = csv.DictReader(open(args.data)) for row in data: first_name, last_name = row['First Name'], row['Last Name'] document.merge(Nome=first_name, Cognome=last_name) docx_destination_name = os.path.join( args.output_folder, "%s-%s-%s.docx" % (args.base_name, sanitize(first_name), sanitize(last_name))) document.write(docx_destination_name) to_pdf(docx_destination_name)
def POST(self): if session.user is None: # Username init user = web.input(username=None) session.user = sanitize(user.username) web.seeother("/game") code = web.input(armory_code=None) process = web.input(action=None) # We check the parameters if process.action: if self.lexicon.parse_lexicon(process.action, session.room.name, test=False) is not False: session.room = session.room.go(self.lexicon.parse_lexicon(process.action, session.room.name, test=False)) web.seeother("/game") else: return render.you_died() # View-specific code elif code.armory_code: if session.attempts != 1: if self.lexicon.parse_lexicon(code.armory_code, session.room.name, test=False) is False: session.attempts -= 1 else: session.room = session.room.go(code.armory_code) web.seeother("/game") else: return render.you_died() else: return self.view
def spell_check_post(): text = request.form['inputtext'] text = sanitize(text) x = [random.randint(0, 9) for y in range(0, 10)] temp = ''.join(map(str, x)) f = open("./" + temp + ".txt", "w+") f.write(text) f.close() #check words cmd = "./a.out " + f.name + " ./wordlist.txt" #run file through spell checker c program checkedtext = subprocess.check_output(cmd, shell=True) #decode to string from bytes checkedtext = checkedtext.decode('ascii') checkedtext = checkedtext.replace("\n", ",") #delete file to prevent resource depletion attacks os.remove(f.name) current_time = datetime.now().strftime('%Y-%m-%d %H:%M:%S') logquery = History(submit_text=text, returned_text=checkedtext, submit_user=current_user.email, timestamp=current_time) db.session.add(logquery) db.session.commit() return render_template('spellcheckpost.html', inputtext=text, outtext=checkedtext)
def averagelen(text): words = sanitize(text).split() letters = 0 for word in words: letters += len(word) return letters / len(words)
def getLiveFollowedStreams(oAuth: str, streams: List[List[dict]]) -> List[Stream]: headers = getAuthorizedHeader(oAuth) liveStreams = [] for streamBatch in streams: params = {"user_id": [stream['to_id'] for stream in streamBatch]} response = requests.get(URLConstants.TWITCH_LIVE_FOLLOWED, headers=headers, params=params) jsonStreams = json.loads(response.text) if jsonStreams['data'] is not None: gameIDs = [] for stream in jsonStreams['data']: gameIDs.append(stream['game_id']) gameIDs = list(set(gameIDs)) gameInformation = getGameInformation(oAuth, gameIDs) for stream in jsonStreams['data']: try: game = [ game for game in gameInformation['data'] if game['id'] == stream['game_id'] ][0] gameTitle = game['name'] boxArtURL = game['box_art_url'].replace( '{width}', '52').replace('{height}', '72') except IndexError: gameTitle = "N/A" boxArtURL = "" gameTitle = sanitize(gameTitle) liveStreams.append(Stream(stream, gameTitle, boxArtURL)) liveStreams.sort(key=lambda x: int(x.viewerCount), reverse=True) return liveStreams
def render(self, path, base="html"): """Renders the given template or the default template, or JSON(P)/YAML.""" if self.is_json(): sanitized = sanitize(self.response_dict(), self.urlize) json_str = json.write(sanitized) callback = self.request.get('callback') if re.match("^[_a-z]([_a-z0-9])*$", callback, re.IGNORECASE): json_str = "%s(%s)" % (callback, json_str) # jsonp self.response.headers['Content-Type'] = "%s; charset=UTF-8" % MIME_JSON self.response.out.write(json_str) return if self.is_yaml(): sanitized = sanitize(self.response_dict(), self.urlize) yaml_str = yaml.safe_dump(sanitized, default_flow_style=False) self.response.headers['Content-Type'] = "text/plain; charset=UTF-8" self.response.out.write(yaml_str) return if self.is_xml(): sanitized = sanitize(self.response_dict(), self.urlize) xml_str = to_xml(value=sanitized, root="response") self.response.headers['Content-Type'] = "%s; charset=UTF-8" % MIME_XML self.response.out.write(xml_str) return if not path: path = self.default_template(ext=base) full_path = os.path.join(TEMPLATES_BASE, path) if self.file_exists(full_path): try: # the template might find these handy self.response_dict( handler=self, is_dev=env.is_dev() ) template = get_jinja2_env().get_template(path) rendered = template.render(**self.response_dict()) if self.is_atom() and self.get_status() == 200: # for atom self.response.headers['Content-Type'] = "%s; charset=UTF-8" % MIME_ATOM self.response.out.write(rendered) except jinja2.TemplateError, error: self.response.headers['Content-Type'] = 'text/plain' message = "Template syntax error: %s" % error logging.critical(message) (error_type, error, tb) = sys.exc_info() tb_formatted = traceback.format_tb(tb) self.response.out.write("\n".join([message]+tb_formatted))
def index(): connection = pymysql.connect(host='tsuts.tskoli.is', port=3306, user='******', passwd='mypassword', db='1511003090_urbotvef', charset='utf8') global name username = sanitize(request.forms.get("username")) password = sanitize(request.forms.get("password")) passconf = sanitize(request.forms.get("passconf")) name = sanitize(request.forms.get("name")) address = sanitize(request.forms.get("address")) phone = sanitize(request.forms.get("phone")) email = sanitize(request.forms.get("email")) response.set_cookie('username', username) with connection.cursor() as cursor: sql = "SELECT user, pass FROM user WHERE user = '******'" cursor.execute(sql) result = cursor.fetchone() if result: uttak = "Notandi er nú þegar til" else: sql = "INSERT INTO user (user, pass, name, addr, phon, emai) VALUES ('" + username + "', '" + password + "', '" + name + "', '" + address + "', '" + phone + "', '" + email + "')" cursor.execute(sql) connection.commit() uttak = "Notandi hefur verið stofnaður!" connection.close() return template("indexAfterSignup.tpl", uttak=uttak)
def get_coach_data(filename): with open(filename) as data: alist = data.readline() alist = alist.strip().split(',') alist = [sanitize(item) for item in alist] # 列表推导 + 格式标准化 # aset = set(alist) # 集合去重 # alist = sorted(list(aset)) # [集合 -> 列表] + 排序 # return alist[0:3] # 列表分片,取前三 return sorted(list(set(alist)))[0:3] # 函数串链
def get_coach_data(filename): with open(filename) as data: alist = data.readline() alist = alist.strip().split(',') adict = {} adict['Name'] = alist.pop(0) adict['DOB'] = alist.pop(0) adict['Times'] = [sanitize(item) for item in alist] adict['BestTime'] = str(sorted(list(set(adict['Times'])))[0:3]) return adict
def get_coach_data(filename): with open(filename) as data: alist = data.readline() alist = alist.strip().split(',') (name, dob) = alist.pop(0), alist.pop(0) # 弹出列表开头的元素(额外数据:名字和生日) alist = [sanitize(item) for item in alist] score = str(sorted(list(set(alist)))[0:3]) print name, "(", dob, ")' fastest times are: ", score
def get_coach_data(filename): try: with open(filename) as f: data = f.readline() templ = data.strip().split(',') return({'Name' : templ.pop(0), 'DOB' : templ.pop(0), 'Times' : str(sorted(set([sanitize.sanitize(t) for t in templ]))[0:3])}) except IOError as ioerr: print('File error: '+str(ioerr)) return(None)
def get_coach_data(filename): with open(filename) as data: alist = data.readline() alist = alist.strip().split(',') adict = {} adict['Name'] = alist.pop(0) adict['DOB'] = alist.pop(0) adict['Times'] = [sanitize(item) for item in alist] return Athlete(adict['Name'], adict['DOB'], adict['Times'] ) # end: get_coach_data
def signup_post(): email = request.form.get('uname') name = request.form.get('name') password = request.form.get('pword') phone = request.form.get('2fa') #sanitize input. If someone does something sketch, they won't get to log in with their gargbage inputs email = sanitize(email) if name: name = sanitize(name) if not phone.isdigit(): flash('Failure - Phone for 2FA is not a number!', 'is-danger') return redirect(url_for('auth.signup_post')) user = User.query.filter_by(email=email).first( ) # if this returns a user, then the email already exists in database if user: # if a user is found, we want to redirect back to signup page so user can try again flash('Failure - Email address already exists', 'is-danger') return redirect(url_for('auth.signup_post')) # create new user with the form data. Hash the password so plaintext version isn't saved. new_user = User(email=email, name=name, password=generate_password_hash(password, method='sha256'), phone=phone) # add the new user to the database failed = False try: db.session.add(new_user) db.session.commit() except Exception as e: #log your exception in the way you want -> log to file, log as error with default logging, send by email. It's upon you db.session.rollback() db.session.flush() # for resetting non-commited .add() failed = True if not failed: flash('Success', 'is-success') return redirect(url_for('auth.signup_post'))
def read(SQL): try: db = get_db() cursor = db.cursor(buffered=True, dictionary=True) cursor.execute(SQL) db.commit() result = cursor.fetchall() return sanitize(result, (lambda val: float(val) if type(val) == Decimal else val)) except mysql.connector.Error as e: print("Failed to execute query: " + str(e)) finally: cursor.close()
def get_file(file_loc): """Reads the map file and returns raw map data""" if not file_loc.endswith('.mod1'): print("Please choose a .mod1 file") return None try: with open(file_loc, "r") as mod1_file: data = mod1_file.readlines() except: print("Please chose a valid .mod1 file") return None return sanitize.sanitize(data)
def get_coach_data(filename): try: with open(filename) as f: data = f.readline() templ = data.strip().split(',') return ({ 'Name': templ.pop(0), 'DOB': templ.pop(0), 'Times': sorted(set([sanitize(t) for t in templ])) }) except IOError as ierr: print('file error:' + str(ioerr)) return None
def get_coach_data(filename): try: with open(filename) as f: data = f.readline() data = data.strip().split(',') dic = { 'Name': data.pop(0), 'Birthdate': data.pop(0), 'Best Times': str(sorted(set([sanitize(t) for t in data]))[0:3]) } return dic except IOError as ioerr: print('File Error! ' + str(ioerr))
def toast(previouslyLiveStreams, refreshedLiveStreams): if platform.release() == "10": previouslyLiveStreamNames = [ stream.stylizedStreamName for stream in previouslyLiveStreams ] streamsToToast = [ stream for stream in refreshedLiveStreams if stream.stylizedStreamName not in previouslyLiveStreamNames ] for stream in streamsToToast: toaster = ToastNotifier() toaster.show_toast(MessageConstants.TOAST.format( stream.stylizedStreamName), sanitize(stream.streamTitle), threaded=False, icon_path='streamopenericon.ico', duration=8)
def commonsort(intext): countdict = {} text = sanitize(intext).split() #count words in text for word in text: if word in countdict.keys(): countdict[word] += 1 else: countdict[word] = 1 #turn dictionary into list countlist = [] for key, value in countdict.items(): countlist.append({"word": key, "number": value}) #sort list countlist = quicksort(countlist) return countlist
def main(): ''' Generate wordcloud for usernames ''' df = pd.read_csv("keys.log") # Space separated string for wordcloud words = ' ' # Loop though and get usernames only for val in df.Username: tmp = sanitize.sanitize(val) words = words + tmp + ' ' wordcloud = WordCloud().generate(words) # Plot wordcloud plt.figure(figsize=(8, 8), facecolor=None) plt.imshow(wordcloud) plt.axis("off") plt.tight_layout(pad=0) plt.show()
def add_chapter(work_id): #{ if request.method == 'POST': try: work = models.Work.query.options(joinedload('chapters')).get_or_404(work_id) new_chapter_data = \ { 'title': request.form['title'], 'numeral': request.form['numeral'], 'body': sanitize(request.form['body']) } new_chapter = models.Chapter(**new_chapter_data) new_chapter.work_id = work.id new_chapter.position = len(work.chapters) db.session.add(new_chapter) db.session.commit() #### DEBUG FOO #### try: chapter_from_db = models.Chapter.query.get(new_chapter.id) print('ΨΨΨΨΨΨΨΨΨΨΨΨ ADD CHAPTER // chapter_from_db success :', chapter_from_db) except: print('ΨΨΨΨΨΨΨΨΨΨΨΨ ADD CHAPTER // chapter_from_db fail with error :', sys.exc_info()[0]) raise #### END DEBUG FOO #### flash('Chapter added.', 'success') return redirect('/work/'+work_id+'/'+str(new_chapter.position+1)) except NameError as e: flash('There were missing fields in the data you submitted.', 'error') print('!!!!!!!!!!!!!!!!!', e) return redirect('/work/'+work_id+'/add') elif request.method == 'GET': work = models.Work.query.options(joinedload('chapters')).get_or_404(work_id) default_numeral = "Chapter " + str(len(work.chapters) + 1) tumblr = current_user.tumblr_key and current_user.tumblr_secret # true or false whether to offer Tumblr import return render_template('work/add.html', work=work, default_numeral=default_numeral, tumblr=tumblr)
def download(self, url, name): """Download mp3 """ filename = sanitize.sanitize(name) request = self.client.get(url, stream=True) if platform.system() == "Windows": filepath = os.curdir + "/downloads/" + filename elif platform.system() == "Darwin": filepath = "/Users/" + os.getenv('USER') + "/Music/" + filename else: filepath = os.curdir + "/downloads/" + filename with open(filepath, "wb") as code: for chunk in request.iter_content(1024): if not chunk: break code.write(chunk)
def __init__(self, stream, gameTitle, boxArtURL): self.ID = stream['id'] self.viewerCount = sanitize(str(stream['viewer_count'])) self.gameID = stream['game_id'] self.type = stream['type'] self.userID = stream['user_id'] self.language = stream['language'] self.thumbnailURL = sanitize(stream['thumbnail_url']) self.stylizedStreamName = sanitize(stream['user_name']) self.streamTitle = sanitize(stream['title']) self.gameTitle = sanitize(gameTitle) self.tagIDs = stream['tag_ids'] self.startedAt = stream['started_at'] self.previewImage = sanitize(stream['thumbnail_url'].replace('{width}', '320').replace('{height}', '180')) self.streamName = sanitize(stream['thumbnail_url'][52:].split('-')[0]) self.boxArtURL = boxArtURL self.loadedBoxArtImage = None self.loadedPreviewImage = None
def scale(self, lst): """Return a new lst which removes entries for each artist such that every artist has a similar number of tracks""" if not lst: return lst m = {} for entry in lst: artist = unicode(entry.get_string(RB.RhythmDBPropType.ARTIST), 'utf-8') sanitized_artist = sanitize(artist) if sanitized_artist not in m: m[sanitized_artist] = [] m[sanitized_artist].append(entry) # calculate average number of songs per artist, with a minimum of 1 avg_songs = max(1, int(float(len(lst)) / len(m.keys()))) ret = [] for sanitized_artist, sub_lst in m.iteritems(): for i in xrange(avg_songs): index = int(random.random()*len(sub_lst)) ret.append(sub_lst[index]) return ret
def get_similar_artists(self, first_artist, apikey, min_familiarity, max_familiarity): """Obtain similar artists, either from cache or from internet. Then call populate_artists with results""" url = "http://developer.echonest.com/api/v4/artist/similar?api_key={0}&name={1}&format=json&results=100&start=0&min_familiarity={2}&max_familiarity={3}" formatted_url = url.format(urllib.quote(apikey), urllib.quote(first_artist.encode("utf8")), min_familiarity, max_familiarity) if formatted_url not in self.similar_artists_map: raw_data = yield client.getPage(formatted_url) similar_artists_json = json.loads(raw_data) response = similar_artists_json["response"] if "artists" in response: similar_artists = [each["name"].encode("utf8") for each in response["artists"]] else: print "Warning: artist '%s' not found" % first_artist similar_artists = [] m = {} for each in similar_artists: m[sanitize(each)] = each self.similar_artists_map[formatted_url] = m returnValue((formatted_url, self.similar_artists_map[formatted_url]))
def login_post(): email = request.form.get('uname') password = request.form.get('pword') mfa = request.form.get('2fa') remember = True if request.form.get('remember') else False if not mfa.isdigit(): flash('Failure - Phone for 2FA is not a number!', 'is-warning') return redirect(url_for('auth.login_post')) #sanitize user input trust no man email = sanitize(email) user = User.query.filter_by(email=email).first() # check if user actually exists # take the user supplied password, hash it, and compare it to the hashed password in database if not user or not check_password_hash(user.password, password): flash( 'Failure - Incorrect username or password. Please check your login details and try again.', 'is-danger') return redirect( url_for('auth.login_post') ) # if user doesn't exist or password is wrong, reload the page if not user.phone == mfa: flash('Failure - Please verify your multi factor authentication', 'is-danger') return redirect(url_for('auth.login_post')) # if the above check passes, then we know the user has the right credentials flash('success', 'is-success') login_user(user, remember=remember) current_time = datetime.now().strftime('%Y-%m-%d %H:%M:%S') loginquery = Logins(login_time=current_time, user_id=email) db.session.add(loginquery) db.session.commit() return redirect(url_for('auth.login_post'))
def scale(self, lst): """Return a new lst which removes entries for each artist such that every artist has a similar number of tracks""" if not lst: return lst m = {} for entry in lst: artist = unicode(entry.get_string(RB.RhythmDBPropType.ARTIST), 'utf-8') sanitized_artist = sanitize(artist) if sanitized_artist not in m: m[sanitized_artist] = [] m[sanitized_artist].append(entry) # calculate average number of songs per artist, with a minimum of 1 avg_songs = max(1, int(float(len(lst)) / len(m.keys()))) ret = [] for sanitized_artist, sub_lst in m.iteritems(): for i in xrange(avg_songs): index = int(random.random() * len(sub_lst)) ret.append(sub_lst[index]) return ret
sJames = sorted(james, reverse=True) # 降序排序 sJulie = sorted(julie, reverse=True) sMikey = sorted(mikey, reverse=True) sSarah = sorted(sarah, reverse=True) print sJames print sJulie print sMikey print sSarah print "==============================" from sanitize import sanitize newJames = [] for time_string in sJames: newJames.append(sanitize(time_string)) newJulie = [] for time_string in sJulie: newJulie.append(sanitize(time_string)) newMikey = [] for time_string in sMikey: newMikey.append(sanitize(time_string)) newSarah = [] for time_string in sSarah: newSarah.append(sanitize(time_string)) # 原地排序 newJames.sort()
import sanitize def get_coach_data(filename): try: with open(filename) as f: data = f.readline() return(data.strip().split(',')) except IOError as ioerr: print("File error: "+str(ioerr)) return(None) james = get_coach_data('./data/james.txt') julie = get_coach_data('./data/julie.txt') mikey = get_coach_data('./data/mikey.txt') sarah = get_coach_data('./data/sarah.txt') print(sorted(set([sanitize.sanitize(t) for t in james]))[0:3]) print(sorted(set([sanitize.sanitize(t) for t in julie]))[0:3]) print(sorted(set([sanitize.sanitize(t) for t in mikey]))[0:3]) print(sorted(set([sanitize.sanitize(t) for t in sarah]))[0:3])
def comment_sanitizer(sender, comment, request, **kwargs): comment.comment = sanitize(comment.comment) comment.save()
import sanitize import getCoachData import os os.chdir('D:/Python/python-git/begginger/2017-03-11/Program') sarah = getCoachData.get_coach_data( 'sarah.txt') # 使用get_coach_data()函数将sarah的数据文件转化为一个列表,然后把它赋值给“sarah”变量 (sarah_name, saran_dob) = sarah.pop(0), sarah.pop(0) # pop()调用将删除并返回列表最前面的数据项。 print(sarah_name + "'s fastest times are:" + str(sorted(set([sanitize.sanitize(t) for t in sarah]))[0:3])) ''' 以上程序确实能正常工作,但必须指定并创建sarah的三个变量,以便表示哪个名字, 出生日期和计时数据与sarah关联。如果再增加代码来处理james等的数据,则需要多达12个变量 所以我们应该改进代码 ''' # 使用字典关联数据
james = data.strip().split(',') with open('./data/julie.txt') as juf: data = juf.readline() julie = data.strip().split(',') with open('./data/mikey.txt') as mif: data = mif.readline() mikey = data.strip().split(',') with open('./data/sarah.txt') as saf: data = saf.readline() sarah = data.strip().split(',') james = sorted(sanitize.sanitize(each_t) for each_t in james) julie = sorted(sanitize.sanitize(each_t) for each_t in julie) mikey = sorted(sanitize.sanitize(each_t) for each_t in mikey) sarah = sorted(sanitize.sanitize(each_t) for each_t in sarah) unique_james = [] unique_julie = [] unique_mikey = [] unique_sarah = [] for each_t in james: if each_t not in unique_james: unique_james.append(each_t) for each_t in julie: if each_t not in unique_julie: unique_julie.append(each_t)
def top3(self): return sorted(set([sanitize(each_t) for each_t in self]))[0:3]
def top3(self): return(sorted(set([sanitize.sanitize(t) for t in self]))[0:3])
import sanitize def get_coach_data(filename): try: with open(filename) as f: data = f.readline() return(data.strip().split(',')) except IOError as ioerr: print('File error: '+str(ioerr)) return(None) sarah = get_coach_data('./data/sarah2.txt') # (sarah_name, sarah_dob) = sarah.pop(0), sarah.pop(0) # print(sarah_name + str(sorted(set([sanitize.sanitize(t) for t in sarah]))[0:3])) sarah_data = {} sarah_data['Name'] = sarah.pop(0) sarah_data['DOB'] = sarah.pop(0) sarah_data['Times'] = sarah print(sarah_data['Name'] + "'s fastest times are: "+str(sorted(set([sanitize.sanitize(t) for t in sarah_data['Times']]))[0:3]))
import sanitize with open('./data/james.txt') as jaf: data = jaf.readline() james = data.strip().split(',') with open('./data/julie.txt') as juf: data = juf.readline() julie = data.strip().split(',') with open('./data/mikey.txt') as mif: data = mif.readline() mikey = data.strip().split(',') with open('./data/sarah.txt') as saf: data = saf.readline() sarah = data.strip().split(',') clean_james =[sanitize.sanitize(each_t) for each_t in james] clean_julie =[sanitize.sanitize(each_t) for each_t in julie] clean_mikey =[sanitize.sanitize(each_t) for each_t in mikey] clean_sarah =[sanitize.sanitize(each_t) for each_t in sarah] print(sorted(clean_james)) print(sorted(clean_julie)) print(sorted(clean_mikey)) print(sorted(clean_sarah))
def clean_data(self): return(sorted(set([sanitize(t) for t in self])))
def top3(self): b_time=str(sorted(set([sanitize(t) for t in self]))[0:3]) return (b_time)
import sanitize def get_coach_data(filename): try: with open(filename) as f: data = f.readline() return(data.strip().split(',')) except IOError as ioerr: print('File error: '+str(ioerr)) return(None) sarah = get_coach_data('./data/sarah2.txt') (sarah_name, sarah_dob) = sarah.pop(0), sarah.pop(0) print(sarah_name + str(sorted(set([sanitize.sanitize(t) for t in sarah]))[0:3]))