Best Python code snippet using hypothesis
reghint.py
Source:reghint.py
1from rpython.jit.codewriter.effectinfo import EffectInfo2from rpython.jit.metainterp.resoperation import rop3from rpython.jit.metainterp.history import (Const, ConstInt, ConstPtr,4 ConstFloat, INT, REF, FLOAT, VECTOR, TargetToken, AbstractFailDescr)5from rpython.jit.backend.llsupport.descr import CallDescr6from rpython.jit.backend.x86.regloc import (FrameLoc, RegLoc, ConstFloatLoc,7 FloatImmedLoc, ImmedLoc, imm, imm0, imm1, ecx, eax, edx, ebx, esi, edi,8 ebp, r8, r9, r10, r11, r12, r13, r14, r15, xmm0, xmm1, xmm2, xmm3, xmm4,9 xmm5, xmm6, xmm7, xmm8, xmm9, xmm10, xmm11, xmm12, xmm13, xmm14,10 X86_64_SCRATCH_REG, X86_64_XMM_SCRATCH_REG,)11from rpython.jit.backend.x86 import rx8612from rpython.jit.backend.llsupport.regalloc import (SAVE_DEFAULT_REGS,13 SAVE_GCREF_REGS, SAVE_ALL_REGS)14from rpython.jit.backend.x86.regalloc import (15 X86RegisterManager, X86XMMRegisterManager,16 X86_64_RegisterManager, X86_64_XMMRegisterManager, compute_gc_level17)18from rpython.jit.backend.x86.arch import IS_X86_32, IS_X86_6419# tell the register allocator hints about which variables should be placed in20# what registers (those are just hints, the register allocator will try its21# best to achieve that).22class X86RegisterHints(object):23 def add_hints(self, longevity, inputargs, operations):24 self.longevity = longevity25 for i in range(len(operations)):26 op = operations[i]27 if rop.has_no_side_effect(op.opnum) and op not in self.longevity:28 continue29 oplist[op.getopnum()](self, op, i)30 def not_implemented_op(self, op, position):31 # no hint by default32 pass33 def consider_int_neg(self, op, position):34 self.longevity.try_use_same_register(op.getarg(0), op)35 consider_int_invert = consider_int_neg36 def _consider_binop_part(self, op, position, symm=False):37 x = op.getarg(0)38 y = op.getarg(1)39 # For symmetrical operations, if y won't be used after the current40 # operation finishes, but x will be, then swap the role of 'x' and 'y'41 if symm:42 if isinstance(x, Const):43 x, y = y, x44 elif (not isinstance(y, Const) and45 self.longevity[x].last_usage > position and46 self.longevity[y].last_usage == position):47 x, y = y, x48 if not isinstance(x, Const):49 self.longevity.try_use_same_register(x, op)50 def _consider_binop(self, op, position):51 self._consider_binop_part(op, position)52 def _consider_binop_symm(self, op, position):53 self._consider_binop_part(op, position, symm=True)54 consider_int_mul = _consider_binop_symm55 consider_int_and = _consider_binop_symm56 consider_int_or = _consider_binop_symm57 consider_int_xor = _consider_binop_symm58 consider_int_mul_ovf = _consider_binop_symm59 consider_int_sub_ovf = _consider_binop60 consider_int_add_ovf = _consider_binop_symm61 def consider_int_add(self, op, position):62 y = op.getarg(1)63 if isinstance(y, ConstInt) and rx86.fits_in_32bits(y.value):64 pass # nothing to be hinted65 else:66 self._consider_binop_symm(op, position)67 consider_nursery_ptr_increment = consider_int_add68 def consider_int_sub(self, op, position):69 y = op.getarg(1)70 if isinstance(y, ConstInt) and rx86.fits_in_32bits(-y.value):71 pass # nothing to be hinted72 else:73 self._consider_binop(op, position)74 def _consider_float_op(self, op, position):75 x = op.getarg(0)76 if not isinstance(x, Const):77 self.longevity.try_use_same_register(x, op)78 consider_float_add = _consider_float_op79 consider_float_sub = _consider_float_op80 consider_float_mul = _consider_float_op81 consider_float_truediv = _consider_float_op82 consider_float_neg = _consider_float_op83 consider_float_abs = _consider_float_op84 def consider_int_lshift(self, op, position):85 x, y = op.getarg(0), op.getarg(1)86 if not isinstance(y, Const):87 self.longevity.fixed_register(position, ecx, y)88 if not isinstance(x, Const):89 self.longevity.try_use_same_register(x, op)90 consider_int_rshift = consider_int_lshift91 consider_uint_rshift = consider_int_lshift92 def consider_uint_mul_high(self, op, position):93 # could do a lot more, but I suspect not worth it94 # just block eax and edx95 self.longevity.fixed_register(position, eax)96 self.longevity.fixed_register(position, edx)97 def consider_call_malloc_nursery(self, op, position):98 self.longevity.fixed_register(position, ecx, op)99 self.longevity.fixed_register(position, edx)100 consider_call_malloc_nursery_varsize = consider_call_malloc_nursery101 consider_call_malloc_nursery_varsize_frame = consider_call_malloc_nursery102 def _consider_call(self, op, position, guard_not_forced=False, first_arg_index=1):103 calldescr = op.getdescr()104 assert isinstance(calldescr, CallDescr)105 gc_level = compute_gc_level(calldescr, guard_not_forced)106 args = op.getarglist()[first_arg_index:]107 argtypes = calldescr.get_arg_types()108 CallHints64(self.longevity).hint(position, args, argtypes, gc_level)109 def _consider_real_call(self, op, position):110 effectinfo = op.getdescr().get_extra_info()111 assert effectinfo is not None112 oopspecindex = effectinfo.oopspecindex113 if oopspecindex != EffectInfo.OS_NONE:114 # XXX safe default: do nothing115 return116 self._consider_call(op, position)117 consider_call_i = _consider_real_call118 consider_call_r = _consider_real_call119 consider_call_f = _consider_real_call120 consider_call_n = _consider_real_call121 def _consider_call_may_force(self, op, position):122 self._consider_call(op, position, guard_not_forced=True)123 consider_call_may_force_i = _consider_call_may_force124 consider_call_may_force_r = _consider_call_may_force125 consider_call_may_force_f = _consider_call_may_force126 consider_call_may_force_n = _consider_call_may_force127 def _consider_call_release_gil(self, op, position):128 # [Const(save_err), func_addr, args...]129 self._consider_call(op, position, guard_not_forced=True, first_arg_index=2)130 consider_call_release_gil_i = _consider_call_release_gil131 consider_call_release_gil_f = _consider_call_release_gil132 consider_call_release_gil_n = _consider_call_release_gil133oplist = [X86RegisterHints.not_implemented_op] * rop._LAST134for name, value in X86RegisterHints.__dict__.iteritems():135 if name.startswith('consider_'):136 name = name[len('consider_'):]137 num = getattr(rop, name.upper())138 oplist[num] = value139class CallHints32(object):140 RegisterManager = X86RegisterManager141 XMMRegisterManager = X86XMMRegisterManager142 def _block_non_caller_save(self, position, save_all_regs, hinted_gpr=None, hinted_xmm=None):143 if hinted_gpr is None:144 hinted_gpr = []145 if hinted_xmm is None:146 hinted_xmm = []147 # block all remaining registers that are not caller save148 # XXX the case save_all_regs == SAVE_GCREF_REGS149 # (save callee-save regs + gc ptrs) is no expressible atm150 if save_all_regs == SAVE_ALL_REGS:151 regs = self.RegisterManager.all_regs152 else:153 regs = self.RegisterManager.save_around_call_regs154 for reg in regs:155 if reg not in hinted_gpr:156 self.longevity.fixed_register(position, reg)157 for reg in self.XMMRegisterManager.all_regs:158 if reg not in hinted_xmm:159 self.longevity.fixed_register(position, reg)160 def hint(self, position, args, argtypes, save_all_regs):161 self._block_non_caller_save(position, save_all_regs)162class CallHints64(CallHints32):163 ARGUMENTS_GPR = [edi, esi, edx, ecx, r8, r9]164 ARGUMENTS_XMM = [xmm0, xmm1, xmm2, xmm3, xmm4, xmm5, xmm6, xmm7]165 RegisterManager = X86_64_RegisterManager166 XMMRegisterManager = X86_64_XMMRegisterManager167 def __init__(self, longevity):168 self.longevity = longevity169 def hint(self, position, args, argtypes, save_all_regs):170 hinted_xmm = []171 hinted_gpr = []172 hinted_args = []173 next_arg_gpr = 0174 next_arg_xmm = 0175 for i in range(len(args)):176 arg = args[i]177 if arg.type == "f" or (i < len(argtypes) and argtypes[i] == 'S'):178 if next_arg_xmm < len(self.ARGUMENTS_XMM):179 tgt = self.ARGUMENTS_XMM[next_arg_xmm]180 if not arg.is_constant() and arg not in hinted_args:181 self.longevity.fixed_register(position, tgt, arg)182 hinted_xmm.append(tgt)183 hinted_args.append(arg)184 next_arg_xmm += 1185 else:186 if next_arg_gpr < len(self.ARGUMENTS_GPR):187 tgt = self.ARGUMENTS_GPR[next_arg_gpr]188 if not arg.is_constant() and arg not in hinted_args:189 self.longevity.fixed_register(position, tgt, arg)190 hinted_gpr.append(tgt)191 hinted_args.append(arg)192 next_arg_gpr += 1193 self._block_non_caller_save(position, save_all_regs, hinted_gpr, hinted_xmm)194if IS_X86_32:195 CallHints = CallHints32196if IS_X86_64:...
generate.py
Source:generate.py
1import os, sys, random, datetime2import argparse, json3from faker import Faker4from utils import generate_date, generate_number, generate_string5def get_cli_args():6 parser = argparse.ArgumentParser(description='Generates random data')7 parser.add_argument('input_file', type=str, help='input data file')8 return parser.parse_args()9def check_missing_keys(*expected_keys, provided_keys):10 for key in expected_keys:11 if key not in provided_keys:12 return True, key13 return False, None14def validate_input_data(input):15 # check for missing keys16 R, missing_key = check_missing_keys('output file', 'delimiter', 'cols', 'sample size', provided_keys=data.keys())17 if R:18 print(f'Missing {missing_key} in file')19 sys.exit(1)20 filename = input['output file']21 delimiter = input['delimiter']22 try:23 rows = int(input['sample size'])24 except ValueError:25 print(f"unable to parse value to integer {input['sample size']}")26 sys.exit(1)27 column_data_list = input['cols']28 for column_data in column_data_list:29 # check for missing keys30 cR, c_missing_key = check_missing_keys('name', 'type', provided_keys=column_data.keys())31 if cR:32 print(f'Missing {c_missing_key} in cols section of the file')33 sys.exit(1)34 return filename, delimiter, rows, column_data_list35def generate_output(filename, delimiter, rows, column_data_list):36 # print(filename, delimiter, rows)37 ff = Faker()38 Faker.seed(0)39 # print header40 header = []41 for item in column_data_list:42 header.append(item['name'])43 data_list = []44 for i in range(rows):45 row_data = []46 for item in column_data_list:47 if 'consider only' in item:48 consider_only = item['consider only']49 data = random.choice(consider_only)50 else:51 consider = item['consider'] if 'consider' in item else []52 rand_x = random.choice([0, 1])53 # generate name54 if item['type'] == 'f_name':55 data = (ff.name() if rand_x == 0 else random.choice(consider)) if len(consider) > 0 else ff.name()56 elif item['type'] == 'f_date':57 data = (generate_date(item) if rand_x == 0 else random.choice(consider)) if len(consider) > 0 else generate_date(item)58 elif item['type'] == 'f_address':59 data = (ff.address() if rand_x == 0 else random.choice(consider)) if len(consider) > 0 else ff.address()60 elif item['type'] == 'f_phonenumber':61 data = (ff.phone_number() if rand_x == 0 else random.choice(consider)) if len(consider) > 0 else ff.phone_number()62 elif item['type'] == 'f_job':63 data = (ff.job() if rand_x == 0 else random.choice(consider)) if len(consider) > 0 else ff.job()64 elif item['type'] == 'f_number':65 data = (generate_number(item) if rand_x == 0 else random.choice(consider)) if len(consider) > 0 else generate_number(item)66 elif item['type'] == 'f_str':67 data = (generate_string(item) if rand_x == 0 else random.choice(consider)) if len(consider) > 0 else generate_string(item)68 elif item['type'] == 'f_ssn':69 data = (ff.ssn() if rand_x == 0 else random.choice(consider)) if len(consider) > 0 else ff.ssn()70 elif item['type'] == 'f_cc_num':71 data = (ff.credit_card_number() if rand_x == 0 else random.choice(consider)) if len(consider) > 0 else ff.credit_card_number()72 elif item['type'] == 'f_company':73 data = (ff.company() if rand_x == 0 else random.choice(consider)) if len(consider) > 0 else ff.company()74 elif item['type'] == 'f_email':75 data = (ff.ascii_email() if rand_x == 0 else random.choice(consider)) if len(consider) > 0 else ff.ascii_email()76 else:77 data = (generate_string(item) if rand_x == 0 else random.choice(consider)) if len(consider) > 0 else generate_string(item)78 if data:79 data = data.replace('\n', ' ').replace('\r', ' ').replace(',', ' ')80 row_data.append(data)81 data_list.append(row_data)82 lines = ''83 with open(filename, 'w') as f:84 # write header85 f.write(delimiter.join(header) + '\n')86 # write rows87 for row in data_list:88 f.write(delimiter.join(row) + '\n')89 print(f'{filename} was generated...')90if __name__ == '__main__':91 args = get_cli_args()92 try:93 with open(args.input_file) as f:94 data = json.load(f)95 generate_output(*validate_input_data(input=data))96 except FileNotFoundError:...
toanlop3.py
Source:toanlop3.py
1#!/usr/bin/env python32import time3def solve():4 consider_1 = []5 for b in range(1, 10):6 for c in range(1, 10):7 for g in range(1, 10):8 for h in range(1, 10):9 for i in range(1, 10):10 for e in range(1, 10):11 cal_1 = 87 - ((13 * b) / c +12 (g * h) / i + 12 * e)13 if cal_1 % 1 == 0 and -9 <= cal_1 <= 17:14 consider_1.append(cal_1)15 consider_1_true = []16 result = 017 for consider in consider_1:18 if consider not in consider_1_true:19 count_consider = 020 count_consider_2 = 021 for a in range(1, 10):22 for d in range(1, 10):23 for f in range(1, 10):24 if a + d - f - consider == 0:25 count_consider_2 = count_consider_2 + 126 if consider not in consider_1_true:27 consider_1_true.append(consider)28 count_consider = consider_1.count(consider)29 if count_consider_2 is not 0:30 result = result + (count_consider_2 * count_consider)31 return result32def main():33 start = time.time()34 print(solve())35 print('Time =', time.time() - start)36if __name__ == "__main__":...
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!!