Best Python code snippet using nose
stickytape_test.py
Source:stickytape_test.py
1import os2import os.path3import tempfile4import contextlib5import platform6import re7import subprocess8import shutil9import sys10from nose.tools import istest, nottest, assert_equal11import spur12import stickytape13from test_scripts import root as test_script_root14@istest15def single_file_script_still_works():16 test_script_output(17 script_path="single_file/hello",18 expected_output=b"Hello\n"19 )20@istest21def stdlib_imports_are_not_modified():22 test_script_output(23 script_path="single_file_using_stdlib/hello",24 expected_output=b"f7ff9e8b7bb2e09b70935a5d785e0cc5d9d0abf0\n"25 )26@istest27def stdlib_module_in_package_is_not_generated():28 test_script_output(29 script_path="script_using_stdlib_module_in_package/hello",30 expected_output=b"xml.etree.ElementTree\nHello\n",31 expected_modules=["greeting"],32 python_binary=sys.executable,33 )34@istest35def script_that_imports_local_module_is_converted_to_single_file():36 test_script_output(37 script_path="script_with_single_local_import/hello",38 expected_output=b"Hello\n"39 )40@istest41def script_that_imports_local_package_is_converted_to_single_file():42 test_script_output(43 script_path="script_with_single_local_import_of_package/hello",44 expected_output=b"Hello\n"45 )46@istest47def can_import_module_from_package():48 test_script_output(49 script_path="script_using_module_in_package/hello",50 expected_output=b"Hello\n"51 )52@istest53def can_import_value_from_module_using_from_import_syntax():54 test_script_output(55 script_path="script_with_single_local_from_import/hello",56 expected_output=b"Hello\n"57 )58@istest59def can_import_multiple_values_from_module_using_from_import_syntax():60 test_script_output(61 script_path="script_using_from_to_import_multiple_values/hello",62 expected_output=b"Hello\n"63 )64@istest65def can_import_module_from_package_using_from_import_syntax():66 test_script_output(67 script_path="script_using_from_to_import_module/hello",68 expected_output=b"Hello\n"69 )70@istest71def can_import_multiple_modules_from_module_using_from_import_syntax():72 test_script_output(73 script_path="script_using_from_to_import_multiple_modules/hello",74 expected_output=b"Hello\n"75 )76@istest77def imported_modules_are_transformed():78 test_script_output(79 script_path="imports_in_imported_modules/hello",80 expected_output=b"Hello\n"81 )82@istest83def circular_references_dont_cause_stack_overflow():84 test_script_output(85 script_path="circular_reference/hello",86 expected_output=b"Hello\n"87 )88@istest89def explicit_relative_imports_with_single_dot_are_resolved_correctly():90 test_script_output(91 script_path="explicit_relative_import_single_dot/hello",92 expected_output=b"Hello\n"93 )94@istest95def explicit_relative_imports_with_single_dot_in_package_init_are_resolved_correctly():96 test_script_output(97 script_path="explicit_relative_import_single_dot_in_init/hello",98 expected_output=b"Hello\n"99 )100@istest101def explicit_relative_imports_from_parent_package_are_resolved_correctly():102 test_script_output(103 script_path="explicit_relative_import_from_parent_package/hello",104 expected_output=b"Hello\n"105 )106@istest107def explicit_relative_imports_with_module_name_are_resolved_correctly():108 test_script_output(109 script_path="explicit_relative_import/hello",110 expected_output=b"Hello\n"111 )112@istest113def explicit_relative_imports_with_module_name_in_package_init_are_resolved_correctly():114 test_script_output(115 script_path="explicit_relative_import_in_init/hello",116 expected_output=b"Hello\n"117 )118@istest119def package_init_can_be_used_even_if_not_imported_explicitly():120 test_script_output(121 script_path="implicit_init_import/hello",122 expected_output=b"Hello\n"123 )124@istest125def value_import_is_detected_when_import_is_renamed():126 test_script_output(127 script_path="import_from_as_value/hello",128 expected_output=b"Hello\n"129 )130@istest131def module_import_is_detected_when_import_is_renamed():132 test_script_output(133 script_path="import_from_as_module/hello",134 expected_output=b"Hello\n"135 )136@istest137def can_explicitly_set_python_interpreter():138 with _temporary_directory() as temp_path:139 venv_path = os.path.join(temp_path, "venv")140 _shell.run(["virtualenv", venv_path])141 site_packages_path = _find_site_packages(venv_path)142 path_path = os.path.join(site_packages_path, "greetings.pth")143 with open(path_path, "w") as path_file:144 path_file.write(find_script("python_path_from_binary/packages\n"))145 test_script_output(146 script_path="python_path_from_binary/hello",147 expected_output=b"Hello\n",148 python_binary=_venv_python_binary_path(venv_path),149 )150@istest151def python_environment_variables_are_ignored_when_explicitly_setting_python_interpreter():152 with _temporary_directory() as temp_path:153 venv_path = os.path.join(temp_path, "venv")154 _shell.run(["virtualenv", venv_path])155 site_packages_path = _find_site_packages(venv_path)156 path_path = os.path.join(site_packages_path, "greetings.pth")157 bad_python_path = os.path.join(venv_path, "pythonpath")158 bad_import_path = os.path.join(bad_python_path, "greetings/greeting.py")159 os.makedirs(os.path.dirname(bad_import_path))160 with open(bad_import_path, "w") as bad_import_path:161 pass162 with open(path_path, "w") as path_file:163 path_file.write(find_script("python_path_from_binary/packages\n"))164 original_python_path = os.environ.get("PYTHONPATH")165 os.environ["PYTHONPATH"] = bad_python_path166 try:167 test_script_output(168 script_path="python_path_from_binary/hello",169 expected_output=b"Hello\n",170 python_binary=_venv_python_binary_path(venv_path),171 )172 finally:173 if original_python_path is None:174 del os.environ["PYTHONPATH"]175 else:176 os.environ["PYTHONPATH"] = original_python_path177@istest178def can_explicitly_copy_shebang():179 test_script_output(180 script_path="script_with_special_shebang/hello",181 expected_output=b"1\n",182 copy_shebang=True,183 )184@istest185def modules_with_triple_quotes_can_be_bundled():186 test_script_output(187 script_path="module_with_triple_quotes/hello",188 expected_output=b"Hello\n'''\n\"\"\"\n"189 )190@istest191def additional_python_modules_can_be_explicitly_included():192 test_script_output(193 script_path="script_with_dynamic_import/hello",194 expected_output=b"Hello\n",195 add_python_modules=("greeting", ),196 )197def _find_site_packages(root):198 paths = []199 for dir_path, dir_names, file_names in os.walk(root):200 for dir_name in dir_names:201 path = os.path.join(dir_path, dir_name)202 if dir_name == "site-packages" and os.listdir(path):203 paths.append(path)204 if len(paths) == 1:205 return paths[0]206 else:207 raise ValueError("Multiple site-packages found: {}".format(paths))208_shell = spur.LocalShell()209@nottest210def test_script_output(script_path, expected_output, expected_modules=None, **kwargs):211 result = stickytape.script(find_script(script_path), **kwargs)212 if expected_modules is not None:213 actual_modules = set(re.findall(r"__stickytape_write_module\('([^']*)\.py'", result))214 assert_equal(set(expected_modules), actual_modules)215 with _temporary_script(result) as script_file_path:216 try:217 if _is_windows():218 command = ["py", script_file_path]219 else:220 command = [script_file_path]221 output = _shell.run(command).output.replace(b"\r\n", b"\n")222 except:223 for index, line in enumerate(result.splitlines()):224 print((index + 1), line)225 raise226 assert_equal(expected_output, output)227def find_script(path):228 return os.path.join(test_script_root, path)229@contextlib.contextmanager230def _temporary_script(contents):231 with _temporary_directory() as dir_path:232 path = os.path.join(dir_path, "script")233 with open(path, "w") as script_file:234 script_file.write(contents)235 _shell.run(["chmod", "+x", path])236 yield path237@contextlib.contextmanager238def _temporary_directory():239 dir_path = tempfile.mkdtemp()240 try:241 yield dir_path242 finally:243 shutil.rmtree(dir_path)244def _venv_python_binary_path(venv_path):245 if _is_windows():246 bin_directory = "Scripts"247 else:248 bin_directory = "bin"249 return os.path.join(venv_path, bin_directory, "python")250def _is_windows():...
test_instructions.py
Source:test_instructions.py
1from nose.tools import istest, eq_2from instructions import Instruction3@istest4def add_03_2():5 instruction = Instruction.decode(b'\x03\xc2', 0)6 eq_(str(instruction), 'add ax, dx')7 eq_(len(instruction), 2)8@istest9def add_03_4():10 instruction = Instruction.decode(b'\x03\x06\x56\x43', 0)11 eq_(str(instruction), 'add ax, 4356h')12 eq_(len(instruction), 4)13@istest14def add_05():15 instruction = Instruction.decode(b'\x05\x13\x00', 0)16 eq_(str(instruction), 'add ax, 13h')17 eq_(len(instruction), 3)18@istest19def sub_2b():20 instruction = Instruction.decode(b'\x2b\xc8', 0)21 eq_(str(instruction), 'sub cx, ax')22 eq_(len(instruction), 2)23@istest24def sub_2d():25 instruction = Instruction.decode(b'\x2d\x00\x10', 0)26 eq_(str(instruction), 'sub ax, 1000h')27 eq_(len(instruction), 3)28@istest29def sub_2e():30 instruction = Instruction.decode(b'\x2e\xac', 0)31 eq_(str(instruction), 'lods byte ptr cs:[si]')32 eq_(len(instruction), 2)33@istest34def xor_33():35 instruction = Instruction.decode(b'\x33\xed', 0)36 eq_(str(instruction), 'xor bp, bp')37 eq_(len(instruction), 2)38@istest39def cmp_3b():40 instruction = Instruction.decode(b'\x3b\xda', 0)41 eq_(str(instruction), 'cmp bx, dx')42 eq_(len(instruction), 2)43@istest44def mov_80():45 instruction = Instruction.decode(b'\x80\x7e\xfe\x13', 0)46 eq_(str(instruction), 'mov [bp+254], 13h')47 eq_(len(instruction), 4)48@istest49def add_83_c7():50 instruction = Instruction.decode(b'\x83\xc7\x04', 0)51 eq_(str(instruction), 'add di, 4')52 eq_(len(instruction), 3)53@istest54def mov_89():55 instruction = Instruction.decode(b'\x89\x1d', 0)56 eq_(str(instruction), 'mov [di], bx')57 eq_(len(instruction), 2)58@istest59def mov_8b():60 instruction = Instruction.decode(b'\x8b\xc4', 0)61 eq_(str(instruction), 'mov ax, sp')62 eq_(len(instruction), 2)63@istest64def mov_8b_5d():65 instruction = Instruction.decode(b'\x8b\x5d\x08', 0)66 eq_(str(instruction), 'mov bx, [di+8]')67 eq_(len(instruction), 3)68@istest69def mov_8c_06():70 instruction = Instruction.decode(b'\x8c\x06\x84\x43', 0)71 eq_(str(instruction), 'mov 4384h, es')72 eq_(len(instruction), 4)73@istest74def mov_8c_45():75 instruction = Instruction.decode(b'\x8c\x45\x02', 0)76 eq_(str(instruction), 'mov word ptr [di+2], es')77 eq_(len(instruction), 3)78@istest79def mov_8e():80 instruction = Instruction.decode(b'\x8e\xda', 0)81 eq_(str(instruction), 'mov ds, dx')82 eq_(len(instruction), 2)83@istest84def nop_90():85 instruction = Instruction.decode(b'\x90', 0)86 eq_(str(instruction), 'nop')87 eq_(len(instruction), 1)88@istest89def call_9a():90 instruction = Instruction.decode(b'\x9a\x00\x00\xbb\x15', 0)91 eq_(str(instruction), 'call 15bb:0000')92 eq_(len(instruction), 5)93@istest94def mov_26_a1():95 instruction = Instruction.decode(b'\x26\xa1\x02\x00', 0)96 eq_(str(instruction), 'mov ax, es:2')97 eq_(len(instruction), 4)98@istest99def mov_a0():100 instruction = Instruction.decode(b'\xa0\xc9\x82', 0)101 eq_(str(instruction), 'mov al, 82C9h')102 eq_(len(instruction), 3)103@istest104def mov_a3():105 instruction = Instruction.decode(b'\xa3\x5c\x43', 0)106 eq_(str(instruction), 'mov 435Ch, ax')107 eq_(len(instruction), 3)108@istest109def mov_b1():110 instruction = Instruction.decode(b'\xb1\x04', 0)111 eq_(str(instruction), 'mov cl, 4')112 eq_(len(instruction), 2)113@istest114def mov_b4():115 instruction = Instruction.decode(b'\xb4\x35', 0)116 eq_(str(instruction), 'mov ah, 35h')117 eq_(len(instruction), 2)118@istest119def mov_b9():120 instruction = Instruction.decode(b'\xb9\x12\x00', 0)121 eq_(str(instruction), 'mov cx, 12h')122 eq_(len(instruction), 3)123@istest124def mov_ba():125 instruction = Instruction.decode(b'\xba\x40\x17', 0)126 eq_(str(instruction), 'mov dx, 1740h')127 eq_(len(instruction), 3)128@istest129def mov_be():130 instruction = Instruction.decode(b'\xbe\xdd\x01', 0)131 eq_(str(instruction), 'mov si, 01DDh')132 eq_(len(instruction), 3)133@istest134def mov_bf():135 instruction = Instruction.decode(b'\xbf\xd0\x84', 0)136 eq_(str(instruction), 'mov di, 84D0h')137 eq_(len(instruction), 3)138@istest139def les_c4():140 instruction = Instruction.decode(b'\xc4\x7d\x0c', 0)141 eq_(str(instruction), 'les di, [di+0Ch]')142 eq_(len(instruction), 3)143@istest144def int_cd():145 instruction = Instruction.decode(b'\xcd\x21', 0)146 eq_(str(instruction), 'int 21h')147 eq_(len(instruction), 2)148@istest149def shr_d3():150 instruction = Instruction.decode(b'\xd3\xe8', 0)151 eq_(str(instruction), 'shr ax, cl')152 eq_(len(instruction), 2)153@istest154def loop_e2():155 instruction = Instruction.decode(b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xe2\xf0', 16)156 eq_(str(instruction), 'loop 0') # loop 16 steps back157 eq_(len(instruction), 2)158@istest159def jcxz_e3():160 instruction = Instruction.decode(b'\xe3\x07', 0)161 eq_(str(instruction), 'jcxz 7')162 eq_(len(instruction), 2)163@istest164def call_e8_negative():165 instruction = Instruction.decode(b'\xe8\x9f\xf8', 0)166 eq_(str(instruction), 'call 0761h')167 eq_(len(instruction), 3)168@istest169def cld_fc():170 instruction = Instruction.decode(b'\xfc', 0)171 eq_(str(instruction), 'cld')172 eq_(len(instruction), 1)173@istest174def push_ff():175 instruction = Instruction.decode(b'\xff\x36\x26\x26', 0)176 eq_(str(instruction), 'push 2626h')...
test_ast_basic.py
Source:test_ast_basic.py
1from tools import *2from tools_ast import *3@istest4def int():5 assert_ast(6 '1',7 """8 [INT:1]9 [EOF:]10 """11 )12@istest13def two_blocks_int_int():14 assert_ast(15 '1 1',16 """17 [INT:1]18 [INT:1]19 [EOF:]20 """21 )22@istest23def string():24 assert_ast(25 '"foo"',26 """27 [STRING:foo]28 [EOF:]29 """30 )31@istest32def symbol():33 assert_ast(34 'foo',35 """36 [SYMBOL:foo]37 [EOF:]38 """39 )40@istest41def sum_of_ints():42 assert_ast(43 "1 + 2",44 """45 [PLUS:+]46 [INT:1]47 [INT:2]48 [EOF:]49 """50 )51@istest52def sum_of_symbol_and_int():53 assert_ast(54 "a + 2",55 """56 [PLUS:+]57 [SYMBOL:a]58 [INT:2]59 [EOF:]60 """61 )62@istest63def longer_sum():64 assert_ast(65 "1 + a - 2",66 """67 [PLUS:+]68 [INT:1]69 [MINUS:-]70 [SYMBOL:a]71 [INT:2]72 [EOF:]73 """74 )75@istest76def function_call_no_args():77 assert_ast(78 "foo()",79 """80 [LPAREN:(]81 [SYMBOL:foo]82 [EOF:]83 """84 )85@istest86def function_call_one_arg():87 assert_ast(88 "foo( bar )",89 """90 [LPAREN:(]91 [SYMBOL:foo]92 [SYMBOL:bar]93 [EOF:]94 """95 )96@istest97def function_call_multiple_args():98 assert_ast(99 "foo( 1, bar, 4 )",100 """101 [LPAREN:(]102 [SYMBOL:foo]103 [INT:1]104 [COMMA:,]105 [SYMBOL:bar]106 [COMMA:,]107 [INT:4]108 [EOF:]109 """110 )111@istest112def double_function_call():113 assert_ast(114 "foo( 1, bar, 4 )( 1 )",115 """116 [LPAREN:(]117 [LPAREN:(]118 [SYMBOL:foo]119 [INT:1]120 [COMMA:,]121 [SYMBOL:bar]122 [COMMA:,]123 [INT:4]124 [INT:1]125 [EOF:]126 """127 )128@istest129def double_function_call_even_if_space():130 assert_ast(131 "foo( 1, bar, 4 ) ( 1 )",132 """133 [LPAREN:(]134 [LPAREN:(]135 [SYMBOL:foo]136 [INT:1]137 [COMMA:,]138 [SYMBOL:bar]139 [COMMA:,]140 [INT:4]141 [INT:1]142 [EOF:]143 """144 )145@istest146def two_blocks_function_call_separator_bracketed_item():147 """148 two_blocks_function_call_bracketted149 This is not parsed as a double function call (like the above test)150 because there is a semi-colon separating the two blocks.151 """152 assert_ast(153 "foo( 1, bar, 4 ) ; ( 1 )",154 """155 [LPAREN:(]156 [SYMBOL:foo]157 [INT:1]158 [COMMA:,]159 [SYMBOL:bar]160 [COMMA:,]161 [INT:4]162 [SEMICOLON:;]163 [INT:1]164 [EOF:]165 """166 )167@istest168def lookup():169 assert_ast(170 'foo.bar',171 """172 [DOT:.]173 [SYMBOL:foo]174 [SYMBOL:bar]175 [EOF:]176 """177 )178@skip # Need to make . and ( at the same level, not allow . precedence179@istest180def several():181 assert_ast(182 'x(3).foo(7)',183 ""184# [LPAREN:(]185# [DOT:.]186# [LPAREN:(]187# [SYMBOL:x]188# [INT:3]189# [INT:7]190# [EOF:]191# """...
multirun.py
Source:multirun.py
1import options2import ocgantestdisjoint3import cvpr4import numpy as np5import random6import os7opt = options.test_options()8opt.istest = 09#First use the validation set to pick best model:wq10text_file = open(opt.dataset + "_progress.txt", "w")11text_file.close()12text_file1 = open(opt.dataset + "_progress1.txt", "w")13text_file1.close()14for itt in range(50):15 text_file1 = open(opt.dataset + "_progress1.txt", "a") 16 text_file = open(opt.dataset + "_progress.txt", "a")17 os.system('python2 cvprappend.py --epochs 1001 --ndf 16 --ngf 64 --expname grapesip64 --img_wd 125 --img_ht 125 --depth 3 --datapath ../ --noisevar 0.2 --lambda1 500 --seed 1000 --append 0')18 auc1 = []19 auc2=[]20 auc3=[]21 auc4=[]22 ran = range(0,1000,10)23 for i in ran:24 opt.epochs = i25 istest = 026 roc_auc = ocgantestdisjoint.main(opt)27 print(roc_auc)28 auc1.append(roc_auc[0])29 auc2.append(roc_auc[1])30 auc3.append(roc_auc[2])31 auc4.append(roc_auc[3])32 #Pick best model w.r.t criterion 133 imax = np.argmax(np.array(auc1))34 opt.epochs = ran[imax]35 opt.istest=136 res1 = ocgantestdisjoint.main(opt)[0]37 print(res1)38 39 imax = np.argmax(np.array(auc2))40 opt.epochs = ran[imax]41 opt.istest=142 res2 = ocgantestdisjoint.main(opt)[1]43 print(res2)44 imax = np.argmax(np.array(auc3))45 opt.epochs = ran[imax]46 opt.istest=147 res3 = ocgantestdisjoint.main(opt)[2]48 print(res3)49 imax = np.argmax(np.array(auc4))50 opt.epochs = ran[imax]51 opt.istest=152 res4 = ocgantestdisjoint.main(opt)[3]53 print(res4)54 text_file.write("%s %s %s %s\n" % (str(res1), str(res2), str(res3), str(res4)))55 text_file.close()56'''57 print("AUC for criterion 1 (test): " + str(ocgantestdisjoint.main(opt)[0]))58#Pick best model w.r.t criterion 259i = np.argmin(np.array(auc2))60opt.epochs = ran[i]61opt.istest=62print(ran[i])63print("AUC for criterion 2 (val): " + str(ocgantestdisjoint.main(opt)[1]))64opt.istest=165print("AUC for criterion 2 (test): " + str(ocgantestdisjoint.main(opt)[1]))66#Pick best model w.r.t criterion 367i = np.argmin(np.array(auc3))68opt.epochs = ran[i]69opt.istest=070print(ran[i])71print("AUC for criterion 3 (val): " + str(ocgantestdisjoint.main(opt)[2]))72opt.istest=173print("AUC for criterion 3 (test): " + str(ocgantestdisjoint.main(opt)[2]))74#Pick best model w.r.t criterion 475i = np.argmin(np.array(auc4))76opt.epochs = ran[i]77opt.istest=078print(ran[i])79print("AUC for criterion 4 (val): " + str(ocgantestdisjoint.main(opt)[3]))80opt.istest=1...
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!!