How to use try_mask method in hypothesis

Best Python code snippet using hypothesis

bitstuf.py

Source:bitstuf.py Github

copy

Full Screen

1#!/usr/bin/env python2# Tool for testing bitstuffing implementations3#4# Copyright (C) 2022 Kevin O'Connor <kevin@koconnor.net>5#6# This file may be distributed under the terms of the GNU GPLv3 license.7import sys8TESTBITS=209def report(msg):10 sys.stderr.write("\n" + msg + "\n")11LOOP = UNLOOP = 012def bitstuf(b, num_bits):13 global LOOP14 edges = b ^ (b >> 1)15 count = num_bits16 i = num_bits-117 while i >= 0:18 LOOP += 119 if not ((edges >> i) & 0xf):20 mask = (1 << (i + 1)) - 121 low = b & mask22 high = (b & ~(mask >> 1)) << 123 b = high ^ low ^ (1 << i)24 i -= 325 count += 126 edges = b ^ (b >> 1)27 i -= 128 return b, count29def bitstuf_batch(b, num_bits):30 global LOOP31 count = num_bits32 while 1:33 edges = b ^ (b >> 1)34 e2 = edges | (edges >> 1)35 e4 = e2 | (e2 >> 2)36 add_bits = ~e437 try_cnt = num_bits38 while 1:39 LOOP += 140 try_mask = ((1 << try_cnt) - 1) << (num_bits - try_cnt)41 if not add_bits & try_mask:42 # No stuff bits needed in try_cnt bits43 if try_cnt >= num_bits:44 return b, count45 num_bits -= try_cnt46 try_cnt = (num_bits + 1) // 247 continue48 if add_bits & (1 << (num_bits - 1)):49 # A stuff bit must be inserted prior to the high bit50 low_mask = (1 << num_bits) - 151 low = b & low_mask52 high = (b & ~(low_mask >> 1)) << 153 b = high ^ low ^ (1 << (num_bits - 1))54 count += 155 if num_bits <= 4:56 return b, count57 num_bits -= 458 break59 # High bit doesn't need stuff bit - accept it, limit try_cnt, retry60 num_bits -= 161 try_cnt //= 262def bitunstuf(sb, num_bits):63 global UNLOOP64 edges = sb ^ (sb >> 1)65 unstuffed_bits = 066 cu = TESTBITS67 cs = num_bits68 while 1:69 UNLOOP += 170 if not cu:71 # Extracted desired bits72 return unstuffed_bits73 if not cs:74 # Need more data75 return -99976 cs -= 177 if (edges >> (cs+1)) & 0xf:78 # Normal data79 cu -= 180 unstuffed_bits |= ((sb >> cs) & 1) << cu81 elif ((edges >> cs) & 0x1f) == 0x00:82 # Six consecutive bits - a bitstuff error83 if (sb >> cs) & 1:84 return -cs85 return -cs86def bitunstuf_batch(sb, num_bits):87 global UNLOOP88 edges = sb ^ (sb >> 1)89 e2 = edges | (edges >> 1)90 e4 = e2 | (e2 >> 2)91 rm_bits = ~e492 unstuffed_bits = 093 cu = TESTBITS94 cs = num_bits95 while 1:96 try_cnt = cu if cu < cs else cs97 while 1:98 UNLOOP += 199 try_mask = ((1 << try_cnt) - 1) << (cs + 1 - try_cnt)100 if not (rm_bits & try_mask):101 # No stuff bits in try_cnt bits - copy into unstuffed_bits102 cu -= try_cnt103 cs -= try_cnt104 unstuffed_bits |= ((sb >> cs) & ((1 << try_cnt) - 1)) << cu105 if not cu:106 # Extracted desired bits107 return unstuffed_bits108 break109 cs -= 1110 if rm_bits & (1 << (cs + 1)):111 # High bit of try_cnt a stuff bit112 if rm_bits & (1 << cs):113 # Six consecutive bits - a bitstuff error114 if (sb >> cs) & 1:115 return -cs116 return -cs117 break118 # High bit not a stuff bit - limit try_cnt and retry119 cu -= 1120 unstuffed_bits |= ((sb >> cs) & 1) << cu121 try_cnt //= 2122 if not cs:123 # Need more data124 return -999125def bitunstuf_batch_pass4(sb, num_bits):126 global UNLOOP127 edges = sb ^ (sb >> 1)128 e2 = edges | (edges >> 1)129 e4 = e2 | (e2 >> 2)130 rm_bits = ~e4131 unstuffed_bits = 0132 cu = TESTBITS133 cs = num_bits134 try_cnt = cu if cu < cs else cs135 while 1:136 UNLOOP += 1137 pass_cnt = try_cnt138 try_mask = ((1 << try_cnt) - 1) << (cs + 1 - try_cnt)139 if rm_bits & try_mask:140 # There is a stuff bit somewhere in try_cnt141 if rm_bits & (1 << cs):142 # High bit is a stuff bit143 cs -= 1144 if rm_bits & (1 << cs):145 # Six consecutive bits - a bitstuff error146 if (sb >> cs) & 1:147 return -cs148 return -cs149 rem_cnt = cu if cu < cs else cs150 pass_cnt = try_cnt = rem_cnt if rem_cnt < 4 else 4151 else:152 # High bit is not a stuff bit - pass 1, limit try_cnt, retry153 pass_cnt = 1154 # Copy pass_cnt bits into unstuffed_bits155 cu -= pass_cnt156 cs -= pass_cnt157 unstuffed_bits |= ((sb >> cs) & ((1 << pass_cnt) - 1)) << cu158 if not cu:159 # Extracted desired bits160 return unstuffed_bits161 if not cs:162 # Need more data163 return -999164 if pass_cnt >= try_cnt:165 try_cnt = cu if cu < cs else cs166 else:167 try_cnt //= 2168def main():169 stuf_func = bitstuf_batch170 unstuf_func = bitunstuf_batch171 for i in range(1<<TESTBITS):172 if not i % (1<<12):173 sys.stdout.write('.')174 sys.stdout.flush()175 val = i | (1 << (TESTBITS+1))176 sv, sc = stuf_func(val, TESTBITS)177 uv = unstuf_func(sv, sc)178 if i != uv:179 report("Mismatch on %d: %s -> %s -> %s (%d)"180 % (i, format(val, '025b'), format(sv, '025b')181 , format(uv, '025b'), sv))182 sys.exit(-1)183 report("Test completed successfully (avg passes: %.3f stuff, %.3f unstuff)"184 % (LOOP / (1<<TESTBITS), UNLOOP / (1<<TESTBITS)))185 # Verify bit stuff errors are detected186 if (unstuf_func(0b1100111100000011, 16) != -2187 or unstuf_func(0b1101111110000000, 16) != -7):188 report("Didn't detect stuff error")189 sys.exit(-1)190if __name__ == '__main__':...

