Best Python code snippet using rester_python
test_validation.py
Source:test_validation.py
...5from ..calculator import Calculator6def test_validation_is_number_01():7 calculator = Calculator()8 rpn_input = "1"9 assert calculator.is_number(rpn_input)10def test_validation_is_number_02():11 calculator = Calculator()12 rpn_input = "1.0"13 assert calculator.is_number(rpn_input)14def test_validation_is_number_03_00():15 calculator = Calculator()16 rpn_input = "0"17 assert calculator.is_number(rpn_input)18def test_validation_is_number_03_01():19 calculator = Calculator()20 rpn_input = "10"21 assert calculator.is_number(rpn_input)22def test_validation_is_number_04():23 calculator = Calculator()24 rpn_input = "01"25 assert calculator.is_number(rpn_input) is False26def test_validation_is_number_05():27 calculator = Calculator()28 rpn_input = "01."29 assert calculator.is_number(rpn_input) is False30def test_validation_is_number_06():31 calculator = Calculator()32 rpn_input = "."33 assert calculator.is_number(rpn_input) is False34def test_validation_is_number_07():35 calculator = Calculator()36 rpn_input = "1.1343243240"37 assert calculator.is_number(rpn_input)38def test_validation_is_number_08():39 calculator = Calculator()40 rpn_input = "1.134324324.0"41 assert calculator.is_number(rpn_input) is False42def test_validation_is_number_09():43 calculator = Calculator()44 rpn_input = "1.134324324.0."45 assert calculator.is_number(rpn_input) is False46def test_validation_is_number_10():47 calculator = Calculator()48 rpn_input = "0x"49 assert calculator.is_number(rpn_input) is False50def test_validation_is_number_11():51 calculator = Calculator()52 rpn_input = "0x0"53 assert calculator.is_number(rpn_input)54def test_validation_is_number_12():55 calculator = Calculator()56 rpn_input = "0xa"57 assert calculator.is_number(rpn_input)58def test_validation_is_number_13():59 calculator = Calculator()60 rpn_input = "0xfaa"61 assert calculator.is_number(rpn_input)62def test_validation_is_number_14():63 calculator = Calculator()64 rpn_input = "0xfr"65 assert calculator.is_number(rpn_input) is False66def test_validation_is_number_15():67 calculator = Calculator()68 rpn_input = "x"69 assert calculator.is_number(rpn_input) is False70def test_validation_is_number_16():71 calculator = Calculator()72 rpn_input = "0b0"73 assert calculator.is_number(rpn_input)74def test_validation_is_number_17():75 calculator = Calculator()76 rpn_input = "1"77 assert calculator.is_number(rpn_input)78def test_validation_is_number_18():79 calculator = Calculator()80 rpn_input = "0b01"81 assert calculator.is_number(rpn_input)82def test_validation_is_number_19():83 calculator = Calculator()84 rpn_input = "10"85 assert calculator.is_number(rpn_input)86def test_validation_is_number_20():87 calculator = Calculator()88 rpn_input = "0b1010111"89 assert calculator.is_number(rpn_input)90def test_validation_is_number_21():91 calculator = Calculator()92 rpn_input = "2b"93 assert calculator.is_number(rpn_input) is False94def test_validation_is_number_22():95 calculator = Calculator()96 rpn_input = "224141w412"97 assert calculator.is_number(rpn_input) is False98def test_validation_is_number_23():99 calculator = Calculator()100 rpn_input = "224141wgaasfas412"101 assert calculator.is_number(rpn_input) is False102def test_validation_is_number_24():103 calculator = Calculator()104 rpn_input = "0000000000000000000000000002"105 assert calculator.is_number(rpn_input) is False106def test_validation_is_number_25():107 calculator = Calculator()108 rpn_input = "0o0"109 assert calculator.is_number(rpn_input)110def test_validation_is_number_26():111 calculator = Calculator()112 rpn_input = "0o01"113 assert calculator.is_number(rpn_input)114def test_validation_is_number_27():115 calculator = Calculator()116 rpn_input = "0o012312312"117 assert calculator.is_number(rpn_input)118def test_validation_is_number_28():119 calculator = Calculator()120 rpn_input = "0o12412412"121 assert calculator.is_number(rpn_input)122def test_validation_is_number_29():123 calculator = Calculator()124 rpn_input = "0o8"125 assert calculator.is_number(rpn_input) is False126def test_validation_is_number_30():127 calculator = Calculator()128 rpn_input = "0oa"129 assert calculator.is_number(rpn_input) is False130def test_validation_is_number_31():131 calculator = Calculator()132 rpn_input = "0o"133 assert calculator.is_number(rpn_input) is False134def test_validation_is_number_32():135 calculator = Calculator()136 rpn_input = "0o153165137"137 assert calculator.is_number(rpn_input)138def test_validation_is_number_33():139 calculator = Calculator()140 rpn_input = "a"141 assert calculator.is_number(rpn_input) is False142def test_validation_is_number_34():143 calculator = Calculator()144 rpn_input = "aadasdaewe"145 assert calculator.is_number(rpn_input) is False146def test_validation_is_operator_00():147 calculator = Calculator()148 rpn_input = "+"149 assert calculator.is_operator(rpn_input)150def test_validation_is_constant_00():151 calculator = Calculator()152 rpn_input = "pi"153 assert calculator.is_constant(rpn_input)154def test_validation_is_constant_01():155 calculator = Calculator()156 rpn_input = "e"157 assert calculator.is_constant(rpn_input)158def test_validation_is_constant_02():159 calculator = Calculator()...
xx_0301_判断字符串是否为数字.py
Source:xx_0301_判断字符串是否为数字.py
1# -*- coding: utf-8 -*-2# __author__ = 'lenovo'3# 以ä¸å®ä¾éè¿å建èªå®ä¹å½æ° is_number() æ¹æ³æ¥å¤æå符串æ¯å¦ä¸ºæ°åï¼4# def is_number(s):5# try:6# float(s)7# return True8# except ValueError:9# pass10#11# try:12# import unicodedata13# unicodedata.numeric(s)14# return True15# except(TypeError, ValueError):16# pass17#18# return False19#20# # æµè¯å符串åæ°å21# print(is_number('foo')) # False22# print(is_number('1')) # True23# print(is_number('1.3')) # True24# print(is_number('-1.37')) # True25# print(is_number('1e3')) # True26#27# # æµè¯ Unicode28# # é¿æä¼¯è¯ 529# print(is_number('Ù¥')) # True30# # æ³°è¯ 231# print(is_number('à¹')) # True32# # ä¸ææ°å33# print(is_number('å')) # True34# # çæå·35# print(is_number('©')) # False36# æ们ä¹å¯ä»¥ä½¿ç¨å
åµ if è¯å¥æ¥å®ç°ï¼37# Python isdigit() æ¹æ³æ£æµå符串æ¯å¦åªç±æ°åç»æã38# Python isnumeric() æ¹æ³æ£æµå符串æ¯å¦åªç±æ°åç»æãè¿ç§æ¹æ³æ¯åªé对unicode对象ã39# æç¨ä»£ç å½åºç°å¤ä¸ªæ±åæ°åæ¶ä¼æ¥éï¼éè¿éåå符串解å³40# 对æ±å表示çæ°åä¹å¯å辨41# def is_number(s):42# try: # å¦æè½è¿è¡float(s)è¯å¥ï¼è¿åTrueï¼å符串sæ¯æµ®ç¹æ°ï¼43# float(s)44# return True45# except ValueError: # ValueError为Pythonçä¸ç§æ åå¼å¸¸ï¼è¡¨ç¤º"ä¼ å
¥æ æçåæ°"46# pass # å¦æå¼åäºValueErrorè¿ç§å¼å¸¸ï¼ä¸åä»»ä½äºæ
ï¼passï¼ä¸åä»»ä½äºæ
ï¼ä¸è¬ç¨åå ä½è¯å¥ï¼47# try:48# import unicodedata # å¤çASCiiç çå
49# for i in s:50# unicodedata.numeric(i) # æä¸ä¸ªè¡¨ç¤ºæ°åçå符串转æ¢ä¸ºæµ®ç¹æ°è¿åçå½æ°51# return True52# except (TypeError, ValueError):53# pass54# return False55# æµè¯å符串åæ°å56# print(is_number('foo')) # False57# print(is_number('1')) # True58# print(is_number('1.3')) # True59# print(is_number('-1.37')) # True60# print(is_number('1e3')) # True61#62# # æµè¯ Unicode63# # é¿æä¼¯è¯ 564# print(is_number('Ù¥')) # True65# # æ³°è¯ 266# print(is_number('à¹')) # True67# # ä¸ææ°å68# print(is_number('å')) # True69# # çæå·70# print(is_number('©')) # False71# è¿ä¸æ¥æ©å±å°å
¨è§æ°å72def is_number(s):73 try:74 float(s)75 return True76 except ValueError:77 pass78 import unicodedata79 try:80 unicodedata.numeric(s)81 return True82 except (TypeError, ValueError):83 pass84 if len(s) < 2:85 return False86 try:87 d = 088 if s.startswith('-'):89 s = s[1:]90 for c in s:91 if c == 'ââ': # å
¨è§åå·92 return False93 if c =='.': # å
¨è§ç¹å·94 if d > 0:95 return False96 else:97 d = 198 continue99 return True100 except (TypeError, ValueError):101 pass102 return False103# æµè¯å符串åæ°å104print(f'{is_number("foo")}')105print(f'{is_number("1") }')106print(f'{is_number("1.3") }')107print(f'{is_number("-1.37") }')108print(f'{is_number("1e3") }')109print(f'{is_number("2.345.6") }')110print(f'{is_number("-5.2-8") }')111print(f'{is_number("52-8") }')112print(f'{is_number("-.5") }')113print(f'{is_number("-5.") }')114print(f'{is_number(".5") }')115# æµè¯Unicode116# é¿æä¼¯è¯ 5117print(f'{is_number("Ù¥") }')118# æ³°è¯ 2119print(f'{is_number("à¹") }')120# ä¸ææ°å121print(f'{is_number("å") }')122print(f'{is_number("åå
") }')123# å
¨è§æ°å124print(f'{is_number("ï¼ï¼ï¼") }')125print(f'{is_number("-ï¼ï¼ï¼") }')126print(f'{is_number("ï¼ï¼ï¼ï¼") }')127print(f'{is_number("ï¼ï¼ï¼ï¼") }')128print(f'{is_number("ï¼ï¼ï¼ï¼") }')129print(f'{is_number("ï¼.ï¼ï¼") }')130print(f'{is_number("ï¼ï¼ï¼ï¼") }')131print(f'{is_number("ï¼ï¼ï¼") }')132print(f'{is_number("ï¼ï¼ï¼ï¼") }')133print(f'{is_number("ï¼ï¼23") }')134print(f'{is_number("ï¼ï¼ï¼ï¼ï¼") }')135# çæå·...
test_is_number.py
Source:test_is_number.py
...3class IsNumberTestCase(TestCase):4 def test_cannot_handle_non_string_objects(self):5 with self.assertRaises(TypeError) as raised:6 # noinspection PyTypeChecker7 is_number(None)8 self.assertEqual(str(raised.exception), 'Expected "str", received "NoneType"')9 with self.assertRaises(TypeError) as raised:10 # noinspection PyTypeChecker11 is_number(False)12 self.assertEqual(str(raised.exception), 'Expected "str", received "bool"')13 with self.assertRaises(TypeError) as raised:14 # noinspection PyTypeChecker15 is_number(0)16 self.assertEqual(str(raised.exception), 'Expected "str", received "int"')17 with self.assertRaises(TypeError) as raised:18 # noinspection PyTypeChecker19 is_number([])20 self.assertEqual(str(raised.exception), 'Expected "str", received "list"')21 with self.assertRaises(TypeError) as raised:22 # noinspection PyTypeChecker23 is_number({'a': 1})24 self.assertEqual(str(raised.exception), 'Expected "str", received "dict"')25 def test_returns_false_if_string_is_empty(self):26 self.assertFalse(is_number(''))27 self.assertFalse(is_number(' '))28 def test_returns_false_if_string_contains_number_but_has_spaces(self):29 self.assertFalse(is_number(' 1'))30 self.assertFalse(is_number('99 '))31 self.assertFalse(is_number(' 1234 '))32 self.assertFalse(is_number(' +1234567890'))33 self.assertFalse(is_number(' 1.2 '))34 def test_returns_false_if_string_is_sign_only(self):35 self.assertFalse(is_number('+'))36 self.assertFalse(is_number('-'))37 def test_returns_false_if_contains_operations(self):38 self.assertFalse(is_number('1 + 1'))39 self.assertFalse(is_number('1+1'))40 self.assertFalse(is_number('1 - 1'))41 self.assertFalse(is_number('1-1'))42 def test_returns_true_for_unsigned_integers(self):43 self.assertTrue(is_number('1'))44 self.assertTrue(is_number('99'))45 self.assertTrue(is_number('1234567890'))46 def test_returns_true_for_signed_integers(self):47 self.assertTrue(is_number('+1'))48 self.assertTrue(is_number('+99'))49 self.assertTrue(is_number('+1234567890'))50 self.assertTrue(is_number('-1'))51 self.assertTrue(is_number('-99'))52 self.assertTrue(is_number('-1234567890'))53 def test_returns_true_for_unsigned_double(self):54 self.assertTrue(is_number('1.0'))55 self.assertTrue(is_number('.007'))56 self.assertTrue(is_number('1.000'))57 self.assertTrue(is_number('99.99'))58 self.assertTrue(is_number('1234567890.000123456'))59 def test_returns_true_for_signed_double(self):60 self.assertTrue(is_number('+1.0'))61 self.assertTrue(is_number('+.007'))62 self.assertTrue(is_number('+1.000'))63 self.assertTrue(is_number('+99.99'))64 self.assertTrue(is_number('+1234567890.000123456'))65 self.assertTrue(is_number('-1.0'))66 self.assertTrue(is_number('-.007'))67 self.assertTrue(is_number('-1.000'))68 self.assertTrue(is_number('-99.99'))69 self.assertTrue(is_number('-1234567890.000123456'))70 def test_double_cannot_contain_multiple_dots(self):71 self.assertFalse(is_number('+1..0'))72 self.assertFalse(is_number('+..007'))73 self.assertFalse(is_number('+1..000'))74 self.assertFalse(is_number('+99..99'))75 self.assertFalse(is_number('+1234567890..000123456'))76 self.assertFalse(is_number('-1..0'))77 self.assertFalse(is_number('-..007'))78 self.assertFalse(is_number('-1..000'))79 self.assertFalse(is_number('-99..99'))80 self.assertFalse(is_number('-1234567890..000123456'))81 def test_number_cannot_contain_multiple_sign(self):82 self.assertFalse(is_number('+-1'))83 self.assertFalse(is_number('++1'))84 self.assertFalse(is_number('--1'))85 self.assertFalse(is_number('+-1.1'))86 self.assertFalse(is_number('++1.1'))87 self.assertFalse(is_number('--1.1'))88 def test_returns_true_for_scientific_notation(self):89 self.assertTrue(is_number('1e3'))90 self.assertTrue(is_number('50e2'))...
bintree0.py
Source:bintree0.py
...78 else:79 raise ValueError("Unknown operator:", op)808182def is_number(x):83 return (isinstance(x, int) or isinstance(x, float) or84 isinstance(x, complex))858687def eval_sum(a, b):88 if is_number(a) and is_number(b):89 return a + b90 if is_number(a) and a == 0:91 return b92 if is_number(b) and b == 0:93 return a94 return make_sum(a, b)959697def eval_diff(a, b):98 if is_number(a) and is_number(b):99 return a - b100 if is_number(a) and a == 0:101 return -b102 if is_number(b) and b == 0:103 return a104 return make_diff(a, b)105106107def eval_prod(a, b):108 if is_number(a) and is_number(b):109 return a * b110 if is_number(a) and a == 0 or is_number(b) and b == 0:111 return 0112 if is_number(a) and a == 1:113 return b114 if is_number(b) and b == 1:115 return a116 return make_prod(a, b)117118119def eval_div(a, b):120 if is_number(a) and is_number(b):121 return a / b122 if is_number(a) and a == 0:123 return 0124 if is_number(b) and b == 0:125 raise ZeroDivisionError126 if is_number(b) and b == 1:127 return a128 return make_div(a, b)129 130 131if __name__ == '__main__':132## t1 = btree(2, btree(4, [], []), btree(8, [], []))133## print(t1)134## set_leftch(leftch(t1), btree(5, [], []))135## print(t1)136137 e1 = make_prod(make_sum(2, 3), make_diff(4, 5))138 e2 = make_prod(make_diff(make_prod(2, 'a'), 3), make_diff(4, 5))139 e3 = make_div(make_sum(make_prod(2, 7), make_div(0, 'b')), make_div('a', 1))140
...
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!!