Best Python code snippet using autotest_python
autograder.py
Source:autograder.py  
1#!/usr/bin/python32import random3import os4import datetime5import subprocess6import struct7import sys8# https://stackoverflow.com/questions/16444726/binary-representation-of-float-in-python-bits-not-hex9def binary(num):10    return ''.join('{:0>8b}'.format(c) for c in struct.pack('!d', num))11def generate_test ( filenum, double=1.0, path="./" ):12    with open("{}tests/test{}.txt".format(path,filenum), "w") as infile:13        infile.write( str(double) )14    with open("{}answers/answer{}.txt".format(path,filenum), "w") as outfile:15        bits = binary(double)16        outfile.write( bits[0] + '_' + bits[1:12] + '_' + bits[12:] )17def generate_test_suite():18    os.makedirs("tests", exist_ok=True)19    os.makedirs("answers", exist_ok=True)20    generate_test ( 0, 0.0 )21    generate_test ( 1, 1.0 )22    generate_test ( 2, -2.0 )23    generate_test ( 3, -0.5 )24    generate_test ( 4, -5./32 )25    generate_test ( 5, 3.625*16 )26def test_doubleToBin ( filenum, path="./", verbose=False ):27    try:28        with open("{}answers/answer{}.txt".format(path,filenum), "r") as outfile:29            answer = outfile.read()30    except EnvironmentError: # parent of IOError, OSError31        print ("answers/answer{}.txt missing".format(filenum))32    try:33        result = subprocess.run(34            ['./doubleToBin', "tests/test{}.txt".format(filenum)],35            cwd=path,36            check=True,37            stdout=subprocess.PIPE,38            stderr=subprocess.STDOUT,39            encoding='ASCII',40            timeout=datetime.timedelta(seconds=4).total_seconds(),41        )42        if verbose:43            print (' '.join(result.args))44            print ("answer")45            print (answer)46            print ("result")47            print (result.stdout)48        assert answer.replace('_','') == result.stdout.replace('_',''), "The printed result doesn't match answers/answer{}.txt. You can add underscores as needed for readability".format(filenum)49        return True50    except subprocess.CalledProcessError as e:51        print (e.stdout)52        print ("Calling ./doubleToBin returned non-zero exit status.")53    except ValueError as e:54        print (result.stdout)55        print ("Please check your output formatting; it should be formatted as a binary number.")56    except AssertionError as e:57        print (result.stdout)58        print (e.args[0])59    return False60def grade_doubleToBin( path='./', verbose=False ):61    score = 062    try:63        subprocess.run( ['make', '-B'], cwd=path, check=True, )64    except subprocess.CalledProcessError as e:65        print ("Couldn't compile doubleToBin.")66        return score67    if test_doubleToBin(0,path,verbose):68        score += 269        if test_doubleToBin(1,path,verbose):70            score += 271            if test_doubleToBin(2,path,verbose):72                score += 273                if test_doubleToBin(3,path,verbose):74                    score += 275                    if test_doubleToBin(4,path,verbose):76                        score += 377                        if test_doubleToBin(5,path,verbose):78                            score += 379                            # standard range test80                            allpass = True81                            for filenum in range(6,10):82                                generate_test (83                                    filenum,84                                    double = random.uniform(85                                        -65536.0,86                                        +65536.0,87                                    ),88                                    path=path89                                )90                                allpass &= test_doubleToBin(filenum,path,verbose)91                            if allpass:92                                score += 393                            # high magnitude test94                            allpass = True95                            for filenum in range(10,14):96                                generate_test (97                                    filenum,98                                    double = random.uniform(99                                        0.,100                                        sys.float_info.max101                                    ),102                                    path=path103                                )104                                allpass &= test_doubleToBin(filenum,path,verbose)105                            if allpass:106                                score += 3107                            # denormalized range test108                            allpass = True109                            for filenum in range(14,18):110                                generate_test (111                                    filenum,112                                    double = random.uniform(113                                        -sys.float_info.min,114                                         sys.float_info.min,115                                    ),116                                    path=path117                                )118                                allpass &= test_doubleToBin(filenum,path,verbose)119                            if allpass:120                                score += 3121                            # negative zero test122                            generate_test ( 18, -0.0, path=path )123                            if test_doubleToBin(16,path,verbose):124                                score += 1125    print ("Score on doubleToBin: {} out of 24.".format(score))126    return score127if __name__ == '__main__':128    # generate_test_suite()129    grade_doubleToBin(verbose=True)...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!!
