Best Python code snippet using avocado_python
types.py
Source:types.py
1""" Data types useful for Internet programming. """2from libs.util import *3import logging4from socket import inet_ntoa, inet_aton5import struct6class InetAddr:7 """ Internet address type. """8 def __init__(self, s):9 """ Initialize from a user-supplied dotted quad string. """10 self._a = s11 def toNetwork(self):12 """ Convert to network data format (four packed binary bytes). """13 return inet_aton(self._a)14 def __str__(self):15 return self._a16 def __repr__(self):17 return "<InetAddr %s>" % (self._a,)18 def __hash__(self):19 """ Returns a hash of this object based on its string rep. """20 return hash(self._a)21 def __cmp__(self, other):22 return cmp(self._a, other._a)23 def __eq__(self, other):24 return self._a == other._a25 @staticmethod26 def fromNetwork(n):27 return InetAddr(inet_ntoa(n))28class DomainName:29 """ Representation of a DNS domain name. """30 def __init__(self, s = ""):31 """ Initialize from a user-supplied string (optionally empty). """32 self._namelist = s.lstrip(".").split(".") # XXX this is subtle33 @staticmethod34 def fromData(data, offset = 0):35 """ Return a DomainName object from user-supplied packed binary36 data, with an optional offset modifier. """37 dn = DomainName()38 dn._namelist = [ ]39 dn._length = 040 followedPointer = False41 while offset < len(data): # loop over label sequence42 (labellen,) = struct.unpack_from("B", data, offset)43 if not labellen:44 dn._namelist.append('')45 if not followedPointer:46 dn._length += 147 break48 elif (labellen >> 6) == 3:49 # this is a pointer label (see RFC 1035, Section 4.1.4)50 (pointer,) = struct.unpack_from(">H", data, offset)51 pointer &= 0x3FFF52 offset = pointer53 if not followedPointer:54 dn._length += 255 followedPointer = True56 else:57 # else, this is a normal label (see RFC 1035, Section 4.1.2)58 offset = offset + 159 (labeldata,) = struct.unpack_from("%ds" % (labellen,), data, offset)60 offset = offset + labellen61 if not followedPointer:62 dn._length += 1 + labellen63 dn._namelist.append(labeldata)64 return dn65 def parent(self):66 """ Return a DomainName object that represents the parent domain. """67 if self != DomainName("."):68 res = DomainName()69 res._namelist = self._namelist[1:]70 return res71 return None72 def __copy__(self):73 """ Return a copy of this DomainName object that loses any74 pointer information that may exist in this object's rep. """75 res = DomainName(str(self))76 return res77 def __len__(self):78 """ Returns the length of the RFC 1035-compliant binary79 representation, taking into account pointers. """80 if not hasattr(self, '_length'):81 l = 082 for name in self._namelist:83 l += len(name) + 184 return l85 return self._length86 def __str__(self):87 """ Returns a '.'-terminated string representation of this88 object. """89 self._namelist90 res = []91 for n in self._namelist:92 if type(n) == str:93 res.append(n.lower())94 else:95 res.append(n.decode('utf-8','backslashreplace').lower())96 # XXX subtle again, same reason97 return ".".join(res).rstrip(".") + "." 98 def __hash__(self):99 """ Returns a hash of this object based on its string rep. """100 return hash(str(self))101 def __cmp__(self, other):102 return cmp(str(self).lower(), str(other).lower())103 def __eq__(self, other):104 return str(self).lower() == str(other).lower()105 def __repr__(self):106 return str(self)107 def pack(self):108 """ Packs this object into RFC 1035-compliant data and returns the109 resulting binary-packed string. """110 l = []111 for name in self._namelist:112 l.append(struct.pack("B", len(name)).decode('unicode-escape'))113 if type(name) == str:114 l.append(name)115 else:116 l.append(name.decode('unicode-escape'))117 res = ("".join(l)).encode('ISO-8859-1')...
split_train_test.py
Source:split_train_test.py
1# ImageSetsã«è¿½å ããç»ååãæ¸ãè¾¼ã2# ã©ã³ãã æ½åºã®ããã°ã©ã é©å½ã«æ¸ãããªã3# trainval.txt=train.txt4# val.txt=test.txt5import glob6import random7import os8class SplitData:9 def __init__(self, _rootPath, _perTrain):10 self.rootPath = _rootPath11 self.xmlPath = f"{_rootPath}Annotations/"12 self.imagePath = f"{_rootPath}JPEGImages/"13 self.perTrain = _perTrain14 def GetFileName(self, _listPath):15 _nameList = []16 for path in _listPath:17 _imName = os.path.splitext(os.path.basename(path))[0]18 _nameList.append(_imName)19 return _nameList20 def JoinPath(self, _nameList, _basePath):21 _pathList = []22 for name in _nameList:23 _joinPath = f"{_basePath}{name}.jpg"24 # print(_joinPath)25 _pathList.append(_joinPath)26 return _pathList27 def SplitList(self, _list, _perTrain):28 # listã8:2ã«åå²ï¼train:test)29 MaxSize = len(_list)30 size = int(MaxSize*_perTrain)31 # print(MaxSize,size,len(_list))32 train = _list[:size]33 test = _list[size:]34 return train, test35 def WriteTxt(self, _dataList, _file):36 path = f"{self.rootPath}ImageSets/Main/{_file}"37 with open(path, 'wt') as f:38 for data in _dataList:39 f.write("%s\n" % data)40 def Main(self):41 # xmlãã©ã«ããªã¹ãã®åå¾42 _xmlList = glob.glob(f"{self.xmlPath}*.xml")43 # xmlãã©ã«ããªã¹ããã©ã³ãã ã«ä¸¦ã³æ¿ãã44 random.shuffle(_xmlList)45 # xmlã®nameã®ã¿åå¾46 _nameList = self.GetFileName(_xmlList)47 # print(len(_nameList)) # 4192:æ£å¸¸48 # åæ§ã®imgãã©ã«ããªã¹ããä½æ(ãã¶ãããã)49 _imgList = self.JoinPath(_nameList, self.imagePath)50 # train : test = trainval : val = 9 : 1 ã§ImageSetsã«ãã¡ã¤ã«åã追è¨51 train, test = self.SplitList(_nameList, self.perTrain)52 train.sort()53 test.sort()54 print(f"{len(train),len(test),len(train)+len(test)}")55 # txtã«è¿½è¨56 self.WriteTxt(train, "train.txt")57 self.WriteTxt(test, "test.txt")58 print("complete!!")59if __name__ == '__main__':60 # trainãã¼ã¿ã®å²å61 perTrain = 0.962 perTest = 0.163 # ----------------------64 _rootPath = "H:/dataset/BearVer2_VOC/"65 a = SplitData(_rootPath, perTrain)...
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!!