Best Python code snippet using responses
lib_acquisition_function.py
Source:lib_acquisition_function.py  
...36                (res.fun < best_acquisition_value):37            res.x = numpy.ndarray.tolist(res.x)38            res.x = lib_data.match_val_type(res.x, x_bounds, x_types)39            if (minimize_constraints_fun is None) or \40                    (minimize_constraints_fun(res.x) is True):41                best_acquisition_value = res.fun42                best_x = res.x43    outputs = None44    if best_x is not None:45        mu, sigma = fun_prediction(best_x, *fun_prediction_args)46        outputs = {'hyperparameter': best_x, 'expected_mu': mu,47                   'expected_sigma': sigma, 'acquisition_func': "ei"}48    return outputs49def _expected_improvement(x, fun_prediction, fun_prediction_args,50                          x_bounds, x_types, samples_y_aggregation,51                          minimize_constraints_fun):52    # This is only for step-wise optimization53    x = lib_data.match_val_type(x, x_bounds, x_types)54    expected_improvement = sys.maxsize55    if (minimize_constraints_fun is None) or (56            minimize_constraints_fun(x) is True):57        mu, sigma = fun_prediction(x, *fun_prediction_args)58        loss_optimum = min(samples_y_aggregation)59        scaling_factor = -160        # In case sigma equals zero61        with numpy.errstate(divide="ignore"):62            Z = scaling_factor * (mu - loss_optimum) / sigma63            expected_improvement = scaling_factor * (mu - loss_optimum) * \64                norm.cdf(Z) + sigma * norm.pdf(Z)65            expected_improvement = 0.0 if sigma == 0.0 else expected_improvement66        # We want expected_improvement to be as large as possible67        # (i.e., as small as possible for minimize(...))68        expected_improvement = -1 * expected_improvement69    return expected_improvement70def next_hyperparameter_lowest_confidence(fun_prediction,71                                          fun_prediction_args,72                                          x_bounds, x_types,73                                          minimize_starting_points,74                                          minimize_constraints_fun=None):75    """76    "Lowest Confidence" acquisition function77    """78    best_x = None79    best_acquisition_value = None80    x_bounds_minmax = [[i[0], i[-1]] for i in x_bounds]81    x_bounds_minmax = numpy.array(x_bounds_minmax)82    for starting_point in numpy.array(minimize_starting_points):83        res = minimize(fun=_lowest_confidence,84                       x0=starting_point.reshape(1, -1),85                       bounds=x_bounds_minmax,86                       method="L-BFGS-B",87                       args=(fun_prediction,88                             fun_prediction_args,89                             x_bounds,90                             x_types,91                             minimize_constraints_fun))92        if (best_acquisition_value) is None or (93                res.fun < best_acquisition_value):94            res.x = numpy.ndarray.tolist(res.x)95            res.x = lib_data.match_val_type(res.x, x_bounds, x_types)96            if (minimize_constraints_fun is None) or (97                    minimize_constraints_fun(res.x) is True):98                best_acquisition_value = res.fun99                best_x = res.x100    outputs = None101    if best_x is not None:102        mu, sigma = fun_prediction(best_x, *fun_prediction_args)103        outputs = {'hyperparameter': best_x, 'expected_mu': mu,104                   'expected_sigma': sigma, 'acquisition_func': "lc"}105    return outputs106def _lowest_confidence(x, fun_prediction, fun_prediction_args,107                       x_bounds, x_types, minimize_constraints_fun):108    # This is only for step-wise optimization109    x = lib_data.match_val_type(x, x_bounds, x_types)110    ci = sys.maxsize111    if (minimize_constraints_fun is None) or (112            minimize_constraints_fun(x) is True):113        mu, sigma = fun_prediction(x, *fun_prediction_args)114        ci = (sigma * 1.96 * 2) / mu115        # We want ci to be as large as possible116        # (i.e., as small as possible for minimize(...),117        # because this would mean lowest confidence118        ci = -1 * ci119    return ci120def next_hyperparameter_lowest_mu(fun_prediction,121                                  fun_prediction_args,122                                  x_bounds, x_types,123                                  minimize_starting_points,124                                  minimize_constraints_fun=None):125    """126    "Lowest Mu" acquisition function127    """128    best_x = None129    best_acquisition_value = None130    x_bounds_minmax = [[i[0], i[-1]] for i in x_bounds]131    x_bounds_minmax = numpy.array(x_bounds_minmax)132    for starting_point in numpy.array(minimize_starting_points):133        res = minimize(fun=_lowest_mu,134                       x0=starting_point.reshape(1, -1),135                       bounds=x_bounds_minmax,136                       method="L-BFGS-B",137                       args=(fun_prediction, fun_prediction_args,138                             x_bounds, x_types, minimize_constraints_fun))139        if (best_acquisition_value is None) or (140                res.fun < best_acquisition_value):141            res.x = numpy.ndarray.tolist(res.x)142            res.x = lib_data.match_val_type(res.x, x_bounds, x_types)143            if (minimize_constraints_fun is None) or (144                    minimize_constraints_fun(res.x) is True):145                best_acquisition_value = res.fun146                best_x = res.x147    outputs = None148    if best_x is not None:149        mu, sigma = fun_prediction(best_x, *fun_prediction_args)150        outputs = {'hyperparameter': best_x, 'expected_mu': mu,151                   'expected_sigma': sigma, 'acquisition_func': "lm"}152    return outputs153def _lowest_mu(x, fun_prediction, fun_prediction_args,154               x_bounds, x_types, minimize_constraints_fun):155    """156    Calculate the lowest mu157    """158    # This is only for step-wise optimization159    x = lib_data.match_val_type(x, x_bounds, x_types)160    mu = sys.maxsize161    if (minimize_constraints_fun is None) or (162            minimize_constraints_fun(x) is True):163        mu, _ = fun_prediction(x, *fun_prediction_args)...checkCreatMultiAndInitRecusion.py
Source:checkCreatMultiAndInitRecusion.py  
...51    def setVarSource(self):52        self.VarSource = "pstru" + self.struNameHld53    def setVarCheck(self):54        self.VarCheck = self.VarSource + "Check"55    def setfun(self):56        self.fun = "void " + getFunName(self.struNameHld) + "(" + self.struName + " *" + self.VarSource + ", " + self.struName + " *" + self.VarCheck + ")\n{\n"57        if self.VarLoopFlag:58            self.fun = self.fun + "    u32    u32i;\n\n"59        for linenum, VarName in enumerate(self.VarNameList):60            VarryObject = re.search(r'([\w|_]+)\[([\w|_]+)\]', VarName)61            if VarryObject:62                VarName = VarryObject.group(1)63                VarSourceName = self.VarSource + "->" + VarName + "[u32i]"64                VarCheckName = self.VarCheck + "->" + VarName + "[u32i]"65                self.fun = self.fun + "\n    for(u32i = 0; u32i < " + VarryObject.group(2) + "; u32i++)\n    {\n"66                if IsStruct(VarName):67                    self.fun = self.fun + "        " + getFunName(self.VarStructNameList[linenum]) + "(&" + VarSourceName + ", &" + VarCheckName + ");\n"68                elif IsStrucPoint(VarName):69                    self.fun = self.fun + "        " + getFunName(self.VarStructNameList[linenum]) + "(" + VarSourceName + ", " + VarCheckName + ");\n"70                else:71                    self.fun = self.fun + "        CHECK_EQUAL_TEXT(" + VarCheckName + ",  " + VarSourceName + ",  " + "\"" + VarSourceName + "\");\n"72                self.fun = self.fun + "    }\n\n"73            else:74                VarSourceName = self.VarSource + "->" + VarName75                VarCheckName = self.VarCheck + "->" + VarName76                if IsStruct(VarName):77                    self.fun = self.fun + "    " + getFunName(self.VarStructNameList[linenum]) + "(&" + VarSourceName + ", &" + VarCheckName + ");\n"78                elif IsStrucPoint(VarName):79                    self.fun = self.fun + "    " + getFunName(self.VarStructNameList[linenum]) + "(" + VarSourceName + ", " + VarCheckName + ");\n"80                else:81                    self.fun = self.fun + "    CHECK_EQUAL_TEXT(" + VarCheckName + ",  " + VarSourceName + ",  " + "\"" + VarSourceName + "\");\n"82        self.fun = self.fun + funEnd83    def getStruInitCode(self, struVarName,structInsList):84        subVarNameList = []85        for instance in structInsList:86            if struVarName == instance.struName:87                subVarNameList = instance.VarNameList88                break89        return subVarNameList90    def setInitFun(self):91        self.initFun = "void " + getInitFunName(self.struNameHld) + "( )\n{\n"92        self.initFun = self.initFun + "    " + self.struName + "  *" + self.VarSource + ";\n"93        for linenum, VarName in enumerate(self.VarNameList):94            VarryObject = re.search(r'([\w|_]+)\[([\w|_]+)\]', VarName)95            if VarryObject:96                VarName = VarryObject.group(1)97                VarSourceName = self.VarSource + "->" + VarName + "[u32i]"98                self.initFun = self.initFun + "\n    for(u32i = 0; u32i < " + VarryObject.group(2) + "; u32i++)\n    {\n"99                if IsStruct(VarName):100                    self.initFun = self.initFun + "        " + VarSourceName + " = ;\n"101                else:102                    self.initFun = self.initFun + "        " + VarSourceName + " = ;\n"103                self.initFun = self.initFun + "    }\n\n"104            else:105                VarSourceName = self.VarSource + "->" + VarName106                if re.search(r'^stru', VarName):107                    self.initFun = self.initFun + "    " + VarSourceName + " = ;\n"108                else:109                    self.initFun = self.initFun + "    " + VarSourceName + " = ;\n"110        self.initFun = self.initFun + funEnd111    def subStruct(self):112        funInitTemp = ""113        codeLine = re.split(r'\n', code)114        for line in codeLine:115            if 116    def checkfunInit(self):117        codeLine = 118    def creatfun(self, code):119        self.setStruName(code)120        self.setVarNameList(code)121        self.setVarSource()122        self.setVarCheck()123        self.setfun()124        self.setInitFun()125fileDef = open('def.txt','r')126fileFun = open('fun.txt','w')127fileFunInit = open('funInit.txt','w')128fileDefStr = fileDef.read()129codeList = splitStructCode(fileDefStr)130for code in codeList:131    structInstance = StructClass()132    structInstance.creatfun(code)133    fileFun.write(structInstance.fun)134    fileFunInit.write(structInstance.initFun)135fileFun.close()136fileDef.close()...fun_linecount.py
Source:fun_linecount.py  
1import os, codecs2import re3import xlwt4from os.path import join, getsize5#filedir = r'D:\ViewRoot\chengengyu_Merge\EMB5216_L2'6filedir = r'D:\ViewRoot\RRM\EMB5216_HL'7#filedir = r'D:\learn\HL DBA彿°è§£æ\now'8class Funline(object):9    def __init__(self):10        self.FunName = False;11        self.FunLineCount = 0;12        self.FunStart = 0;13        self.FunEnd = 0;14        self.FunFileName = False;15    16def findfiles(filedir):17    filelist = []18    filename_file = open('filename.txt','w')19    for root, dirs, files in os.walk(filedir):20        if files:21            for name in files:22                if re.search('.c$', name):23                    filelist.append(join(root, name))               24                    filename_file.write(name+'\n')25    filename_file.close26    return filelist27#è·åæä»¶å表28filelist = findfiles(filedir)29fun_check_result = open('fun_check_result.txt','w')30fun_check_errLine = open('fun_check_errLine.txt','w')31FunLineList = []32for file in filelist:33    filename = ''34    filename = re.findall(r'\\([^\\]+\.c)$', file)35    try:36        fp = codecs.open(file,'rb')37        #fp = open(file,'r')38        #print(file)39        FirstFunFlag = True40        LastFunFlag = False41        FunLineNum = 042        for num,eachline in enumerate(fp):         43            try:44                #å°è¯ç¨ gbkè§£ç 45                eachline = eachline.decode('GBK')46                linetemp = eachline.strip()47                if re.search(r'^(?:void|s32|OSP_STATUS|u32|u8|u16|s8|s16) +\S+(?:$|\s*\(.*(,|\)))', linetemp):48                    if not re.search(r';|extern', linetemp):49                        if FirstFunFlag == False:      #䏿¯ç¬¬ä¸ä¸ªå½æ°åéè¦è®°å½å
¶è¡æ°50                            #print(num, FunlineNum)51                            if fun.FunLineCount != 0:52                                fun_check_result.write("å¼å¸¸å
容fun:" + fun.FunName + eachline + '\n')53                            else:54                                fun.FunLineCount = num - FunLineNum55                                fun.FunStart = FunLineNum56                                fun.FunEnd = num57                        fun = Funline()58                        FunLineList.append(fun)59                        FirstFunFlag = False60                        FunLineNum = num  #è®°å½å½æ°å¼å§çè¡æ°61                        LastFunFlag = True #åå®è¿æ¯æåä¸ä¸ªå½æ°62                        if fun.FunName != False:63                           fun_check_result.write("å¼å¸¸å
容fun:" + eachline + '\n')64                        else:65                            fun.FunName = linetemp66                            fun.FunFileName = filename67            except Exception as e:68                #è§£æå¤±è´¥ï¼é常æ¯å ä¸ºä¸äºæ±åçåè§å
¨è§æé®é¢        69                print(e)70                fun_check_errLine.write(str(e))71                err = "æ æ³è§£æçè¡: æä»¶ä¸º" + file + "è¡å·ä¸º:" + str(num+1) + '\n'72                fun_check_errLine.write(err)73        fp.close74        if LastFunFlag:75            if fun.FunLineCount != 0:76               fun_check_result.write("å¼å¸¸å
容fun:" + fun.FunName + eachline + '\n')77            else:78                fun.FunLineCount = num - FunLineNum79                fun.FunStart = FunLineNum80                fun.FunEnd = num81    except Exception as e:82        print(e)83        fun_check_errLine.write(str(e))84        err = 'æ æ³æå¼æä»¶' + file +'\n'85        fun_check_result.write(err)86fun_check_result.close()87fun_check_errLine.close()88#print(FunLineList)89styles = {'datetime': xlwt.easyxf(num_format_str='yyyy-mm-dd hh:mm:ss'),90          'date': xlwt.easyxf(num_format_str='yyyy-mm-dd'),91          'time': xlwt.easyxf(num_format_str='hh:mm:ss'),92          'header': xlwt.easyxf('font: name Times New Roman, color-index black, bold on', num_format_str='#,##0.00'),93          'default': xlwt.Style.default_style}94wb = xlwt.Workbook(encoding = 'utf-8')95sheet = wb.add_sheet(u'彿°')96row = 097sheet.write(row,0, 'æä»¶å')98sheet.write(row,1, '彿°å')99sheet.write(row,2, 'è¡æ°')100sheet.write(row,3, 'å¼å§è¡æ°')101sheet.write(row,4, 'ç»æè¡æ°')102for num,eachitem in enumerate(FunLineList):     103    row = num + 1104    sheet.write(row,0, eachitem.FunFileName)105    sheet.write(row,1, eachitem.FunName)106    sheet.write(row,2, eachitem.FunLineCount)107    sheet.write(row,3, eachitem.FunStart)108    sheet.write(row,4, eachitem.FunEnd)...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!!
