How to use __code__ method in Slash

Best Python code snippet using slash

test_bytecode_modification.py

Source:test_bytecode_modification.py Github

copy

Full Screen

1import dis2import sys3import unittest4from io import StringIO5from _pydevd_frame_eval.pydevd_modify_bytecode import insert_code6TRACE_MESSAGE = "Trace called"7def tracing():8 print(TRACE_MESSAGE)9def call_tracing():10 tracing()11def bar(a, b):12 return a + b13IS_PY36 = sys.version_info[0] == 3 and sys.version_info[1] == 614@unittest.skipIf(not IS_PY36, reason='Test requires Python 3.6')15class TestInsertCode(unittest.TestCase):16 lines_separator = "---Line tested---"17 def check_insert_every_line(self, func_to_modify, func_to_insert, number_of_lines):18 first_line = func_to_modify.__code__.co_firstlineno + 119 last_line = first_line + number_of_lines20 for i in range(first_line, last_line):21 self.check_insert_to_line_with_exec(func_to_modify, func_to_insert, i)22 print(self.lines_separator)23 def check_insert_to_line_with_exec(self, func_to_modify, func_to_insert, line_number):24 code_orig = func_to_modify.__code__25 code_to_insert = func_to_insert.__code__26 success, result = insert_code(code_orig, code_to_insert, line_number)27 exec(result)28 output = sys.stdout.getvalue().strip().split(self.lines_separator)[-1]29 self.assertTrue(TRACE_MESSAGE in output)30 def check_insert_to_line_by_symbols(self, func_to_modify, func_to_insert, line_number, code_for_check):31 code_orig = func_to_modify.__code__32 code_to_insert = func_to_insert.__code__33 success, result = insert_code(code_orig, code_to_insert, line_number)34 self.compare_bytes_sequence(list(result.co_code), list(code_for_check.co_code))35 def compare_bytes_sequence(self, code1, code2):36 seq1 = [(offset, op, arg) for offset, op, arg in dis._unpack_opargs(code1)]37 seq2 = [(offset, op, arg) for offset, op, arg in dis._unpack_opargs(code2)]38 self.assertTrue(len(seq1) == len(seq2), "Bytes sequences have different lengths")39 for i in range(len(seq1)):40 of, op1, arg1 = seq1[i]41 _, op2, arg2 = seq2[i]42 self.assertEqual(op1, op2, "Different operators at offset {}".format(of))43 if arg1 != arg2:44 if op1 in (100, 101, 106, 116):45 # Sometimes indexes of variable names and consts may be different, when we insert them, it's ok46 continue47 else:48 self.assertEquals(arg1, arg2, "Different arguments at offset {}".format(of))49 def test_assignment(self):50 self.original_stdout = sys.stdout51 sys.stdout = StringIO()52 try:53 def original():54 a = 155 b = 256 c = 357 self.check_insert_every_line(original, tracing, 3)58 finally:59 sys.stdout = self.original_stdout60 def test_for_loop(self):61 self.original_stdout = sys.stdout62 sys.stdout = StringIO()63 try:64 def original():65 n = 366 sum = 067 for i in range(n):68 sum += i69 return sum70 self.check_insert_every_line(original, tracing, 5)71 finally:72 sys.stdout = self.original_stdout73 def test_if(self):74 self.original_stdout = sys.stdout75 sys.stdout = StringIO()76 try:77 def original():78 if True:79 a = 180 else:81 a = 082 print(a)83 self.check_insert_to_line_with_exec(original, tracing, original.__code__.co_firstlineno + 2)84 self.check_insert_to_line_with_exec(original, tracing, original.__code__.co_firstlineno + 5)85 finally:86 sys.stdout = self.original_stdout87 def test_else(self):88 self.original_stdout = sys.stdout89 sys.stdout = StringIO()90 try:91 def original():92 if False:93 a = 194 else:95 a = 096 print(a)97 self.check_insert_to_line_with_exec(original, tracing, original.__code__.co_firstlineno + 4)98 self.check_insert_to_line_with_exec(original, tracing, original.__code__.co_firstlineno + 5)99 finally:100 sys.stdout = self.original_stdout101 def test_for_else(self):102 self.original_stdout = sys.stdout103 sys.stdout = StringIO()104 try:105 def original():106 sum = 0107 for i in range(3):108 sum += i109 else:110 print(sum)111 def check_line_1():112 tracing()113 sum = 0114 for i in range(3):115 sum += i116 else:117 print(sum)118 def check_line_3():119 sum = 0120 for i in range(3):121 tracing()122 sum += i123 else:124 print(sum)125 def check_line_5():126 sum = 0127 for i in range(3):128 sum += i129 else:130 tracing()131 print(sum)132 self.check_insert_to_line_with_exec(original, tracing, original.__code__.co_firstlineno + 1)133 self.check_insert_to_line_with_exec(original, tracing, original.__code__.co_firstlineno + 3)134 self.check_insert_to_line_with_exec(original, tracing, original.__code__.co_firstlineno + 5)135 sys.stdout = self.original_stdout136 self.check_insert_to_line_by_symbols(original, call_tracing, original.__code__.co_firstlineno + 1,137 check_line_1.__code__)138 self.check_insert_to_line_by_symbols(original, call_tracing, original.__code__.co_firstlineno + 3,139 check_line_3.__code__)140 self.check_insert_to_line_by_symbols(original, call_tracing, original.__code__.co_firstlineno + 5,141 check_line_5.__code__)142 finally:143 sys.stdout = self.original_stdout144 def test_elif(self):145 self.original_stdout = sys.stdout146 sys.stdout = StringIO()147 try:148 def original():149 a = 5150 b = 0151 if a < 0:152 print("a < 0")153 elif a < 3:154 print("a < 3")155 else:156 print("a >= 3")157 b = a158 return b159 def check_line_1():160 tracing()161 a = 5162 b = 0163 if a < 0:164 print("a < 0")165 elif a < 3:166 print("a < 3")167 else:168 print("a >= 3")169 b = a170 return b171 def check_line_8():172 a = 5173 b = 0174 if a < 0:175 print("a < 0")176 elif a < 3:177 print("a < 3")178 else:179 tracing()180 print("a >= 3")181 b = a182 return b183 def check_line_9():184 a = 5185 b = 0186 if a < 0:187 print("a < 0")188 elif a < 3:189 print("a < 3")190 else:191 print("a >= 3")192 tracing()193 b = a194 return b195 self.check_insert_to_line_with_exec(original, tracing, original.__code__.co_firstlineno + 1)196 self.check_insert_to_line_with_exec(original, tracing, original.__code__.co_firstlineno + 2)197 self.check_insert_to_line_with_exec(original, tracing, original.__code__.co_firstlineno + 8)198 self.check_insert_to_line_with_exec(original, tracing, original.__code__.co_firstlineno + 9)199 self.check_insert_to_line_by_symbols(original, call_tracing, original.__code__.co_firstlineno + 1,200 check_line_1.__code__)201 self.check_insert_to_line_by_symbols(original, call_tracing, original.__code__.co_firstlineno + 8,202 check_line_8.__code__)203 self.check_insert_to_line_by_symbols(original, call_tracing, original.__code__.co_firstlineno + 9,204 check_line_9.__code__)205 finally:206 sys.stdout = self.original_stdout207 def test_call_other_function(self):208 self.original_stdout = sys.stdout209 sys.stdout = StringIO()210 try:211 def original():212 a = 1213 b = 3214 c = bar(a, b)215 return c216 def check_line_3():217 a = 1218 b = 3219 tracing()220 c = bar(a, b)221 return c222 def check_line_4():223 a = 1224 b = 3225 c = bar(a, b)226 tracing()227 return c228 self.check_insert_every_line(original, tracing, 4)229 sys.stdout = self.original_stdout230 self.check_insert_to_line_by_symbols(original, call_tracing, original.__code__.co_firstlineno + 3,231 check_line_3.__code__)232 self.check_insert_to_line_by_symbols(original, call_tracing, original.__code__.co_firstlineno + 4,233 check_line_4.__code__)234 finally:235 sys.stdout = self.original_stdout236 def test_class_method(self):237 self.original_stdout = sys.stdout238 sys.stdout = StringIO()239 try:240 class A(object):241 @staticmethod242 def foo():243 print("i'm in foo")244 @staticmethod245 def check_line_2():246 tracing()247 print("i'm in foo")248 original = A.foo249 self.check_insert_to_line_with_exec(original, tracing, original.__code__.co_firstlineno + 2)250 self.check_insert_to_line_by_symbols(original, call_tracing, original.__code__.co_firstlineno + 2,251 A.check_line_2.__code__)252 finally:253 sys.stdout = self.original_stdout254 def test_offset_overflow(self):255 self.original_stdout = sys.stdout256 sys.stdout = StringIO()257 try:258 def foo():259 a = 1260 b = 2 # breakpoint261 c = 3262 a1 = 1 if a > 1 else 2263 a2 = 1 if a > 1 else 2264 a3 = 1 if a > 1 else 2265 a4 = 1 if a > 1 else 2266 a5 = 1 if a > 1 else 2267 a6 = 1 if a > 1 else 2268 a7 = 1 if a > 1 else 2269 a8 = 1 if a > 1 else 2270 a9 = 1 if a > 1 else 2271 a10 = 1 if a > 1 else 2272 a11 = 1 if a > 1 else 2273 a12 = 1 if a > 1 else 2274 a13 = 1 if a > 1 else 2275 for i in range(1):276 if a > 0:277 print("111")278 # a = 1279 else:280 print("222")281 return b282 def check_line_2():283 a = 1284 tracing()285 b = 2286 c = 3287 a1 = 1 if a > 1 else 2288 a2 = 1 if a > 1 else 2289 a3 = 1 if a > 1 else 2290 a4 = 1 if a > 1 else 2291 a5 = 1 if a > 1 else 2292 a6 = 1 if a > 1 else 2293 a7 = 1 if a > 1 else 2294 a8 = 1 if a > 1 else 2295 a9 = 1 if a > 1 else 2296 a10 = 1 if a > 1 else 2297 a11 = 1 if a > 1 else 2298 a12 = 1 if a > 1 else 2299 a13 = 1 if a > 1 else 2300 for i in range(1):301 if a > 0:302 print("111")303 # a = 1304 else:305 print("222")306 return b307 self.check_insert_to_line_with_exec(foo, tracing, foo.__code__.co_firstlineno + 2)308 self.check_insert_to_line_by_symbols(foo, call_tracing, foo.__code__.co_firstlineno + 2,309 check_line_2.__code__)310 finally:311 sys.stdout = self.original_stdout312 def test_long_lines(self):313 self.original_stdout = sys.stdout314 sys.stdout = StringIO()315 try:316 def foo():317 a = 1318 b = 1 if a > 1 else 2 if a > 0 else 3 if a > 4 else 23 if a > 1 else 2 if a > 0 else 3 if a > 4 else 23 if a > 1 else 2 if a > 0 else 3 if a > 4 else 23 if a > 1 else 2 if a > 0 else 3 if a > 4 else 23 if a > 1 else 2 if a > 0 else 3 if a > 4 else 23 if a > 1 else 2 if a > 0 else 3 if a > 4 else 23 if a > 1 else 2 if a > 0 else 3 if a > 4 else 23 if a > 1 else 2 if a > 0 else 3 if a > 4 else 23 if a > 1 else 2 if a > 0 else 3 if a > 4 else 23 if a > 1 else 2 if a > 0 else 3 if a > 4 else 23319 c = 1 if b > 1 else 2 if b > 0 else 3 if a > 4 else 23 if a > 1 else 2 if a > 0 else 3 if a > 4 else 23 if a > 1 else 2 if a > 0 else 3 if a > 4 else 23 if a > 1 else 2 if a > 0 else 3 if a > 4 else 23 if a > 1 else 2 if a > 0 else 3 if a > 4 else 23 if a > 1 else 2 if a > 0 else 3 if a > 4 else 23 if a > 1 else 2 if a > 0 else 3 if a > 4 else 23 if a > 1 else 2 if a > 0 else 3 if a > 4 else 23 if a > 1 else 2 if a > 0 else 3 if a > 4 else 23 if a > 1 else 2 if a > 0 else 3 if a > 4 else 23320 d = 1 if c > 1 else 2 if c > 0 else 3 if a > 4 else 23 if a > 1 else 2 if a > 0 else 3 if a > 4 else 23 if a > 1 else 2 if a > 0 else 3 if a > 4 else 23 if a > 1 else 2 if a > 0 else 3 if a > 4 else 23 if a > 1 else 2 if a > 0 else 3 if a > 4 else 23 if a > 1 else 2 if a > 0 else 3 if a > 4 else 23 if a > 1 else 2 if a > 0 else 3 if a > 4 else 23 if a > 1 else 2 if a > 0 else 3 if a > 4 else 23 if a > 1 else 2 if a > 0 else 3 if a > 4 else 23 if a > 1 else 2 if a > 0 else 3 if a > 4 else 23321 e = d + 1322 return e323 def check_line_2():324 a = 1325 tracing()326 b = 1 if a > 1 else 2 if a > 0 else 3 if a > 4 else 23 if a > 1 else 2 if a > 0 else 3 if a > 4 else 23 if a > 1 else 2 if a > 0 else 3 if a > 4 else 23 if a > 1 else 2 if a > 0 else 3 if a > 4 else 23 if a > 1 else 2 if a > 0 else 3 if a > 4 else 23 if a > 1 else 2 if a > 0 else 3 if a > 4 else 23 if a > 1 else 2 if a > 0 else 3 if a > 4 else 23 if a > 1 else 2 if a > 0 else 3 if a > 4 else 23 if a > 1 else 2 if a > 0 else 3 if a > 4 else 23 if a > 1 else 2 if a > 0 else 3 if a > 4 else 23327 c = 1 if b > 1 else 2 if b > 0 else 3 if a > 4 else 23 if a > 1 else 2 if a > 0 else 3 if a > 4 else 23 if a > 1 else 2 if a > 0 else 3 if a > 4 else 23 if a > 1 else 2 if a > 0 else 3 if a > 4 else 23 if a > 1 else 2 if a > 0 else 3 if a > 4 else 23 if a > 1 else 2 if a > 0 else 3 if a > 4 else 23 if a > 1 else 2 if a > 0 else 3 if a > 4 else 23 if a > 1 else 2 if a > 0 else 3 if a > 4 else 23 if a > 1 else 2 if a > 0 else 3 if a > 4 else 23 if a > 1 else 2 if a > 0 else 3 if a > 4 else 23328 d = 1 if c > 1 else 2 if c > 0 else 3 if a > 4 else 23 if a > 1 else 2 if a > 0 else 3 if a > 4 else 23 if a > 1 else 2 if a > 0 else 3 if a > 4 else 23 if a > 1 else 2 if a > 0 else 3 if a > 4 else 23 if a > 1 else 2 if a > 0 else 3 if a > 4 else 23 if a > 1 else 2 if a > 0 else 3 if a > 4 else 23 if a > 1 else 2 if a > 0 else 3 if a > 4 else 23 if a > 1 else 2 if a > 0 else 3 if a > 4 else 23 if a > 1 else 2 if a > 0 else 3 if a > 4 else 23 if a > 1 else 2 if a > 0 else 3 if a > 4 else 23329 e = d + 1330 return e331 self.check_insert_to_line_with_exec(foo, tracing, foo.__code__.co_firstlineno + 2)332 sys.stdout = self.original_stdout333 self.check_insert_to_line_by_symbols(foo, call_tracing, foo.__code__.co_firstlineno + 2,334 check_line_2.__code__)335 finally:336 sys.stdout = self.original_stdout337 def test_many_names(self):338 self.original_stdout = sys.stdout339 sys.stdout = StringIO()340 try:341 from tests_pydevd_python._bytecode_many_names_example import foo342 self.check_insert_to_line_with_exec(foo, tracing, foo.__code__.co_firstlineno + 2)343 finally:344 sys.stdout = self.original_stdout345 def test_extended_arg_overflow(self):346 from tests_pydevd_python._bytecode_overflow_example import Dummy, DummyTracing347 self.check_insert_to_line_by_symbols(Dummy.fun, call_tracing, Dummy.fun.__code__.co_firstlineno + 3,348 DummyTracing.fun.__code__)349 def test_double_extended_arg(self):350 self.original_stdout = sys.stdout351 sys.stdout = StringIO()352 try:353 def foo():354 a = 1355 b = 2356 if b > 0:357 d = a + b358 d += 1359 b = b - 1 if a > 0 else b + 1360 b = b - 1 if a > 0 else b + 1361 b = b - 1 if a > 0 else b + 1362 b = b - 1 if a > 0 else b + 1363 b = b - 1 if a > 0 else b + 1364 b = b - 1 if a > 0 else b + 1365 b = b - 1 if a > 0 else b + 1366 b = b - 1 if a > 0 else b + 1367 b = b - 1 if a > 0 else b + 1368 b = b - 1 if a > 0 else b + 1369 b = b - 1 if a > 0 else b + 1370 b = b - 1 if a > 0 else b + 1371 b = b - 1 if a > 0 else b + 1372 b = b - 1 if a > 0 else b + 1373 b = b - 1 if a > 0 else b + 1374 b = b - 1 if a > 0 else b + 1375 b = b - 1 if a > 0 else b + 1376 b = b - 1 if a > 0 else b + 1377 b = b - 1 if a > 0 else b + 1378 a = a + 1379 return a380 def foo_check():381 a = 1382 b = 2383 tracing()384 if b > 0:385 d = a + b386 d += 1387 b = b - 1 if a > 0 else b + 1388 b = b - 1 if a > 0 else b + 1389 b = b - 1 if a > 0 else b + 1390 b = b - 1 if a > 0 else b + 1391 b = b - 1 if a > 0 else b + 1392 b = b - 1 if a > 0 else b + 1393 b = b - 1 if a > 0 else b + 1394 b = b - 1 if a > 0 else b + 1395 b = b - 1 if a > 0 else b + 1396 b = b - 1 if a > 0 else b + 1397 b = b - 1 if a > 0 else b + 1398 b = b - 1 if a > 0 else b + 1399 b = b - 1 if a > 0 else b + 1400 b = b - 1 if a > 0 else b + 1401 b = b - 1 if a > 0 else b + 1402 b = b - 1 if a > 0 else b + 1403 b = b - 1 if a > 0 else b + 1404 b = b - 1 if a > 0 else b + 1405 b = b - 1 if a > 0 else b + 1406 a = a + 1407 return a408 self.check_insert_to_line_with_exec(foo, tracing, foo.__code__.co_firstlineno + 2)409 sys.stdout = self.original_stdout410 self.check_insert_to_line_by_symbols(foo, call_tracing, foo.__code__.co_firstlineno + 3,411 foo_check.__code__)412 finally:...

