Best Python code snippet using keyboard
execSortingAndTiming.py
Source:execSortingAndTiming.py  
1from heapSort import *2from insertionSort import *3from mergeSort import *4# InsertionSort5# MergeSort6# HeapSort7# Also lots of repeated code, not stoked, but it works.8class ExecSortingAndTiming:9    def __init__(self):10        pass11    @classmethod12    def exec_insertion_sort_perm(cls, length, listType):13        input_file = open('lists/permuted/perm' + length + '.txt').readlines()14        sort = InsertionSort(input_file)15        for x in range(0, len(sort.input_file)):16            sort.input_file[x] = sort.input_file[x].strip('\n')17        start = time.time()18        sort.insertion_sort_algo(sort.input_file)19        end = time.time()20        with open("outfile.txt", "a+") as outfile:21            outfile.write("Type: " + listType + "\n")22            outfile.write("Word List Size: " + length + "\n")23            outTime = (end - start)24            outfile.write(str(outTime) + "\n")25            outfile.write("--------------------" + "\n")26            outfile.close()27        print("\n")28    @classmethod29    def exec_insertion_sort_sorted(cls, length, listType):30        input_file = open('lists/sorted/sorted' + length + '.txt').readlines()31        sort = InsertionSort(input_file)32        for x in range(0, len(sort.input_file)):33            sort.input_file[x] = sort.input_file[x].strip('\n')34        start = time.time()35        sort.insertion_sort_algo(sort.input_file)36        end = time.time()37        with open("outfileInsert.txt", "a+") as outfile:38            outfile.write("Type: " + listType + "\n")39            outfile.write("Word List Size: " + length + "\n")40            outTime = (end - start)41            outfile.write(str(outTime) + "\n")42            outfile.write("--------------------" + "\n")43            outfile.close()44        print("\n")45    @classmethod46    def exec_merge_sort_perm(cls, length, listType):47        input_file = open('lists/permuted/perm' + length + '.txt').readlines()48        sort = MergeSort(input_file)49        for x in range(0, len(sort.input_file)):50            sort.input_file[x] = sort.input_file[x].strip('\n')51        start = time.time()52        sort.merge_sort_algo(sort.input_file)53        end = time.time()54        with open('outfileMerge.txt', 'a+') as outfile:55            outfile.write("Type: " + listType + "\n")56            outfile.write("Word List Size: " + length + "\n")57            outTime = (end - start)58            outfile.write(str(outTime) + "\n")59            outfile.write("--------------------" + "\n")60            outfile.close()61        print("\n")62    @classmethod63    def exec_merge_sort_sorted(cls, length, listType):64        input_file = open('lists/sorted/sorted' + length + '.txt').readlines()65        sort = MergeSort(input_file)66        for x in range(0, len(sort.input_file)):67            sort.input_file[x] = sort.input_file[x].strip('\n')68        start = time.time()69        sort.merge_sort_algo(sort.input_file)70        end = time.time()71        with open('outfileMerge.txt', 'a+') as outfile:72            outfile.write("Type: " + listType + "\n")73            outfile.write("Word List Size: " + length + "\n")74            outTime = (end - start)75            outfile.write(str(outTime) + "\n")76            outfile.write("--------------------" + "\n")77            outfile.close()78        print("\n")79    @classmethod80    def exec_heap_sort_sorted(cls, length, listType):81        input_file = open('lists/sorted/sorted' + length + '.txt').readlines()82        sort = HeapSort(input_file)83        for x in range(0, len(sort.input_file)):84            sort.input_file[x] = sort.input_file[x].strip('\n')85        start = time.time()86        buildHeapTime = sort.heap_sort_algo(sort.input_file)87        end = time.time()88        with open('outfileHeapSort.txt', 'a+') as outfile:89            outfile.write("Type: " + listType + "\n")90            outfile.write("Word List Size: " + length + "\n")91            outTime = (end - start)92            outfile.write(str(outTime) + "\n")93            outfile.write("--------------------" + "\n")94            outfile.close()95        print("\n")96        with open('outfileBuildHeap.txt', 'a+') as outfile:97            outfile.write("Type: " + listType + " BuildHeap" + "\n")98            outfile.write("Word List Size: " + length + "\n")99            outTime = buildHeapTime100            outfile.write(str(outTime) + "\n")101            outfile.write("--------------------" + "\n")102            outfile.close()103            print("\n")104    @classmethod105    def exec_heap_sort_perm(cls, length, listType):106        input_file = open('lists/permuted/perm' + length + '.txt').readlines()107        sort = HeapSort(input_file)108        for x in range(0, len(sort.input_file)):109            sort.input_file[x] = sort.input_file[x].strip('\n')110        start = time.time()111        buildHeapTime = sort.heap_sort_algo(sort.input_file)112        end = time.time()113        with open('outfileHeapSort.txt', 'a+') as outfile:114            outfile.write("Type: " + listType + "\n")115            outfile.write("Word List Size: " + length + "\n")116            outTime = (end - start)117            outfile.write(str(outTime) + "\n")118            outfile.write("--------------------" + "\n")119            outfile.close()120        print("\n")121        with open('outfileBuildHeap.txt', 'a+') as outfile:122            outfile.write("Type: " + listType + " BuildHeap" + "\n")123            outfile.write("Word List Size: " + length + "\n")124            outTime = buildHeapTime125            outfile.write(str(outTime) + "\n")126            outfile.write("--------------------" + "\n")127            outfile.close()...encoder.py
Source:encoder.py  
1import math2import numpy as np3import sys4import util5import golomb6import unary7import elias_gamma8import fibonacci9import delta10import time11import os12def app_help():13    print("Usage:  encoder.py [encode] [input file name] [output file name] [method]")14    print("\tor\n\tencoder.py [decode] [input file name] [output file name]")15    print("Encoding methods supported:\n1â¢Golomb\n2â¢Elias-Gamma\n3â¢Fibonacci\n4â¢Unary\n5â¢Delta")16if len(sys.argv) >= 4:17    function = sys.argv[1]18    input_file = sys.argv[2]19    output_file_name = sys.argv[3]20    if function.lower() == "encode":21        output_file_name += ".cod"22        if len(sys.argv) == 5:23            method = sys.argv[4]24            current_time = " - Execution time: " + str(time.process_time()) + "s"25            if method.lower() == "golomb" or method.lower() == "1":26                util.write_to_file(input_file, output_file_name, 1)27            elif method.lower() == "elias-gamma" or method.lower() == "elias gamma" or method.lower() == "2":28                print("Started encoding", input_file, "into", output_file_name,29                      "using Elias-Gamma method" + current_time)30                util.write_to_file(input_file, output_file_name, 2)31            elif method.lower() == "fibonacci" or method.lower() == "3":32                print("Started encoding", input_file, "into", output_file_name, "using Fibonacci method" + current_time)33                util.write_to_file(input_file, output_file_name, 3)34            elif method.lower() == "unary" or method.lower() == "4":35                print("Started encoding", input_file, "into", output_file_name, "using Unary method" + current_time)36                util.write_to_file(input_file, output_file_name, 4)37            elif method.lower() == "delta" or method.lower() == "5":38                print("Started encoding", input_file, "into", output_file_name, "using Delta method" + current_time)39                util.write_to_file(input_file, output_file_name, 5)40            # ecc41            print("Encoding finished" + " - Execution time: " + str(time.process_time()) + "s")42            print("Size change (compressed file size / input file size): " +43                  "{:.2f}".format(os.stat(output_file_name).st_size / os.stat(input_file).st_size))44            print("Generating noise control file: " + sys.argv[3] + ".ecc")45            util.encode_noise_control(output_file_name, sys.argv[3] + ".ecc")46            print("Noise control file created: " + sys.argv[3] + ".ecc" + " - Execution time: " + str(47                time.process_time()) + "s")48        else:49            app_help()50    elif function.lower() == "decode":51        if input_file.count(".") < 1 or (52                input_file.split('.')[len(input_file.split('.')) - 1] != "ecc" and input_file.split('.')[53            len(input_file.split('.')) - 1] != "cod"):54            print("Please provide a file with the .ecc extension as input")55        else:56            if input_file.split('.')[len(input_file.split('.')) - 1] == "cod":57                has_ecc = False58            else:59                has_ecc = True60            print("Started decoding", input_file, "into",61                  output_file_name + " - Execution time: " + str(time.process_time()) + "s")62            if has_ecc:63                util.decode_noise_control(input_file, input_file.split('.')[0] + ".cod")64                print("Error correction completed" + " - Execution time: " + str(time.process_time()) + "s")65                input_file = input_file.split('.')[0] + ".cod"66            input_file = open(input_file, "rb")67            is_text_file = input_file.read(1)68            if is_text_file == 1:69                is_text_file = True70            else:71                is_text_file = False72            method = input_file.read(1)73            divisor = int.from_bytes(input_file.read(1), "big")74            if len(method) > 0:75                print("Starting decoder - Execution time: " + str(time.process_time()) + "s")76                if int.from_bytes(method, "big") == 1:77                    output_file = open(output_file_name, "wb")78                    print("Detected Golomb method with divisor", divisor)79                    golomb.decode(input_file, output_file, divisor, is_text_file)80                elif int.from_bytes(method, "big") == 2:81                    output_file = open(output_file_name, "wb")82                    print("Detected Elias-Gamma method")83                    elias_gamma.decode(input_file, output_file, is_text_file)84                elif int.from_bytes(method, "big") == 3:85                    output_file = open(output_file_name, "wb")86                    print("Detected Fibonacci method")87                    fibonacci.decode(input_file, output_file, is_text_file)88                elif int.from_bytes(method, "big") == 4:89                    output_file = open(output_file_name, "wb")90                    print("Detected Unary method")91                    unary.decode(input_file, output_file, is_text_file)92                elif int.from_bytes(method, "big") == 5:93                    output_file = open(output_file_name, "wb")94                    print("Detected Delta method")95                    delta.decode(input_file, output_file, is_text_file)96                print("Decoder finished - Execution time: " + str(time.process_time()) + "s")97    else:98        app_help()99else:...makeRsf.py
Source:makeRsf.py  
1# !/usr/bin/python2import sys3if len(sys.argv) < 2:4  print "Usage:",sys.argv[0],"rsf-output-file-name"5  sys.exit(64)6output_file_name=sys.argv[1]7output_file=open(output_file_name, 'w')8input_file=open("modulesWithIDs.txt", 'r')9for line in input_file:10	output_file.write("Module\t" + line)11input_file.close()12input_file=open("moduleBelongsToModule.txt", 'r')13for line in input_file:14	output_file.write("ModuleBelongsToModule\t" + line)15input_file.close()16input_file=open("filesWithIDs.txt", 'r')17for line in input_file:18	output_file.write("File\t" + line)19input_file.close()20input_file=open("fileBelongsToModule.txt", 'r')21for line in input_file:22	output_file.write("FileBelongsToModule\t" + line)23input_file.close()24input_file=open("includeBelongsToFile.txt", 'r')25for line in input_file:26	output_file.write("Include\t" + line)27input_file.close()28input_file=open("conditionalCompilationBlocks.txt", 'r')29for line in input_file:30	output_file.write("ConditionalCompilation\t" + line)31input_file.close()32input_file=open("classesWithIDs.txt", 'r')33for line in input_file:34	output_file.write("Class\t" + line)35input_file.close()36#input_file=open("typedefsWithIDs.txt", 'r')37#for line in input_file:38#	output_file.write("TypeDef\t" + line)39#input_file.close()40input_file=open("classBelongsToFile.txt", 'r')41for line in input_file:42	output_file.write("ClassBelongsToFile\t" + line)43input_file.close()44input_file=open("inheritanceWithIDs.txt", 'r')45for line in input_file:46	output_file.write("InheritsFrom\t" + line)47input_file.close()48input_file=open("methodsWithIDs.txt", 'r')49for line in input_file:50	output_file.write("Method\t" + line)51input_file.close()52input_file=open("methodBelongsToClass.txt", 'r')53for line in input_file:54	output_file.write("MethodBelongsToClass\t" + line)55input_file.close()56input_file = open("methodVisibility.txt", 'r')57for line in input_file:58	output_file.write("Visibility\t" + line)59input_file.close()60input_file = open("methodSignature.txt", 'r')61for line in input_file:62	output_file.write("Signature\t" + line)63input_file.close()64input_file=open("methodHasClassAsReturnType.txt", 'r')65for line in input_file:66	output_file.write("HasType\t" + line)67input_file.close()68input_file=open("attributesWithIDs.txt", 'r')69for line in input_file:70	output_file.write("Attribute\t" + line)71input_file.close()72input_file=open("attributeBelongsToClass.txt", 'r')73for line in input_file:74	output_file.write("AttributeBelongsToClass\t" + line)75input_file.close()76input_file=open("attributeHasClassAsType.txt", 'r')77for line in input_file:78	output_file.write("HasType\t" + line)79input_file.close()80input_file=open("functionsWithIDs.txt", 'r')81for line in input_file:82	output_file.write("Function\t" + line)83input_file.close()84input_file=open("invokableEntityBelongsToFile.txt", 'r')85for line in input_file:86	output_file.write("InvokableEntityBelongsToFile\t" + line)87input_file.close()88input_file=open("functionHasClassAsReturnType.txt", 'r')89for line in input_file:90	output_file.write("HasType\t" + line)91input_file.close()92input_file=open("defsWithAssociation.txt", 'r')93for line in input_file:94	output_file.write("DefinitionForDeclaration\t" + line)95input_file.close()96input_file=open("globalVarsWithIDs.txt", 'r')97for line in input_file:98	output_file.write("GlobalVar\t" + line)99input_file.close()100input_file=open("accessibleEntityBelongsToFile.txt", 'r')101for line in input_file:102	output_file.write("AccessibleEntityBelongsToFile\t" + line)103input_file.close()104input_file=open("globalVarHasClassAsType.txt", 'r')105for line in input_file:106	output_file.write("HasType\t" + line)107input_file.close()108input_file=open("invocationsWithIDs.txt", 'r')109for line in input_file:110	output_file.write("Invokes\t" + line)111input_file.close()112input_file=open("invocationLocations.txt", 'r')113for line in input_file:114	output_file.write("LineNo\t" + line)115input_file.close()116input_file=open("accessesWithIDs.txt", 'r')117for line in input_file:118	output_file.write("Accesses\t" + line)119input_file.close()120input_file=open("accessesLocations.txt", 'r')121for line in input_file:122	output_file.write("LineNo\t" + line)123input_file.close()124input_file=open("leftValueAccesses.txt", 'r')125for line in input_file:126	output_file.write("LeftValueAccess\t" + line)127input_file.close()128input_file=open("entityBelongsToBlock.txt", 'r')129for line in input_file:130	output_file.write("entityBelongsToBlock\t" + line)131input_file.close()132input_file=open("annotations.txt", 'r')133for line in input_file:134	output_file.write("Annotation\t" + line)135input_file.close()136input_file=open("annotationBelongsToEntity.txt", 'r')137for line in input_file:138	output_file.write("AnnotationBelongsToEntity\t" + line)139input_file.close()140input_file=open("metricsWithIDs.txt", 'r')141for line in input_file:142	output_file.write("Measurement\t" + line)143input_file.close()144input_file=open("cfMetricsWithIDs.txt", 'r')145for line in input_file:146	output_file.write("Measurement\t" + line)147input_file.close()...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!!
