Best Python code snippet using yandex-tank
gmo.py
Source:gmo.py  
1import struct2import io3def read_chunk_header(data):4    return struct.unpack_from('<HHI', data)5class Gmo:6    def __init__(self, data):7        # check magic numbers8        assert data[0:16] == b'OMG.00.1PSP\00\00\00\00\00'9        # skip over top level entry (0x0002)10        data = data[16:]11        chunk_type, header_size, chunk_size = read_chunk_header(data)12        assert chunk_type == 0x000213        data = data[header_size:]14        self.subfiles = []15        # read subfiles16        while len(data) > 0:17            if len(data) < 8:18                print('# WARN: not enough data to read last chunk')19                break20            chunk_type, header_size, chunk_size = read_chunk_header(data)21            if chunk_type == 0:22                print('# WARN: chunk type 0 encountered')23                break24            assert chunk_type == 325            self.subfiles.append(GmoSubfile(data[8:header_size], data[header_size:chunk_size]))26            data = data[chunk_size:]27class GmoSubfile:28    def __init__(self, header, data):29        self.bones = []30        self.objects = []31        self.materials = []32        self.textures = []33        # read chunks34        while len(data) > 0:35            chunk_type, header_size, chunk_size = read_chunk_header(data)36            if chunk_type == 4: # bone37                self.bones.append(GmoBone(data[8:header_size], data[header_size:chunk_size]))38            elif chunk_type == 5: # object39                self.objects.append(GmoObject(data[8:header_size], data[header_size:chunk_size]))40            elif chunk_type == 8: # material41                self.materials.append(GmoMaterial(data[8:header_size], data[header_size: chunk_size]))42            elif chunk_type == 0xa: # texture43                self.read_texture(data[8:header_size], data[header_size:chunk_size])44            else:45                print('# TODO: subfile chunk type {:04x} [{}]'.format(chunk_type, data[:chunk_size].hex()))46            data = data[chunk_size:]47    def read_texture(self, header, data):48        while len(data) > 0:49            chunk_type, header_size, chunk_size = read_chunk_header(data)50            assert chunk_type == 0x8012 and header_size == 051            if chunk_type == 0x8012: # texture... path?? in development???52                # oh my god its even a png53                # maybe theres a toggle to use this instead of the usual tga?54                path = data[8:chunk_size].decode().rstrip('\00')55                self.textures.append(path)56            else:57                print('# TODO: texture chunk type {:04x} [{}]'.format(chunk_type, data[:chunk_size].hex()))58            data = data[chunk_size:]59    def write_mtl(self, mtl):60        for i, material in enumerate(self.materials):61            print('newmtl {:02}'.format(i), file=mtl)62            material._write_mtl(self, mtl)63            print(file=mtl)64class GmoBone:65    def __init__(self, header, data):66        while len(data) > 0:67            chunk_type, header_size, chunk_size = read_chunk_header(data)68            print('# TODO: bone chunk type {:04x}'.format(chunk_type))69            data = data[chunk_size:]70class GmoVertex:71    def __init__(self, uv, norm, pos):72        self.uv = uv73        self.norm = norm74        self.pos = pos75class GmoMesh:76    def __init__(self, material_index, faces):77        self.material_index = material_index78        self.faces = faces79class GmoObject:80    def __init__(self, header, data):81        self.meshes = []82        self.vertices = []83        while len(data) > 0:84            chunk_type, header_size, chunk_size = read_chunk_header(data)85            if chunk_type == 6: # mesh86                self.meshes.append(GmoMesh(data[8:header_size], data[header_size:chunk_size]))87            elif chunk_type == 7: # vertex array88                #print(data[header_size:chunk_size].hex())89                # TODO: what the fuck do these values mean90                val0, vertex_count = struct.unpack_from('<II', data[header_size:])91                if val0 == 0x240011ff:92                    vertex_size = 9*493                elif val0 == 0x200011e3:94                    vertex_size = 8*495                else:96                    print('# TODO: vertex array format {:08x}'.format(val0))97                vertex_data_offset = chunk_size - vertex_count*vertex_size98                for i in range(vertex_count):99                    if val0 == 0x240011ff:100                        # unknown third value101                        u, v, _, nx, ny, nz, x, y, z = struct.unpack_from('<fff fff fff', data[vertex_data_offset + i*vertex_size:])102                    elif val0 == 0x200011e3:103                        u, v, nx, ny, nz, x, y, z = struct.unpack_from('<ff fff fff', data[vertex_data_offset + i*vertex_size:])104                    self.vertices.append(GmoVertex((u, v), (nx, ny, nz), (x, y, z)))105            else:106                print('# TODO: model surface chunk type {:04x} [{}]'.format(chunk_type, data[:chunk_size].hex()))107            data = data[chunk_size:]108    def write_obj(self, obj, mtl_path):109        print('mtllib {}'.format(mtl_path), file=obj)110        print(file=obj)111        for vertex in self.vertices:112            print('v {} {} {}'.format(*vertex.pos), file=obj)113            print('vt {} {}'.format(vertex.uv[0], 1.0 - vertex.uv[1]), file=obj)114            print('vn {} {} {}'.format(*vertex.norm), file = obj)115        print(file=obj)116        for i, mesh in enumerate(self.meshes):117            print('g {:02}'.format(i), file=obj)118            print('usemtl {:02}'.format(mesh.material_index), file=obj)119            for face in mesh.faces:120                assert len(face) in [3, 4]121                if len(face) == 3:122                    print('f {a}/{a}/{a} {b}/{b}/{b} {c}/{c}/{c}'.format(a=face[0]+1, b=face[1]+1, c=face[2]+1), file=obj)123                elif len(face) == 4:124                    print('f {a}/{a}/{a} {b}/{b}/{b} {c}/{c}/{c} {d}/{d}/{d}'.format(a=face[0]+1, b=face[1]+1, c=face[2]+1, d=face[3]+1), file=obj)125class GmoMesh:126    def __init__(self, header, data):127        self.faces = []128        while len(data) > 0:129            chunk_type, header_size, chunk_size = read_chunk_header(data)130            131            if chunk_type == 0x8061: # material info132                self.material_index = struct.unpack_from('< H', data[8:])[0] - 0x2000133            elif chunk_type == 0x8066: # index data134                assert header_size == 0135                header_size = 8136                val0, val1, primitive_type = struct.unpack_from('< HHI', data[header_size:])137                val0 = val0 - 0x1000 # an index... into what? vertex array probably138                #assert val0 == 0139                assert val1 == 7 #no idea about this one140                stripe_size, stripe_count = struct.unpack_from('< II', data[header_size+8:])141                if primitive_type == 3: # triangles142                    assert stripe_count == 1143                    #print(stripe_size, (chunk_size - (header_size+16))/2)144                    for i in range(stripe_size//3):145                        a, b, c = struct.unpack_from('< HHH', data[header_size+16 + i*6:])146                        self.faces.append((a, b, c))147                    return148                elif primitive_type == 4: # quads149                    print(stripe_size, stripe_count)150                    for i in range(stripe_count):151                        for j in range(stripe_size//2 - 1):152                            a, b, d, c = struct.unpack_from('< HHHH', data[header_size+16 + i*stripe_size*2 + j*4:])153                            self.faces.append((a, b, c, d))154                else:155                    print('# WARN: unknown primitive type {}'.format(primitive_type))156            else:157                print('# TODO: mesh chunk type {:04x} [{}]'.format(chunk_type, data[:chunk_size].hex()))158            data = data[chunk_size:]159class GmoMaterial:160    def __init__(self, header, data):161        while len(data) > 0:162            chunk_type, header_size, chunk_size = read_chunk_header(data)163            if chunk_type == 9: # texture reference164                print('- texture ref -')165                print(header_size, chunk_size)166                print(data[8:header_size].hex())167                print(data[header_size:].hex())168                self.texture_index = struct.unpack_from('<H', data[header_size+8:])[0] - 0x2000169            elif chunk_type == 0x8082: # rgba color170                r, g, b, a = struct.unpack('< ffff', data[8:chunk_size])171                self.color = (r, g, b, a)172            else:173                print('# TODO: material chunk type {:04x} [{}]'.format(chunk_type, data[:chunk_size].hex()))174            data = data[chunk_size:]175    def _write_mtl(self, subfile, mtl):176        if hasattr(self, 'texture_index'):...wave.py
Source:wave.py  
...68		sp_wavfile.write(filepath, self._sample_rate, data_int)69	@staticmethod70	def from_file(filepath):71		logging.info('Loading wav ' + filepath)72		def read_chunk_header(file):73			chunk_id = file.read(4)74			chunk_size, = struct.unpack('<I', file.read(4))75			return chunk_id, chunk_size76		with open(filepath, 'rb') as file:77			riff_id, riff_size = read_chunk_header(file)78			if riff_id != b'RIFF':79				raise ValueError('RIFF header id incorrect', riff_id)80			# check if you want, but pretty much ignore81			wave_id = file.read(4)82			fmt_id, fmt_size = read_chunk_header(file)83			if fmt_id != b'fmt ':84				raise ValueError('FMT header id incorrect', riff_id)85			fmt_chunk = file.read(16)86			audio_fmt, num_chan, sr, bytes_per_sec, block_align, bit_depth = struct.unpack('<HHIIHH', fmt_chunk)87			# skip over the extended format stuff for now88			fmt_ext = file.read(fmt_size - 16)89			next_chunk_id, next_chunk_size = read_chunk_header(file)90			while next_chunk_id != b'data':91				file.seek(next_chunk_size, 1)92				next_chunk_id, next_chunk_size = read_chunk_header(file)93			data_start = file.tell()94			data_len = next_chunk_size95			byte_depth = bit_depth / 896			num_frames = int(data_len / byte_depth / num_chan)97			file = tf.read_file(filepath)98			audio_data = tf.decode_raw(tf.substr(file, data_start, data_len), BIT_DEPTH_TYPE[bit_depth], little_endian=True)99			audio_data = tf.cast(audio_data, tf.float32) / (2 ** (bit_depth - 1))100			audio_data = tf.reshape(audio_data, [num_frames, num_chan])...playaiff.py
Source:playaiff.py  
...10	sampwidth = c.getwidth()11	samprate = 0.0 # unknown12	filename = sys.argv[1]13	f = open(filename, 'r')14	type, totalsize = aiff.read_chunk_header(f)15	if type <> 'FORM':16		raise aiff.Error, 'FORM chunk expected at start of file'17	aiff.read_form_chunk(f)18	while 1:19		try:20			type, size = aiff.read_chunk_header(f)21		except EOFError:22			break23		if v: print 'header:', `type`, size24		if type == 'COMM':25			nchannels, nsampframes, sampwidth, samprate = \26				aiff.read_comm_chunk(f)27			if v: print nchannels, nsampframes, sampwidth, samprate28		elif type == 'SSND':29			offset, blocksize = aiff.read_ssnd_chunk(f)30			if v: print offset, blocksize31			data = f.read(size-8)32			if size%2: void = f.read(1)33			p = makeport(nchannels, sampwidth, samprate)34			play(p, data, offset, blocksize)...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!!