Full Screen

Full Screen

compiler.py

Source:compiler.py Github

copy

Full Screen

1"""2 (c) Tivole3 Compiler for 'Turing Code'4"""5# Libraries6import copy7__code__ = open('turing_code.txt', 'r')8__lines__ = __code__.readlines()9__lines_backend__ = copy.deepcopy(__lines__)10# Removing empty lines11while '\n' in __lines__:12 __lines__.remove('\n')13# Checking for symbols ';', ',', '->', '{' and '}'14__passed_lines__ = 015__checked_codes__ = []16for __i__ in range(len(__lines__)):17 __passed_lines__ += 118 if '#' in __lines__[__i__]:19 __checking_line__ = __lines__[__i__][:__lines__[__i__].find('#')]20 if __checking_line__ == '':21 __passed_lines__ -= 122 continue23 else:24 __checking_line__ = __lines__[__i__]25 # Check for symbol ';'26 assert ';' in __checking_line__, f'(!) Compilation Error. Missing \';\' in line {__lines_backend__.index(__lines__[__i__]) + 1}'27 28 if __passed_lines__ >= 5:29 # Check for symbol '->'30 assert '->' in __checking_line__, f'(!) Compilation Error. Missing \'->\' in line {__lines_backend__.index(__lines__[__i__]) + 1}'31 32 # Check for symbol ','33 assert __checking_line__.count(',') == 3, f'(!) Compilation Error. Invalid syntax in line {__lines_backend__.index(__lines__[__i__]) + 1}'34 # Check for unkown commands35 __unknown_command__ = __checking_line__[__checking_line__.find(';') + 1:].replace('\n', '')36 assert __checking_line__[__checking_line__.find(';') + 1:].replace(' ', '').replace('\n', '') in '', f'(!) Compilation Error. Unkown command \'{__unknown_command__}\' in line {__lines_backend__.index(__lines__[__i__]) + 1}'37 38 # Check for double commands39 assert not __checking_line__.replace('\n', '') in __checked_codes__, f'(!) Compilation Error. Doubled Turing code in line {__lines_backend__.index(__lines__[__i__]) + 1}'40 __checked_codes__.append(__checking_line__.replace('\n', ''))41 else:42 assert '{' in __checking_line__, '(!) Compilation Error. Missing \'{\'' + f' in line {__lines_backend__.index(__lines__[__i__]) + 1}'43 assert '}' in __checking_line__, '(!) Compilation Error. Missing \'}\'' + f' in line {__lines_backend__.index(__lines__[__i__]) + 1}'44# Removing commentaries for compiling45for __i__ in range(len(__lines__)):46 if '#' in __lines__[__i__]:47 if __lines__[__i__].find('#') > 0:48 __lines__[__i__] = __lines__[__i__][:__lines__[__i__].find('#')]49 else:50 __lines__[__i__] = '\n'51# Removing unnecessary symbols for compiling52for __i__ in range(len(__lines__)):53 # Removing ' '54 if ' ' in __lines__[__i__]:55 __lines__[__i__] = __lines__[__i__].replace(' ', '')56 # Removing '\n'57 if '\n' in __lines__[__i__]:58 __lines__[__i__] = __lines__[__i__].replace('\n', '')59# Removing empty lines60while '' in __lines__:61 __lines__.remove('')62# Checking Alphabet63assert 'ALPHABET' in __lines__[0], '(!) Compilation Error. ALPHABET is missing!'64# Checking Empty symbol65assert 'EMPTY' in __lines__[1], '(!) Compilation Error. Empty symbol (\'EMPTY\') is missing!'66# Checking START_STATE and STOP_STATE 67assert 'START_STATE' in __lines__[2], '(!) Compilation Error. START_STATE is missing!'68assert 'STOP_STATE' in __lines__[3], '(!) Compilation Error. STOP_STATE is missing!'69# Reading Alphabet70__ALPHABET__ = __lines__[0][__lines__[0].find('{') + 1:__lines__[0].find('}')].replace(' ', '').split(',')71# Reading Empty symbol72__EMPTY__ = __lines__[1][__lines__[1].find('{') + 1:__lines__[1].find('}')].replace(' ', '').split(',')[0]73if __lines__[1][__lines__[1].find('{') + 1:__lines__[1].find('}')].count(',') == 1:74 __NUMBER_OF_EMPTY__ = int(__lines__[1][__lines__[1].find('{') + 1:__lines__[1].find('}')].replace(' ', '').split(',')[1])75else:76 __NUMBER_OF_EMPTY__ = 2577# Readding START_STATE and STOP_STATE78__START_STATE__ = __lines__[2][__lines__[2].find('{') + 1:__lines__[2].find('}')].replace(' ', '')79__STOP_STATE__ = __lines__[3][__lines__[3].find('{') + 1:__lines__[3].find('}')].replace(' ', '')80# Checking EMPTY symbol81assert __EMPTY__ in __ALPHABET__, f'(!) Compilation Error. Empty symbol \'{__EMPTY__}\' is not found in ALPHABET.'82# Reading codes83__CODE__ = []84for __i__ in range(4, len(__lines__)):85 __CODE__.append(tuple(__lines__[__i__][:__lines__[__i__].find(';')].replace(' ', '').replace('->', ',').split(',')))86# Checking is all symbols from ALPHABET87for __i__ in range(len(__CODE__)):88 assert __CODE__[__i__][1] in __ALPHABET__, f'(!) Compilation Error. Symbol \'{__CODE__[__i__][1]}\' not found in the ALPHABET.'89 assert __CODE__[__i__][3] in __ALPHABET__, f'(!) Compilation Error. Symbol \'{__CODE__[__i__][3]}\' not found in the ALPHABET.'90 assert __CODE__[__i__][4] in ['L', 'R', 'S'], f'(!) Compilation Error. Symbol \'{__CODE__[__i__][4]}\' is unknown. Use only L, R or S.'91# Checking is all states defined92for __i__ in range(len(__CODE__)):93 if __CODE__[__i__][2] != __STOP_STATE__:94 assert __CODE__[__i__][2] in [__CODE__[__j__][0] for __j__ in range(len(__CODE__))], f'(!) Compilation Error. Udefined state \'{__CODE__[__i__][2]}\''95# Checking is __START_STATE__ defined96assert __START_STATE__ in [__CODE__[__j__][0] for __j__ in range(len(__CODE__))], f'(!) Compilation Error. Start state \'{__START_STATE__}\' is undefined.'97def get_empty_symbol():98 return __EMPTY__99def Turing_Machine(Sequence_INPUT):100 """101 Function to program Turing Machine code.102 """103 # Checking INPUT Sequence104 for c in Sequence_INPUT:105 assert c in __ALPHABET__, f'(!) Compilation Error. Unknown symbol \'{c}\' in input data. Check ALPHABET.'106 Sequence_INPUT = list(__EMPTY__ * __NUMBER_OF_EMPTY__ + Sequence_INPUT + __EMPTY__ * __NUMBER_OF_EMPTY__)107 Sequence = copy.deepcopy(Sequence_INPUT)108 109 state = __START_STATE__110 # Put the machine_arrow in the right place111 machine_arrow = 0112 while Sequence_INPUT[machine_arrow] == __EMPTY__:113 machine_arrow += 1114 Sequences_OUTPUT = []115 Sequences_OUTPUT.append([copy.deepcopy(Sequence_INPUT), state, machine_arrow, ''])116 TM_STOP = False117 while not TM_STOP:118 Find = False119 for i in range(len(__CODE__)):120 STOP = False121 if state == __CODE__[i][0] and Sequence[machine_arrow] == __CODE__[i][1]:122 Find = True123 Sequence[machine_arrow] = __CODE__[i][3]124 state = __CODE__[i][2]125 if __CODE__[i][4] == 'R':126 machine_arrow += 1 # Go RIGHT127 elif __CODE__[i][4] == 'L':128 machine_arrow -= 1 # Go LEFT129 elif __CODE__[i][4] == 'S':130 machine_arrow = machine_arrow # STOP131 STOP = True132 break133 if not Find:134 assert Find, f'(!) Compilation Error. Undefined action for state=\'{state}\' and symbol \'{Sequence[machine_arrow]}\'.'135 break136 Sequences_OUTPUT.append([copy.deepcopy(Sequence), state, machine_arrow, __CODE__[i][4]])137 if state == __STOP_STATE__ and STOP == True:138 TM_STOP = True...

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 Slash 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