How to use as_result method in hypothesis

Best Python code snippet using hypothesis

couter.py

Source:couter.py Github

copy

Full Screen

1#!usr/bin/env python2#-*- coding:utf-8 -*-3# author:yangva4# datetime:2017/12/23 0023 21:315import re6def format_string(string): #格式化字符串,把符号格式化7 string = string.replace('++','+')8 string = string.replace('-+','-')9 string = string.replace('--','+')10 string = string.replace('*+','*')11 string = string.replace('/+','/')12 string = string.replace(' ','')13 return string14def counter_md(string): #乘除15 pattern_str2 = '\d+\.?\d*[*/][+\-]?\d+\.?\d*' #匹配乘除法,带上正负号,[]中的 - 有特殊意义,所以要转义16 while re.findall(pattern_str2,string):17 expression = re.search(pattern_str2,string).group()18 #如果有乘法,分割并分别运算19 if expression.count('*'):20 x,y = expression.split('*')21 mul_result = str(float(x)*float(y))22 string = string.replace(expression,mul_result)23 string = format_string(string)24 #如果有除法,分割并分别运算25 if expression.count('/'):26 x,y = expression.split('/')27 div_result = str(float(x)/float(y))28 string = string.replace(expression,div_result)29 string = format_string(string)30 return string31def counter_as(string): #加减32 pattern_add = '[\-]?\d+\.?\d*\+[+\-]?\d+\.?\d*' #匹配加法33 pattern_sub = '[\-]?\d+\.?\d*\-[+\-]?\d+\.?\d*' #匹配减法34 #处理加法35 while re.findall(pattern_add,string):36 add_list = re.findall(pattern_add,string) #将结果分割成一个小式子37 for add_str in add_list: #迭代每个小式子,分别计算38 x,y = add_str.split('+')39 add_result = '+'+str(float(x)+float(y))40 string = string.replace(add_str,add_result) #得到的结果替换到式子中41 #处理减法42 while re.findall(pattern_sub,string):43 sub_list = re.findall(pattern_sub,string)44 for sub_str in sub_list:45 numbers = sub_str.split('-')46 #如果分割出来的小式子里有如-5-3的式子,会分割出['','5','3']则再分割一次47 if len(numbers) == 3:48 result = 0 #定义变量,方便后续存储结果49 for v in numbers:50 if v:51 result -= float(v)52 else: #正常结果,比如4-5,分割得到的则是['4','5']53 x,y = numbers54 result = float(x) - float(y)55 #替换字符串56 string = string.replace(sub_str,str(result))57 return string58def check(string): #检查合法性59 check_flag = True #标志位60 if not string.count('(') == string.count(')'):61 print('括号数量不统一')62 check_flag = False63 if re.findall('[a-zA-Z]+',string):64 check_flag = False65 print('非法字符')66 check_flag = False67 return check_flag68if __name__ == '__main__':69 #info = '20-4+9*((44-22+134/3 - (-3+33+34*5/2*5-9/3*55)-45-3)+55+3*234)'70 # 检验合法性71 info = input('请输入式子:')72 if check(info):73 print('info:',info)74 info = format_string(info)75 print(info)76 print('eval(info):',eval(info)) #作为与输出结果对比的验证77 while info.count('(') > 0: #计算括号内的式子78 pattern_str = re.search('\([^()]*\)',info).group()79 #按照运算优先级,先计算乘除法的结果80 md_result = counter_md(pattern_str)81 #再计算加减法的结果82 as_result = counter_as(md_result)83 #把计算得到的结果作[1:-1]切片,把括号去掉再重新格式化替换原数据替换到式子中84 info = format_string(info.replace(pattern_str,as_result[1:-1]))85 else: #计算括号外的式子,不用再匹配直接运算86 md_result = counter_md(info)87 as_result = counter_as(md_result)88 info = info.replace(info,as_result)...

Full Screen

Full Screen

railway_catch_test.py

Source:railway_catch_test.py Github

copy

Full Screen

...14 result = Railway(download, failed_parse, output).run()15 assert result.is_failure()16 assert isinstance(result.error, RuntimeError)17 assert repr(result.error) == "RuntimeError('Failed to parse')"18@as_result()19def download(result):20 return [{"date": "2022-01-19", "clicks": 13}, {"date": "2022-01-20", "clicks": 37}]21@as_result()22def parse(result):23 return [[entity["date"], entity["clicks"]] for entity in result]24@as_result()25def output(result):26 return {"data": {"rows": result}}27@as_result(RuntimeError)28def failed_download(result):29 raise RuntimeError("Failed to download")30@as_result(RuntimeError)31def failed_parse(result):...

Full Screen

Full Screen

configurable_railway_test.py

Source:configurable_railway_test.py Github

copy

Full Screen

...6 f"Got a <{result.error.__class__.__name__}> with the message '{result.error.args[0]}'"7 )8 result = Railway(failed_download, parse, output).run(failure_handler=handle_error)9 assert result == "Got a <RuntimeError> with the message 'Failed to download'"10@as_result()11def download(result):12 return [13 {"date": "2022-01-19", "clicks": 13},14 {"date": "2022-01-20", "clicks": 37},15 ]16@as_result()17def parse(result):18 return [[entity["date"], entity["clicks"]] for entity in result]19@as_result()20def output(result):21 return {"data": {"rows": result}}22@as_result(RuntimeError)23def failed_download(result):...

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