Best Python code snippet using localstack_python
analisadorSemantico.py
Source:analisadorSemantico.py
1import os234class analisadorSemantico:5 def __init__(self):6 self.filaSemantica = []7 self.tx = 08 self.armazenarEscritaDoCodigo = ""9 self.armazenarLoop = ""10 self.analisadorDeErro = 01112 def gerarArquivo(self):13 with open("programa.c", 'a') as arquivo:14 arquivo.write(self.armazenarEscritaDoCodigo)1516 def semantico(self, indiceDaProducoes, tabelaDeSimbolos):1718 if indiceDaProducoes == 2:19 cabecalho = "#include<stdio.h> \ntypedef char literal[256]; \nvoid main(void) { \n\t/*----Variaveis temporarias----*/\n"20 for x in range(self.tx):21 cabecalho += "{}int T{};\n".format("\t", x)22 cabecalho += "{}/*------------------------------*/\n".format("\t")23 self.armazenarEscritaDoCodigo = cabecalho + self.armazenarEscritaDoCodigo24 self.armazenarEscritaDoCodigo += "}"25 if(self.analisadorDeErro == 0):26 self.gerarArquivo()2728 elif indiceDaProducoes == 5:29 print("LV -> varfim ;")30 self.armazenarEscritaDoCodigo += ("\n\n\n")31 varfim = self.filaSemantica.pop()32 self.filaSemantica.append({'Classe': "LV", 'Token': varfim["Token"], 'Tipo': varfim["Tipo"], 'Linha': varfim["Linha"],'Coluna': varfim["Linha"]})3334 elif indiceDaProducoes == 6:35 print("D -> TIPO L;")36 i = 037 while i < len(tabelaDeSimbolos):38 if tabelaDeSimbolos[i]['Classe'] == self.filaSemantica[-2]['Classe']:39 tabelaDeSimbolos[i]['Tipo'] = self.filaSemantica[-2]['Tipo']40 i += 141 self.filaSemantica.pop()4243 elif indiceDaProducoes == 7:44 print("id.tipo = TIPO.tipo")45 self.filaSemantica[-3]["Tipo"] = self.filaSemantica[-2]["Tipo"]46 self.armazenarEscritaDoCodigo += ("{}{} {} ;\n".format("\t", self.filaSemantica[-2]["Tipo"], self.filaSemantica[-3]["Classe"]))47 self.filaSemantica.pop(-2)4849 elif indiceDaProducoes == 8:50 print("id.tipo = TIPO.tipo")51 self.filaSemantica[-1]["Tipo"] = self.filaSemantica[-2]["Tipo"]52 self.armazenarEscritaDoCodigo += ("{}{} {} ;\n".format("\t", self.filaSemantica[-2]["Tipo"], self.filaSemantica[-1]["Classe"]))53 self.filaSemantica.pop(-2)5455 elif indiceDaProducoes == 9:56 print("TIPO -> inteiro")57 TIPO = self.filaSemantica.pop()58 self.filaSemantica.append({'Classe': 'TIPO', 'Token': 'TIPO', 'Tipo': TIPO["Tipo"], 'Linha': TIPO["Linha"], 'Coluna': TIPO["Coluna"]})5960 elif indiceDaProducoes == 10:61 print("TIPO -> real")62 TIPO = self.filaSemantica.pop()63 self.filaSemantica.append({'Classe': 'TIPO', 'Token': 'TIPO', 'Tipo': TIPO["Tipo"], 'Linha': TIPO["Linha"], 'Coluna': TIPO["Coluna"]})6465 elif indiceDaProducoes == 11:66 print("TIPO -> literal")67 TIPO = self.filaSemantica.pop()68 self.filaSemantica.append({'Classe': 'TIPO', 'Token': 'TIPO', 'Tipo': TIPO["Tipo"], 'Linha': TIPO["Linha"], 'Coluna': TIPO["Coluna"]})6970 elif indiceDaProducoes == 13:71 print("ES -> leia id;")72 PT_V = self.filaSemantica.pop()73 id = self.filaSemantica.pop()74 self.filaSemantica.pop()75 self.filaSemantica.append({'Classe': "ES", 'Token': "", 'Tipo': "", 'Linha': id["Linha"], 'Coluna': id["Coluna"]})76 if id["Tipo"] != '':77 if id["Tipo"] == "literal":78 self.armazenarEscritaDoCodigo += ('{}scanf("%s", {}'.format("\t", id["Classe"]) + ");\n")79 elif id["Tipo"] == "int":80 self.armazenarEscritaDoCodigo += ('{}scanf("%d", &{}'.format("\t", id["Classe"]) + ");\n")81 elif id["Tipo"] == "real":82 self.armazenarEscritaDoCodigo += ('{}scanf("%lf", &{}'.format("\t", id["Classe"]) + ");\n")83 else:84 print("Erro: Variável não declarada na linha {} e coluna {}".format(PT_V["Linha"], PT_V["Coluna"]))85 self.analisadorDeErro = 18687 elif indiceDaProducoes == 14:88 print("ES -> escreva ARG;")89 self.filaSemantica.pop()90 ARG = self.filaSemantica.pop()91 self.filaSemantica.pop()92 self.filaSemantica.append({'Classe': "ES", 'Token': "", 'Tipo': "", 'Linha': ARG["Linha"], 'Coluna': ARG['Coluna']})93 if ARG["Tipo"] == 'literal':94 self.armazenarEscritaDoCodigo += ('{}printf("%s", {});\n'.format("\t", ARG["Classe"]))95 elif ARG["Tipo"] == 'int':96 self.armazenarEscritaDoCodigo += ('{}printf("%d", {});\n'.format("\t", ARG["Classe"]))97 elif ARG["Tipo"] == 'double':98 self.armazenarEscritaDoCodigo += ('{}printf("%lf", {});\n'.format("\t", ARG["Classe"]))99 else:100 if self.filaSemantica[-5]['Token'] == 'facaAte':101 self.armazenarLoop += '{}printf({});\n'.format("\t", ARG["Classe"])102 else:103 self.armazenarEscritaDoCodigo += ('{}printf({});\n'.format("\t", ARG["Classe"]))104105 elif indiceDaProducoes == 15:106 print("ARG.atributos <- literal.atributos ")107 ARG = self.filaSemantica.pop()108 self.filaSemantica.append(ARG)109110 elif indiceDaProducoes == 16:111 print("ARG.atributos <- num.atributos")112 ARG = self.filaSemantica.pop()113 self.filaSemantica.append(ARG)114115 elif indiceDaProducoes == 17:116 print("ARG.atributos <- id.atributos")117 ARG = self.filaSemantica.pop()118 if ARG["Tipo"] != '':119 self.filaSemantica.append(ARG)120 else:121 print("Erro: Variável não declarada na linha {} e coluna {}".format(ARG['Linha'], ARG['Coluna']))122 self.analisadorDeErro = 1123124 elif indiceDaProducoes == 19:125 print("CMD <- id rcb LD;")126 self.filaSemantica.pop()127 LD = self.filaSemantica.pop()128 rcb = self.filaSemantica.pop()129 id = self.filaSemantica.pop()130 if id["Tipo"] != '':131 if id["Tipo"] == LD["Tipo"]:132 self.armazenarEscritaDoCodigo += ("{}{} {} {};\n".format("\t",id["Classe"], rcb["Tipo"], LD["Classe"]))133 self.filaSemantica.append({'Classe': "CMD", 'Token': "Num", 'Tipo': id['Tipo'], 'Linha': id['Linha'], 'Coluna': id['Coluna']})134 else:135 print("Erro: Tipos diferentes para atribuição na linha {}".format(rcb["Linha"]))136 self.analisadorDeErro = 1137 else:138 print("Erro: Variável não declarada na linha {} e coluna {}".format(id["Linha"], id['Coluna']))139 self.analisadorDeErro = 1140141 elif indiceDaProducoes == 20:142 print("LD <- OPRD opm OPRD;")143 OPRD2 = self.filaSemantica.pop()144 opm = self.filaSemantica.pop()145 OPRD1 = self.filaSemantica.pop()146 if OPRD1["Tipo"] != "literal" and OPRD1["Tipo"] == OPRD2["Tipo"]:147 tx = self.tx148 self.armazenarEscritaDoCodigo += ("{}T{} = {} {} {};\n".format("\t",tx, OPRD1["Classe"], opm["Tipo"], OPRD2["Classe"]))149 self.filaSemantica.append({'Classe': "T{}".format(tx), 'Token': "", 'Tipo': OPRD1["Tipo"], 'Linha': OPRD1["Linha"], 'Coluna': OPRD1["Coluna"]})150 self.tx += 1151 else:152 print("Erro: Operandos com tipos incompatÃveis na linha {} e coluna {}".format(opm['Linha'], opm['Coluna']))153 self.analisadorDeErro = 1154155 elif indiceDaProducoes == 21:156 print("LD.atributos <- OPRD.atributos ")157 LD = self.filaSemantica.pop()158 self.filaSemantica.append(LD)159160 elif indiceDaProducoes == 22:161 print("OPRD.atributos <- id.atributos ")162 OPRD = self.filaSemantica.pop()163 if OPRD["Tipo"] != '':164 self.filaSemantica.append(OPRD)165 else:166 print("Erro: Variável não declarada na linha {} e coluna {}".format(OPRD['Linha'], OPRD['Coluna']))167 self.analisadorDeErro = 1168169 elif indiceDaProducoes == 23:170 print("OPRD.tipo <- num.tipo")171 OPRD = self.filaSemantica.pop()172 self.filaSemantica.append(OPRD)173174 elif indiceDaProducoes == 25:175 print("COND -> CAB CP")176 CAB = self.filaSemantica.pop()177 self.filaSemantica.pop()178 self.armazenarEscritaDoCodigo += ("\t"+"}\n")179 self.filaSemantica.append({'Classe': "COND", 'Token': "", 'Tipo': "", 'Linha': CAB["Linha"], 'Coluna': CAB["Coluna"]})180181 elif indiceDaProducoes == 26:182 print("CAB -> se (EXP_R) então")183 self.filaSemantica.pop()184 self.filaSemantica.pop()185 EXP_R = self.filaSemantica.pop()186 self.filaSemantica.pop()187 self.filaSemantica.pop()188 self.armazenarEscritaDoCodigo += ("{}if ( {} )\n".format("\t",EXP_R["Classe"]) + "\t{\n")189 self.filaSemantica.append({'Classe': "CAB", 'Token': "", 'Tipo': "", 'Linha': EXP_R["Linha"], 'Coluna': EXP_R["Coluna"]})190191 elif indiceDaProducoes == 27:192 print("EXP_R -> OPRD opr OPRD")193 OPRD2 = self.filaSemantica.pop()194 opr = self.filaSemantica.pop()195 OPRD1 = self.filaSemantica.pop()196197 if OPRD1["Tipo"] != '' and OPRD2["Tipo"] != '':198 if (OPRD1["Tipo"] == "int" or OPRD1["Tipo"] == 'double') and (OPRD2["Tipo"] == "int" or OPRD2["Tipo"] == 'double'):199 tx = self.tx200 self.filaSemantica.append({'Classe': "T{}".format(tx), 'Token': "boolean", 'Tipo': "boolean", 'Linha': OPRD1["Linha"], 'Coluna': OPRD1["Coluna"]})201 self.armazenarEscritaDoCodigo += ("{}T{} = {} {} {};\n".format("\t",tx, OPRD1["Classe"], opr["Tipo"], OPRD2["Classe"]))202 self.tx += 1203 else:204 print("Erro: Operandos com tipos incompatÃveis na linha {} e coluna {}".format(opr['Linha'], opr['Coluna']))205 self.analisadorDeErro = 1206 else:207 print("Erro: Variável não declarada na linha {} e coluna {}".format(opr["Linha"], opr["Coluna"]))208 self.analisadorDeErro = 1209 elif indiceDaProducoes == 33:210 print("R -> facaAte (EXP_R) CP_R ")211 self.filaSemantica.pop()212 self.filaSemantica.pop()213 EXP_R = self.filaSemantica.pop()214 self.filaSemantica.pop()215 self.filaSemantica.pop()216 self.armazenarEscritaDoCodigo += ("{}while ( {} )\n ".format("\t", EXP_R["Classe"]) + "\t{\n")217 self.armazenarEscritaDoCodigo += (self.armazenarLoop)218 self.filaSemantica.append({'Classe': "R", 'Token': "", 'Tipo': "", 'Linha': EXP_R["Linha"], 'Coluna': EXP_R["Coluna"]})219 elif indiceDaProducoes == 34:220 print("CP_R -> ES CP_R ")221 self.filaSemantica.pop()222 CP_R = self.filaSemantica.pop()223 self.filaSemantica.append({'Classe': "CP_R", 'Token': "", 'Tipo': "", 'Linha': CP_R["Linha"], 'Coluna': CP_R["Coluna"]})224 elif indiceDaProducoes == 35:225 print("CP_R -> CMD CP_R ")226 self.filaSemantica.pop()227 CP_R = self.filaSemantica.pop()228 self.filaSemantica.append({'Classe': "CP_R", 'Token': "", 'Tipo': "", 'Linha': CP_R["Linha"], 'Coluna': CP_R["Coluna"]})229 elif indiceDaProducoes == 36:230 print("CP_R -> COND CP_R ")231 self.filaSemantica.pop()232 CP_R = self.filaSemantica.pop()233 self.filaSemantica.append({'Classe': "CP_R", 'Token': "", 'Tipo': "", 'Linha': CP_R["Linha"], 'Coluna': CP_R["Coluna"]})234 elif indiceDaProducoes == 37:235 print("CP_R -> fimFaca ")236 CP_R = self.filaSemantica.pop()237 self.filaSemantica.append(CP_R)
...
coverage_report_builder.py
Source:coverage_report_builder.py
1'''2Created on Mar 27, 20203@author: ballance4'''5from ucis.scope_type_t import ScopeTypeT6from ucis.report.coverage_report import CoverageReport7from ucis.coverpoint import Coverpoint8from ucis.cover_type_t import CoverTypeT9from math import ceil10from ucis.cross import Cross11class CoverageReportBuilder(object):12 """13 Builds a coverage-report object from a UCIS database14 """15 16 def __init__(self, db):17 self.db = db18 self.report = CoverageReport()19 20 @staticmethod21 def build(db : 'UCIS') ->'CoverageReport':22 """23 Builds a CoverageReport object from a UCIS database24 """25 26 builder = CoverageReportBuilder(db)27 return builder._build()28 29 def _build(self)->'CoverageReport':30 31 for iscope in self.db.scopes(ScopeTypeT.INSTANCE):32 self.build_covergroups(iscope)33 34 return self.report35 36 def build_covergroups(self, iscope):37 38 coverage = 0.039 div = 040 41 for cg_t in iscope.scopes(ScopeTypeT.COVERGROUP):42 cg = self.build_covergroup(cg_t)43 if cg.weight > 0:44 coverage += cg.coverage * cg.weight45 div += cg.weight46 self.report.covergroups.append(cg)47 self.report.covergroup_m[cg.instname] = cg48 self.report.coverage = coverage/div49 50 def build_covergroup(self, cg_n)->CoverageReport.Covergroup:51 cg_r = CoverageReport.Covergroup(52 cg_n.getScopeName(),53 cg_n.getScopeName())54 55 cg_r.weight = cg_n.getWeight()56 57 for cp_in in cg_n.scopes(ScopeTypeT.COVERPOINT):58 cg_r.coverpoints.append(self.build_coverpoint(cp_in))59 60 for cr_in in cg_n.scopes(ScopeTypeT.CROSS):61 cg_r.crosses.append(self.build_cross(cr_in))62 for cg_in in cg_n.scopes(ScopeTypeT.COVERINSTANCE):63 cg_r.covergroups.append(self.build_covergroup(cg_in))64 65 # Determine the covergroup coverage66 coverage = 0.067 div = 068 for cp in cg_r.coverpoints:69 if cp.weight > 0:70 coverage += cp.coverage * cp.weight71 div += cp.weight72 73 for cr in cg_r.crosses:74 coverage += cr.coverage * cr.weight75 div += cr.weight76 77 if div > 0: coverage /= div78 cg_r.coverage = coverage79 80 return cg_r81 def build_coverpoint(self, cp_n : Coverpoint):82 cp_r = CoverageReport.Coverpoint(cp_n.getScopeName())83 cp_r.weight = cp_n.getWeight()84 85 # Read in bins86 num_hit = 087 total = 088 for ci_n in cp_n.coverItems(CoverTypeT.CVGBIN):89 cvg_data = ci_n.getCoverData()90 91 if cvg_data.data >= cvg_data.at_least:92 num_hit += 193 94 cp_r.bins.append(CoverageReport.CoverBin(95 ci_n.getName(),96 cvg_data.at_least,97 cvg_data.data))98 total += 199 100 for ci_n in cp_n.coverItems(CoverTypeT.IGNOREBIN):101 cvg_data = ci_n.getCoverData()102 103 cp_r.ignore_bins.append(CoverageReport.CoverBin(104 ci_n.getName(),105 cvg_data.at_least,106 cvg_data.data))107 108 for ci_n in cp_n.coverItems(CoverTypeT.ILLEGALBIN):109 cvg_data = ci_n.getCoverData()110 111 cp_r.illegal_bins.append(CoverageReport.CoverBin(112 ci_n.getName(),113 cvg_data.at_least,114 cvg_data.data))115 if total > 0:116 cp_r.coverage = (100*num_hit)/total117 else:118 cp_r.coverage = 0119 120 return cp_r121 122 def build_cross(self, cr_n : Cross):123 cr_r = CoverageReport.Cross(cr_n.getScopeName())124 125 # Read in bins126 num_hit = 0127 total = 0128 for ci_n in cr_n.coverItems(CoverTypeT.CVGBIN):129 cvg_data = ci_n.getCoverData()130 if cvg_data.data >= cvg_data.at_least:131 num_hit += 1132 133 cr_r.bins.append(CoverageReport.CoverBin(134 ci_n.getName(),135 cvg_data.goal,136 cvg_data.data))137 total += 1138 if total > 0: cr_r.coverage = (100*num_hit)/total139 140 return cr_r 141 142 143 ...
spline.py
Source:spline.py
1# -*- coding: utf-8 -*-2# <nbformat>3.0</nbformat>3# <codecell>4import numpy as np5def spline(cp_mu,cp_R):6 7 N = [4, 6, 10, 18, 34]8 mu_d = []9 R_d = []10 # Interpolation for mu11 # Double the vector12 for j in range(0, len(N)):13 for ele in cp_mu:14 mu_d.append(ele)15 mu_d.append(ele)16 mu_d_av = np.array(mu_d)17 # First Average18 for i in range(1,len(mu_d)):19 mu_d_av[i] = (mu_d[i]+mu_d[i-1])/220 mu_d_av2 = np.array(mu_d_av)21 # Second Average22 for i in range(1,len(mu_d_av)):23 mu_d_av2[i] = (mu_d_av[i]+mu_d_av[i-1])/224 cp_mu = mu_d_av2[-N[j]:]25 mu_d = []26 27 # Interpolation for R28 for ele in cp_R:29 R_d.append(ele)30 R_d.append(ele)31 R_d_av = np.array(R_d)32 # First Average33 for i in range(1,len(R_d)):34 R_d_av[i] = (R_d[i]+R_d[i-1])/235 R_d_av2 = np.array(R_d_av)36 # Second Average37 for i in range(1,len(R_d_av)):38 R_d_av2[i] = (R_d_av[i]+R_d_av[i-1])/239 cp_R = R_d_av2[-N[j]:]40 R_d = []41 mu = np.exp(cp_mu)42 R = np.exp(cp_R)...
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!!