Best Python code snippet using pytest-cov
pycencap.py
Source:pycencap.py
...77 s += 'arraytype(%s)'%(self.arraytype)78 s += 'arraysize(%s)'%(self.arraysize)79 s += 'enumtype(%s)'%(self.enumtype)80 if self.structnode is not None:81 s += 'structnode(%s)'%(get_node_desc(self.structnode))82 else:83 s += 'structnode(None)'84 if self.funcdeclnode is not None:85 s += 'funcdeclnode(%s)'%(get_node_desc(self.funcdeclnode))86 else:87 s += 'funcdelc(None)'88 s += 'namevarname(%s)'%(self.namevarname)89 s += 'checkptridx(%d)'%(self.checkptridx)90 s += 'checkarrayidx(%d)'%(self.checkarrayidx)91 s += 'prevnode(%s)'%(self.prevnode)92 return s9394 def __iadd__(self,other):95 if not isinstance(other,NodeTypeDecl):96 raise Exception('not NodeTypeDecl')97 if len(other.parent_typename) > 0:98 self.parent_typename = other.parent_typename99 if len(other.parent_argname) > 0:100 self.parent_argname = other.parent_argname101 if len(other.typename) > 0:102 self.typename = other.typename103 if len(other.memname) > 0 :104 self.memname = other.memname105 if other.ptrtype > 0:106 self.ptrtype += other.ptrtype107 if other.arraytype > 0 :108 self.arraytype += other.arraytype109 if len(other.arraysize) != 0:110 self.arraysize.extend(other.arraysize)111 if other.enumtype :112 self.enumtype = other.enumtype113 if self.prevnode is None and other.prevnode is not None:114 self.prevnode = other.prevnode115 if self.structnode is None and other.structnode is not None:116 self.structnode = other.structnode117 if self.funcdeclnode is None and other.funcdeclnode is not None:118 self.funcdeclnode = other.funcdeclnode119 return self 120121 def clone(self,other):122 if not isinstance(other,NodeTypeDecl):123 raise Exception('not NodeTypeDecl')124 self.parent_typename = other.parent_typename125 self.parent_argname = other.parent_argname126 self.typename = other.typename127 self.memname = other.memname128 self.ptrtype = other.ptrtype129 self.arraytype = other.arraytype130 self.arraysize = other.arraysize131 self.enumtype = other.enumtype132 self.prevnode = other133 self.structnode = other.structnode134 self.funcdeclnode = other.funcdeclnode135 self.namevarname = other.namevarname136 self.checkptridx = other.checkptridx137 self.checkarrayidx = other.checkarrayidx138 return139140def get_node_desc(node,tabs=0):141 sio = StringIO.StringIO()142 node.show(sio,offset=tabs*4)143 return sio.getvalue()144145def __has_decl_mem(cnode):146 retval = False147 for (cname,child) in cnode.children():148 if isinstance(child,pycparser.c_ast.Decl):149 return True150 elif isinstance(child,pycparser.c_ast.FuncDecl):151 return True152 return retval153154def __debug_node_array(nodearr,fmt):155 s = ''156 s += '%s\n'%(fmt)157 i = 0158 for k in nodearr:159 s += '[%d]\n%s'%(i,get_node_desc(k))160 i += 1161 return s162163def debug_node_array(nodearr,fmt):164 return __debug_node_array(nodearr,fmt)165166def __get_struct_decl(cnode):167 retnames=[]168 for (cname,child) in cnode.children():169 if isinstance(child,pycparser.c_ast.Struct):170 #logging.info('append (%s)'%(child.name))171 retnames.append(child.name)172 else:173 retnames.extend(__get_struct_decl(child))174 return retnames175176177def __get_inner_struct(cnode):178 retnodes = []179 for (cname,c) in cnode.children():180 if isinstance(c,pycparser.c_ast.Struct):181 retval = __has_decl_mem(c)182 if retval:183 retnodes.append(c)184 else:185 retnodes.extend(__get_inner_struct(c))186 return retnodes187188def get_struct_node(ast,structname):189 retnode = []190 if structname in ast.structdef.keys():191 retnode.append(ast.structdef[structname])192 return retnode193194195def __has_enum_list(cnode):196 retval = False197 for (cname ,child) in cnode.children():198 if isinstance(child,pycparser.c_ast.EnumeratorList):199 return True200 else:201 retval = __has_enum_list(child)202 if retval:203 return True204 return False205206def __get_enum_decl(cnode):207 retnames=[]208 for (cname,child) in cnode.children():209 if isinstance(child,pycparser.c_ast.Enum) and child.name is not None:210 #logging.info('append (%s)'%(child.name))211 retnames.append(child.name)212 else:213 retnames.extend(__get_enum_decl(child))214 return retnames215216def get_enum_node(ast,enumname):217 retnode = []218 if enumname in ast.enumdef.keys():219 retnode.append(ast.enumdef[enumname])220 return retnode221222def get_decl_types(cnode):223 retnodes = []224 for (cname,child) in cnode.children():225 if isinstance(child,pycparser.c_ast.IdentifierType):226 retnodes.append(' '.join(child.names))227 else:228 retnodes.extend(get_decl_types(child))229 return retnodes230231def __oprate_node_format(c):232 s = ''233 if isinstance(c,pycparser.c_ast.BinaryOp):234 childrends = []235 i = 0236 for (dname,d) in c.children():237 childrends.append(__oprate_node_format(d))238 i += 1239 assert(i == 2)240 if c.op in ['+','/','*','-']:241 s += '(%s %s %s)'%(childrends[0],c.op,childrends[1])242 else:243 logging.warn('unknown op %s'%(c.op))244 elif isinstance(c,pycparser.c_ast.UnaryOp):245 childrends = []246 i = 0247 for (dname,d) in c.children():248 childrends.append(__oprate_node_format(d))249 i += 1250 assert(i == 1)251 if c.op == 'sizeof':252 s += '(sizeof(%s))'%(childrends[0])253 elif c.op == '-':254 s += '(-%s)'%(childrends[0])255 else:256 logging.warn('unknown op (%s)'%(c.op))257 elif isinstance(c,pycparser.c_ast.Constant):258 s += '(%s)'%(c.value)259 elif isinstance(c,pycparser.c_ast.Typename):260 for (dname,d) in c.children():261 s += __oprate_node_format(d)262 elif isinstance(c,pycparser.c_ast.TypeDecl):263 for (dname,d) in c.children():264 s += __oprate_node_format(d)265 elif isinstance(c,pycparser.c_ast.IdentifierType):266 s += '%s'%(' '.join(c.names))267 else:268 logging.warn('unknown\n%s'%(get_node_desc(c)))269 logging.info('s(%s)\n%s'%(s,get_node_desc(c)))270 return s271272273def oprate_node_format(opnode):274 s = ''275 if isinstance(opnode,pycparser.c_ast.BinaryOp):276 i = 0277 childrends = []278 for (cname,c) in opnode.children():279 assert(i < 2)280 childrends.append(__oprate_node_format(c))281 i += 1282 assert(i == 2)283 if opnode.op in ['+','/','*','-']:284 s += '(%s %s %s)'%(childrends[0],opnode.op,childrends[1])285 else:286 logging.warn('unknown op (%s)\n%s'%(opnode.op,get_node_desc(opnode)))287 elif isinstance(opnode,pycparser.c_ast.UnaryOp):288 i = 0289 childrends = []290 for (cname,c) in opnode.children():291 assert(i < 1)292 childrends.append(__oprate_node_format(c))293 i += 1294 assert(i == 1)295 if opnode.op == 'sizeof':296 s += '(sizeof(%s))'%(childrends[0])297 elif opnode.op == '-' :298 s += '(-%s)'%(childrens[0])299 else:300 logging.warn('unknown op (%s)\n%s'%(opnode.op,get_node_desc(opnode)))301 else:302 logging.warn('unknown \n%s'%(get_node_desc(opnode)))303 logging.info('s(%s)\n%s'%(s,get_node_desc(opnode)))304 return s305306def __get_decl_type_name(ast,cnode):307 nodetype = NodeTypeDecl()308 for (dname,d) in cnode.children():309 if isinstance(d,pycparser.c_ast.TypeDecl):310 nodetype += __get_decl_type_name(ast,d)311 elif isinstance(d,pycparser.c_ast.IdentifierType):312 nodetype.typename = ' '.join(d.names)313 elif isinstance(d,pycparser.c_ast.PtrDecl):314 nodetype += __get_decl_type_name(ast,d)315 nodetype.ptrtype += 1316 elif isinstance(d,pycparser.c_ast.ArrayDecl):317 nodetype += __get_decl_type_name(ast,d)318 nodetype.arraytype += 1319 elif isinstance(d,pycparser.c_ast.Constant):320 nodetype.arraysize.append(d.value)321 elif isinstance(d,pycparser.c_ast.BinaryOp) or isinstance(d,pycparser.c_ast.UnaryOp):322 logging.info('oprade handle')323 nodetype.arraysize.append(oprate_node_format(d))324 elif isinstance(d,pycparser.c_ast.Struct):325 nodetype.structnode = d326 if d.name is not None:327 nodetype.typename = d.name328 elif isinstance(d,pycparser.c_ast.FuncDecl):329 nodetype.funcdeclnode = d330 else:331 logging.warn('unknown type (%s)\n%s'%(dname,get_node_desc(d)))332 nodetype += __get_decl_type_name(ast,d)333 return nodetype334335def get_decl_type_name(ast,cnode):336 nodetype = NodeTypeDecl()337 nodetype.memname = cnode.name338 nodetype += __get_decl_type_name(ast,cnode)339 assert(nodetype.arraytype >= len(nodetype.arraysize))340 if nodetype.arraytype > len(nodetype.arraysize):341 # this like int *m_arr[32][][]; 342 logging.info('arraytype %d len(%d)'%(nodetype.arraytype,len(nodetype.arraysize)))343 morecnt = (nodetype.arraytype - len(nodetype.arraysize))344 nodetype.arraytype -= morecnt345 nodetype.ptrtype += morecnt346 return nodetype347348def __get_typedef_type_name(ast,cnode):349 nodetype = NodeTypeDecl()350 for (cname,c) in cnode.children():351 if isinstance(c,pycparser.c_ast.IdentifierType):352 nodetype.typename = ' '.join(c.names)353 elif isinstance(c,pycparser.c_ast.PtrDecl):354 nodetype += __get_typedef_type_name(ast,c)355 nodetype.ptrtype += 1356 elif isinstance(c,pycparser.c_ast.ArrayDecl):357 nodetype += __get_typedef_type_name(ast,c)358 nodetype.arraytype += 1359 elif isinstance(c,pycparser.c_ast.Constant):360 nodetype.arraysize.append(int(c.value))361 elif isinstance(c,pycparser.c_ast.FuncDecl):362 nodetype.funcdeclnode = c363 else:364 logging.warn('unknown type (%s)\n%s'%(cname,get_node_desc(c)))365 nodetype += __get_decl_type_name(ast,c)366 return nodetype367368def get_typedef_type_name(ast,cnode):369 nodetype = NodeTypeDecl()370 nodetype += __get_typedef_type_name(ast,cnode)371 return nodetype372373374def get_declare_member(cnodes):375 retnodes = []376 for (cname,c) in cnodes.children():377 if isinstance(c,pycparser.c_ast.Decl):378 retnodes.append(c)379 return retnodes380381382def __append_list_prevnode(prevnode,cnode):383 nodetype = NodeTypeDecl()384 nodetype.prevnode = prevnode385 nodetype.structnode = cnode386 return nodetype387388def __callback_struct_enum_def(newast,c,prevnode,callback):389 nodetype = __append_list_prevnode(prevnode,c)390 if callback is not None:391 callback(newast,c,nodetype)392 nodetype = None393 return394395def __get_name_prevnode(cnode,prevnode):396 retnames = []397 curname = None398 if hasattr(cnode,'name'):399 curname = cnode.name400 else:401 assert(isinstance(cnode,pycparser.c_ast.IdentifierType) or \402 isinstance(cnode,pycparser.c_ast.FuncDecl))403 curprevnode = prevnode404 if curname is not None:405 retnames.append(curname)406 while curprevnode is not None:407 cn = curprevnode.structnode408 if isinstance(cn ,pycparser.c_ast.TypeDecl):409 if cn.declname is not None:410 curname = cn.declname411 retnames.append(curname)412 elif isinstance(cn,pycparser.c_ast.Typedef):413 if cn.name is not None:414 curname = cn.name415 retnames.append(curname)416 curprevnode = curprevnode.prevnode417 return retnames418419def __has_func_pointer(cnode,prevnode):420 curprevnode = prevnode421 retval = False422 while curprevnode is not None:423 cn = curprevnode.structnode424 if isinstance(cn , pycparser.c_ast.PtrDecl):425 retval = True426 break427 curprevnode = curprevnode.prevnode428 if retval:429 # we check that it is under Typedef node430 curprevnode = prevnode431 while curprevnode is not None:432 cn = curprevnode.structnode433 if isinstance(cn , pycparser.c_ast.Typedef):434 retval = True435 break436 curprevnode = curprevnode.prevnode437 return retval438439basic_types = [440 'long unsigned int',441 'unsigned char',442 'unsigned short int',443 'unsigned int',444 'unsigned long int',445 'signed char',446 'signed short int',447 'signed int',448 'signed long int',449 'long int',450 'int',451 'void',452 'char',453 'short int']454455def __get_identifier_node(prevnode):456 # we get the toppest node457 retnode = None458 curprevnode = prevnode459 i = 0460 while curprevnode is not None:461 cn = curprevnode.structnode462 retnode = cn463 if isinstance(cn ,pycparser.c_ast.TypeDecl) or isinstance(cn,pycparser.c_ast.Typedef) or \464 isinstance(cn,pycparser.c_ast.PtrDecl) or isinstance(cn,pycparser.c_ast.ArrayDecl) or \465 isinstance(cn,pycparser.c_ast.IdentifierType) or isinstance(cn,pycparser.c_ast.FuncDecl):466 pass467 else:468 logging.warn('unknown IdentifierType[%d](%s)\n%s'%(i,cn.__class__.__name__,get_node_desc(cn)))469 i += 1470 curprevnode = curprevnode.prevnode471 return retnode472473def __make_struct_enum_def(newast,cnode,prevnode):474 if isinstance(cnode,pycparser.c_ast.Typedef) or isinstance(cnode,pycparser.c_ast.Decl) or \475 isinstance(cnode,pycparser.c_ast.PtrDecl) or isinstance(cnode,pycparser.c_ast.TypeDecl) or \476 isinstance(cnode,pycparser.c_ast.ArrayDecl) or \477 isinstance(cnode,pycparser.c_ast.Constant) or isinstance(cnode,pycparser.c_ast.BinaryOp) or \478 isinstance(cnode,pycparser.c_ast.InitList) or isinstance(cnode,pycparser.c_ast.NamedInitializer) or\479 isinstance(cnode,pycparser.c_ast.CompoundLiteral) or isinstance(cnode,pycparser.c_ast.Typename) or \480 isinstance(cnode,pycparser.c_ast.Cast) or isinstance(cnode,pycparser.c_ast.UnaryOp) or \481 isinstance(cnode,pycparser.c_ast.StructRef):482 for (cname,c) in cnode.children():483 __callback_struct_enum_def(newast,c,prevnode,__make_struct_enum_def)484 elif isinstance(cnode,pycparser.c_ast.Struct):485 retnodes = get_declare_member(cnode)486 if len(retnodes) > 0:487 retnames = __get_name_prevnode(cnode,prevnode)488 if len(retnames) > 0:489 for curname in retnames:490 if curname not in newast.structdef.keys():491 newast.structdef[curname] = cnode492 else:493 if get_node_desc(cnode) != get_node_desc(newast.structdef[curname]) :494 logging.warn('(%s)(%s) already defined (%s)'%(495 curname,get_node_desc(cnode),get_node_desc(newast.structdef[curname])))496 else:497 newast.structnotlists.append(cnode)498 newast.structnotlists_prevnode.append(prevnode)499 else:500 newast.structnotlists.append(cnode)501 newast.structnotlists_prevnode.append(prevnode)502 elif isinstance(cnode,pycparser.c_ast.Enum):503 retval = __has_enum_list(cnode)504 if retval :505 retnames = __get_name_prevnode(cnode,prevnode)506 if len(retnames) > 0 :507 for curname in retnames:508 if curname not in newast.enumdef.keys():509 newast.enumdef[curname] = cnode510 else:511 if get_node_desc(cnode) != get_node_desc(newast.enumdef[curname]):512 logging.warn('(%s)(%s) already defined (%s)'%(513 curname,get_node_desc(cnode),get_node_desc(newast.enumdef[curname])))514 else:515 newast.enumnotlists.append(cnode)516 newast.enumnotlists_prevnode.append(prevnode)517 else:518 newast.enumnotlists.append(cnode)519 newast.enumnotlists_prevnode.append(prevnode)520521 elif isinstance(cnode,pycparser.c_ast.FuncDef) or \522 isinstance(cnode,pycparser.c_ast.ID) or isinstance(cnode,pycparser.c_ast.Union):523 pass524 elif isinstance(cnode,pycparser.c_ast.IdentifierType):525 # now we should make like typedef unsigned long uint32_t;526 # this is the code527 retnames = __get_name_prevnode(cnode,prevnode)528 name = ' '.join(cnode.names) 529 if len(name) > 0:530 if name not in basic_types and name not in newast.structdef.keys():531 logging.warn('(%s) not in basic types'%(name))532 structnode = __get_identifier_node(prevnode)533 if len(retnames) > 0:534 for curname in retnames:535 if curname not in newast.structdef.keys():536 newast.structdef[curname] = structnode537 else:538 if get_node_desc(structnode) != get_node_desc(newast.structdef[curname]):539 logging.warn('[%s]\n %s\n!!!!!!!!!!!!!\n %s\n++++++++++++\n'%(540 curname,get_node_desc(structnode),get_node_desc(newast.structdef[curname])))541 else:542 logging.warn('(%s) no defines'%(prevnode))543 else:544 logging.warn('(%s) IdentifierType\n %s'%(prevnode,get_node_desc(cnode)))545 elif isinstance(cnode,pycparser.c_ast.FuncDecl):546 if __has_func_pointer(cnode,prevnode):547 # we have defined this pointer548 retnames = __get_name_prevnode(cnode,prevnode)549 # no names550 structnode = __get_identifier_node(prevnode)551 if len(retnames) > 0:552 for curname in retnames:553 if curname not in newast.structdef.keys():554 newast.structdef[curname] = structnode555 else:556 if get_node_desc(structnode) != get_node_desc(newast.structdef[curname]):557 logging.warn('[%s]\n %s\n!!!!!!!!!!!!!\n %s\n++++++++++++\n'%(558 curname,get_node_desc(structnode),get_node_desc(newast.structdef[curname])))559 else:560 logging.warn('(%s) no defines'%(prevnode))561 else:562 logging.warn('can not pass %s(%s)'%(cnode.__class__.__name__,get_node_desc(cnode)))563 return564565class NewAst(object):566 pass567568def make_struct_enum_def(ast):569 newast = NewAst()570 newast.structdef = dict()571 newast.enumdef = dict()572 newast.structnotlists = []573 newast.enumnotlists = []574 newast.structnotlists_prevnode = []575 newast.enumnotlists_prevnode = []576 newast.ast = ast577578 for (cname,c) in ast.children():579 if isinstance(c,pycparser.c_ast.Decl) and c.name is not None:580 # we do not scan for declare581 # like extern File* stderr;582 continue583 prevnode = NodeTypeDecl()584 prevnode.structnode = c585 __make_struct_enum_def(newast,c,prevnode)586 prevnode = None587588 i = 0589 assert(len(newast.enumnotlists) == len(newast.enumnotlists_prevnode))590 while i < len(newast.enumnotlists):591 k = newast.enumnotlists[i]592 p = newast.enumnotlists_prevnode[i]593 enumnode = None594 if k.name is not None:595 if k.name in newast.enumdef.keys():596 enumnode = newast.enumdef[k.name]597 retnames = __get_name_prevnode(k,p)598 if len(retnames) > 0:599 for n in retnames:600 if n not in newast.enumdef.keys() :601 if enumnode is None:602 logging.warn('[%d]\n %s(%s)'%(i,get_node_desc(k),p))603 else:604 newast.enumdef[n] = enumnode605 elif enumnode is not None:606 if get_node_desc(enumnode) != get_node_desc(newast.enumdef[n]):607 logging.warn('[%d]\n %s(%s)\n %s'%(608 i,get_node_desc(k),p,get_node_desc(newast.enumdef[n])))609 else:610 logging.warn('[%d]node:\n %s\n %s'%(i,get_node_desc(k),p))611 # now to get the name612 i += 1613 newast.enumnotlists = []614 newast.enumnotlists_prevnode = []615 i = 0616 assert(len(newast.structnotlists) == len(newast.structnotlists_prevnode))617 while i < len(newast.structnotlists):618 k = newast.structnotlists[i]619 p = newast.structnotlists_prevnode[i]620 structnode = None621 if k.name is not None:622 if k.name in newast.structdef.keys():623 structnode = newast.structdef[k.name]624 retnames = __get_name_prevnode(k,p)625 if len(retnames) > 0:626 for n in retnames:627 if n not in newast.structdef.keys() :628 if structnode is None:629 logging.warn('[%d]\n %s(%s)'%(i,get_node_desc(k),p))630 else:631 newast.structdef[n] = structnode632 elif structnode is not None:633 if get_node_desc(structnode) != get_node_desc(newast.structdef[n]):634 logging.warn('[%d]\n %s(%s)\n %s'%(635 i,get_node_desc(k),p,get_node_desc(newast.structdef[n])))636 else:637 logging.warn('[%d]node:\n %s\n %s'%(i,get_node_desc(k),p))638 # now to get the name639 i += 1640 newast.structnotlists = []641 newast.structnotlists_prevnode = []642 newast.ast = ast643 return newast644645646def parse_file_callback(gccecommand,file,callback=None,ctx=None):647 if len(gccecommand) == 0:648 ast = pycparser.parse_file(file,use_cpp=False)649 else:650 ast = pycparser.parse_file(file,use_cpp=True,cpp_path=gccecommand[0],651 cpp_args=gccecommand[1:])
...
dot_export.py
Source:dot_export.py
...4 def get_id(name):5 if name not in identifiers:6 identifiers[name] = f"_{len(identifiers)}"7 return identifiers[name]8 def get_node_desc(t: TensorDesc):9 return "\n".join(10 [t.name, "Parameters:"] +11 (["None"] if t.producer is None12 or len([i for i in t.producer.inputs if i.is_constant]) == 0 else13 [f"{i.name}: " +14 (f"{i.sparse_size}/{i.size // i.elem_size}"15 if i.sparse_size else f"{i.size // i.elem_size}")16 for i in t.producer.inputs if i.is_constant]) +17 [f"Shape: {t.shape[1:]}"])18 contents = f'''19 digraph g {{20 {"; ".join(f'{get_id(o.name)} [label="{get_node_desc(o)}", shape=box]' 21 for o in g.tensors.values() if not o.is_constant)}22 {"; ".join(f"{get_id(i.name)} -> {get_id(o.name)}" 23 for o in g.tensors.values() if o.producer is not None24 for i in o.producer.inputs if not i.is_constant)}25 }}26 '''27 with open(output_file, "w") as f:...
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!!