Best Python code snippet using fMBT_python
easyrider.py
Source:easyrider.py  
1# Write your awesome code here2import json3import re4#with open('easyrider.json','r') as f1:5#    data = json.load(f1)6data = json.loads(input())7def checktype(target,type_check):8    if type(target) == type_check:9        return(True)10    else:11        return(False)12#regex templates13name_re = r"([A-Z][a-z]+\s)+(Road|Avenue|Boulevard|Street)$"14time_temp = '[01][0-9]:[0-5][0-9]$|2[0-3]:[0-5][0-9]$'15data_check = {'bus_id': {'datatype': int, 'typeErrors': 0, 'emptyErrors': 0, 'formatErrors': 0},16              'stop_id': {'datatype': int, 'typeErrors': 0, 'emptyErrors': 0, 'formatErrors': 0},17              'stop_name': {'datatype': str, 'typeErrors': 0, 'emptyErrors': 0, 'formatErrors': 0},18              'next_stop': {'datatype': int, 'typeErrors': 0, 'emptyErrors': 0, 'formatErrors': 0},19              'stop_type': {'datatype': str, 'typeErrors': 0, 'emptyErrors': 0, 'formatErrors': 0},20              'a_time': {'datatype': str, 'typeErrors': 0, 'emptyErrors': 0, 'formatErrors': 0}21              }22for item in data:23    for item_obj in item:24        if not checktype(item[item_obj],data_check[item_obj]['datatype']):25            data_check[item_obj]['typeErrors'] += 126        else:27            if item[item_obj] == "" and item_obj != "stop_type":28                data_check[item_obj]['emptyErrors'] += 129            elif item_obj == "stop_type":30                if not re.match('[OSF]?$',item[item_obj]):31                    data_check[item_obj]['formatErrors'] += 132            elif item_obj == 'a_time':33                if not re.match(time_temp,item[item_obj]):34                    data_check[item_obj]['formatErrors'] += 135            elif item_obj == 'stop_name':36                if not (re.match(name_re,item[item_obj])):37                    data_check[item_obj]['formatErrors'] += 138total_errors = 039for item_obj in data_check:40    total_errors += data_check[item_obj]['typeErrors'] + data_check[item_obj]['emptyErrors'] + data_check[item_obj]['formatErrors']41# print("Format validation errors: " + str(total_errors) + " errors")42#for item_obj in data_check:43#    if data_check[item_obj]['typeErrors'] + data_check[item_obj]['emptyErrors']+ + data_check[item_obj]['formatErrors'] > 0:44#        print(item_obj + ": " + str(data_check[item_obj]['typeErrors'] + data_check[item_obj]['emptyErrors']+ + data_check[item_obj]['formatErrors']))45# print("stop_name: " + str(data_check['stop_name']['formatErrors']))46# print("stop_type: " + str(data_check['stop_type']['formatErrors']))47# print("a_time: " + str(data_check['a_time']['formatErrors']))48busses = []49busses_and_stops = {}50for item in data:51    if item['bus_id'] not in busses:52        busses.append(item['bus_id'])53        busses_and_stops[item['bus_id']] = 154    else:55        busses_and_stops[item['bus_id']] += 156#for bus in busses_and_stops:57 #   print('bus_id: ' + str(bus) + ', stops: ' + str(busses_and_stops[bus]))58lines = set()59passed = True60for item in data:61    lines.add(item['bus_id'])62start_stops = set()63transfer_stops = set()64finish_stops = set()65all_stops = set()66for line in lines:67    start= False68    finish = False69    for item in data:70        if(item['bus_id']==line):71            if(item['stop_type']=="S"):72                start=True73                start_stops.add(item['stop_name'])74            elif(item['stop_type']=="F"):75                finish=True76                finish_stops.add(item['stop_name'])77            if(item['stop_name'] in all_stops):78                transfer_stops.add(item['stop_name'])79            else:80                all_stops.add(item['stop_name'])81    if not start or not finish:82        print("There is no start or end stop for the line: " + str(line))83        passed=False84        break85#if passed:86#    print("Start stops: " + str(len(start_stops)) + " " + str(sorted(start_stops)))87#    print("Transfer stops: " + str(len(transfer_stops)) + " " + str(sorted(transfer_stops)))88#    print("Finish stops: " + str(len(finish_stops)) + " " + str(sorted(finish_stops)))89wrong_times = {}90for line in lines:91    this_line_stops = []92    this_line_times = {}93    this_line_stop_names = {}94    this_line_nextstops = {}95    for item in data:96        if item['bus_id'] == line:97            this_line_stops.append(item['stop_id'])98            this_line_times[item['stop_id']] = item['a_time']99            this_line_stop_names[item['stop_id']] = item['stop_name']100            this_line_nextstops[item['stop_id']] = item['next_stop']101            if item['stop_type'] == 'S':102                start_stop = item['stop_id']103    this_stop = start_stop104    next_stop = this_line_nextstops[this_stop]105    for i in range(len(this_line_stops)-1):106        if this_line_times[next_stop] < this_line_times[this_stop]:107            wrong_times[line] = this_line_stop_names[next_stop]108            break109        this_stop = next_stop110        next_stop = this_line_nextstops[this_stop]111# print("Arrival time test:")112# if len(wrong_times) == 0:113#     print("OK")114# else:115#     for wrong_time in wrong_times:116#         print("bus_id line " + str(wrong_time) + " wrong time on station " + wrong_times[wrong_time])117wrong_stops = set()118for item in data:119    if(item['stop_type']=="O"):120        if item['stop_name'] in start_stops:121            wrong_stops.add(item['stop_name'])122        elif item['stop_name'] in finish_stops:123            wrong_stops.add(item['stop_name'])124        elif item['stop_name'] in transfer_stops:125            wrong_stops.add(item['stop_name'])126# def wrong_stop_printer(wrong_stops):127#     start_string = "Wrong stop type: ["128#     for i in range(len(wrong_stops)):129#         if i == 1:130#             start_string += wrong_stops[i]131#         else:132#             start_string += ", " + wrong_stops[i]133#         if i == len(wrong_stops):134#             start_string += "]"135print('On demand stops test:')136if len(wrong_stops) == 0:137    print("OK")138else:...phonetics.py
Source:phonetics.py  
1import phonetics2import editdistance3import fuzzy4from itertools import combinations, product5print(phonetics.dmetaphone('catherine'))6print(phonetics.dmetaphone('kathryn'))7print(phonetics.dmetaphone('335 Deinhard Lane, Mc Call, ID 83638'))8print(phonetics.dmetaphone('5105 Berwyn Road, College Park, MD 20740'))9print(phonetics.dmetaphone('5105 Berwin Road, College Park, MD 20740'))10name1 = '5105 Berwyn Road, College Park, MD 20740'11name2 = '5105 Berwin Road, College Park, MD 20740'12name3 = '335 Deinhard Lane, Mc Call, ID 83638'13nysiis_score = editdistance.eval(fuzzy.nysiis(name1), fuzzy.nysiis(name2))14other_nysiis_score = editdistance.eval(fuzzy.nysiis(name1), fuzzy.nysiis(name3))15print(nysiis_score)16print(other_nysiis_score)17#copied over from polish.py18def loadTestData(inPath = r'testData.txt' ):19    testDataPath = inPath20    with open(testDataPath, 'r') as temp:21        testData = [x[:-1] for x in temp.readlines()]22    return testData23matchStrings = loadTestData()24#mocking up a fake demo rows here...25testStrings = """26101 north vinebridge street, anytown md 9999927101 north vineridge street, anytown md 9999928101 vinebridge street, anytown md 9999929101 north vine bridge street, anytown md 9999930101 north vine Rridge street, anytown md 9999931101 north pine bridge street, anytown md 9999932101 north pinebridge street, anytown md 9999933101 pinebridge street, anytown md 9999934101 pinebridge street south, anytown md 9999935101 pinebridge street north, anytown md 99999365105 Berwin Road, College Park, MD 2074037""".strip().split('\n')38#Given the tuple output by usaddress, return a dictionary39def usAdr2Dict(inTuple):40    outDict = {}41    for item in inTuple:42        outDict[item[1]]=item[0]43    return outDict44#Given a dictionary of formatted errors, report each address45def report_format_errors(formatted_dict):46    formatErrors = formatted_dict['errors'] 47    if len(formatErrors)>0:48        print('the following test records failed and require code fixes:')49    for errorLine in formatErrors:50        print(errorLine)51        errorStan = standardizer.standardize(errorLine)52        for item in errorStan:53            print('\t',item[1],item[0])54        print('\n')55#Given two raw input files, standardize and clean each address56#output a formatted dictionary containing each category and its standardized result57def format_strings(address_strings):58    format_dict = {}59    fidRows = []60    formatErrors = []61    for string in address_strings:62        try:63            standard_dict = usAdr2Dict(standardizer.standardize(string))64            formatted_entry = comparator.fid_prepare(standard_dict)65            fidRows.append(formatted_entry)66        except:67            formatErrors.append(string)68    format_dict['matchable'] = fidRows69    format_dict['errors'] = formatErrors70    report_format_errors(format_dict)71    return format_dict72#Deduplicate all the adddresses within a single cleaned file73def pairwise_match(cleaned_data):74    scores = []75    counter = 076    for fidA, fidB in combinations(cleaned_data, 2):77        fid_list = comparator.fid_pair(fidA, fidB)78        fid_score = -179        counter += 180        if (isinstance(fid_list[0], str) and isinstance(fid_list[1], str)):81            fid_score = fidComparator(*fid_list)82        print(fidA, fidB, fid_score)83    print(counter)84    scores.append(fid_score)85#Identify all matches between two standardized files86def find_matches(setA, setB):87    matchResults = []88    matchTestCounter = 089    print('looping through match tests')90    91    for entryA, entryB in product(setA, setB):92        matchTestCounter += 193        if matchTestCounter % 40 == 0:94            print('\tProgress: {}'.format(str(matchTestCounter)))95        fid_list = comparator.fid_pair(entryA, entryB)96        fid_score = -197        if (isinstance(fid_list[0], str) and isinstance(fid_list[1], str)):98            fid_score = fidComparator(*fid_list)99        matchResults.append(fid_score)100    return matchResults101#return output as dataframe102def find_matches_in_df(setA, setB):103    df = pd.DataFrame(columns = ['Address 1', 'Address 2', 'FidScore'])104    matchTestCounter = 0105    for entryA, entryB in product(setA, setB):106        fid_list = comparator.fid_pair(entryA, entryB)107        fid_score = -1108        if (isinstance(fid_list[0], str) and isinstance(fid_list[1], str)):109            fid_score = fidComparator(*fid_list)110        df.loc[matchTestCounter] = [entryA, entryB, fid_score]111        matchTestCounter += 1112    pd.set_option("display.max_rows", None, "display.max_columns", None)113    print(df)114    return df115def find_matches_output_file(setA, setB, file_path):116    f = open(file_path, "w")117    for entryA, entryB in product(setA, setB):118        fid_list = comparator.fid_pair(entryA, entryB)119        fid_score = -1120        if (isinstance(fid_list[0], str) and isinstance(fid_list[1], str)):121            fid_score = fidComparator(*fid_list)122        # nysiis_score = editdistance.eval(fuzzy.nysiis(entryA), fuzzy.nysiis(entryB))123        string_entryA = ', '.join([str(item) for item in entryA])124        string_entryB = ', '.join([str(item) for item in entryB])125        f.write(string_entryA + " | " + string_entryB + " | " + str(fid_score) + " | " + str(nysiis_score) + "\n")126    f.close()127def find_phonetic_matches(setA, setB, file_path):128    f = open(file_path, "w")129    for entryA, entryB in product(setA, setB):130        nysiis_score = editdistance.eval(fuzzy.nysiis(entryA), fuzzy.nysiis(entryB))131        f.write(entryA + " | " + entryB + " | " + str(nysiis_score) + "\n")132    f.close()133standardized_testing = format_strings(testStrings)134standardized_match = format_strings(matchStrings)135pairwise_match(standardized_match['matchable'])136pairwise_match(standardized_testing['matchable'])137find_matches_output_file(standardized_match['matchable'], standardized_testing['matchable'], "match.txt")...tango.py
Source:tango.py  
1from flask import Blueprint, render_template, abort, request2import flask3import logging4from .utils import getSuccessResponse5from ...alg.algorithm import recognize6from ..controller.dataset import save7from ..controller import validate8from ..controller import online as session9tango = Blueprint('tango', __name__,10  template_folder='templates',11  url_prefix='/tango')12logging.info("this is from the tango file");13@tango.route('/save', methods=['POST'])14def saveData():15  """ flask route function, post request take in a sample example and will save it """16  data = request.json17  ## validate input18  formatErrors = validate.json_route(data, route=request.path)19  if(formatErrors != None):20    return flask.jsonify(**formatErrors)21  letter = data.get('letter',"unknown")22  ## save the data23  success = save(data, letter)24  print("send a success as a response")25  result = getSuccessResponse()26  if(not success):27    result = flask.jsonify(28      success=False,29      responseCode=500,30      message="internal server error")31  return result32@tango.route('/offline/rec', methods=['POST'])33def rec_offline():34  """ This is a post request, take an example data and will return35      the letter it corresponds with36  """37  data = request.json38  ## validate input39  formatErrors = validate.json_route(data, route=request.path)40  if(formatErrors != None):41    return flask.jsonify(**formatErrors)42  ## run algorithm43  result = recognize(data, offline=True)44  ## return result45  if(result == None):46    return getSuccessResponse()47  return result48@tango.route('/online/connect')49def rec_online_connect():50  sessionID = session.create()51  ## return result52  return flask.jsonify(53    success=True,54    message="",55    responseCode=200,56    sessionID=sessionID57  )58@tango.route('/online/disconnect')59def rec_online_disconnect():60  """ This will take in a session id and close that session"""61  sessionID = request.args.get('sessionID')62  ## validate63  if(sessionID == None): return flask.jsonify(64      success=False,65      responseCode=404,66      message="sessionID not passed in as url parameter"67    ), 40468  ## disconect from user69  result = session.close(sessionID)70  if(result != None): return flask.jsonify(**result), result['responseCode']71  ## send response72  return getSuccessResponse()73@tango.route('/online/rec', methods=['POST'])74def rec_online():75  """ This function will take in the data for a given interval,76      based on storing the last intervals. It will return if the77      last n seconds was a valid letter78  """79  data = request.json80  sessionID = request.args.get('sessionID')81  ## validate input82  formatErrors = validate.json_route(data, route=request.path)83  if(formatErrors != None):84    return flask.jsonify(**formatErrors)85  ## run alg86  result = session.data(sessionID, data)87  print("result: ", result)88  if(result != None): return flask.jsonify(**result[0]), result[1]89  ## return response...Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