Full Screen

Full Screen

dir_utils.py

Source:dir_utils.py Github

copy

Full Screen

1import os2import sys3from datetime import datetime4ANO = str(datetime.now().year)5def pega_fontes():6 fontes = []7 for drive in pega_letras():8 for dir in os.listdir(drive):9 if os.path.isdir(os.path.join(drive, dir)):10 try:11 caminho = os.path.join(drive, dir)12 if not detecta_mascara(caminho) == []:13 fontes.append(caminho)14 except ValueError:15 continue16 return fontes17def pega_letras():18 drives = []19 if sys.platform == 'win32':20 import win32api21 drives = win32api.GetLogicalDriveStrings()22 drives = [drive[:2] for drive in drives.split('\000')[:-1]]23 return drives24def formata_mascaras(pastas: list) -> str:25 mascaras = []26 for nivel, pasta in enumerate(pastas):27 mascaras.append(mask_of_data(pasta, nivel))28 return '/'.join(mascaras)29def mask_of_data(pasta: str, nivel: int = 0) -> bool:30 masks = ('%Y', '%m', '%d')31 compose_masks = ('%Y%m%d', '%Y%m', '%m%d')32 for try_mask in masks[nivel:]:33 try:34 prefix = ''35 if len(pasta) == 1:36 pasta = '0' + pasta37 prefix = '#'38 datetime.strptime(pasta, try_mask)39 return prefix + try_mask40 except ValueError:41 continue42 for try_mask in compose_masks:43 try:44 datetime.strptime(pasta, try_mask)45 return try_mask46 except ValueError:47 continue48 return None49def detecta(caminho: str, pastas: list):50 lpastas = None51 lista_pastas = os.listdir(os.path.join(caminho, *pastas))52 if len(lista_pastas) == 0:53 print('1', pastas)54 return formata_mascaras(pastas)55 if len(pastas) == 0:56 print('2', lista_pastas)57 lpastas = [pasta for pasta in lista_pastas if pasta[:4] == ANO]58 print('2.1', lpastas)59 if len(lpastas) == 0:60 return pastas61 if lpastas:62 pasta = lpastas[0]63 else:64 pasta = lista_pastas[0]65 print('2.2', pasta)66 if mask_of_data(pasta) is not None:67 pastas.append(pasta)68 print('3', pastas)69 return detecta(caminho, pastas)70 if len(pastas) > 0: # Achou o pote no fim do arco-íris71 return formata_mascaras(pastas)72 raise ValueError('Caminho %s inválido para a função detecta_mascara!!!'73 % caminho)74def detecta_mascara(caminho: str):75 mascaras = []...

Full Screen

Full Screen

gen_trimap.py

Source:gen_trimap.py Github

copy

Full Screen

1#! /usr/bin/python2# -*- coding: utf-8 -*- #3import cv24from PIL import Image, ImageDraw, ImageFont5import numpy as np6def gen_trimap(mask, r = 0.01):7 '''8 generate trimap from segmentation, 9 input:10 mask 0 for bg 1 for fg11 output:12 0, bg13 255, fg14 128, uncertain15 '''16 mask = (mask > 0.5).astype(np.int16)*25517 h, w = mask.shape[:2]18 delta = int(min(h, w) * r) + 3;19 border = get_border(mask);20 kernel = np.ones((delta, delta), np.uint8)21 uncertain = cv2.dilate(border, kernel, iterations=1)22 mask[uncertain > 0] = 12823 return mask.astype(np.uint8)24def get_border(im):25 ddepth = cv2.CV_16S26 dx = cv2.Sobel(im, ddepth, 1, 0)27 dy = cv2.Sobel(im, ddepth, 0, 1)28 dxabs = cv2.convertScaleAbs(dx)29 dyabs = cv2.convertScaleAbs(dy)30 mag = cv2.addWeighted(dxabs, 1, dyabs, 1, 0)31 border = (mag > 0).astype(np.uint8);32 return border33if __name__ == "__main__":34 x = cv2.imread("images/try_mask.png", 0)35 x = (x > 0).astype(np.int16)36 y = get_border(x)37 z = gen_trimap(x)38 cv2.imshow("y", y*255);39 cv2.imshow("z", z);...

Full Screen

Full Screen

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run hypothesis automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful