Best Python code snippet using localstack_python
test_rename_builtins.py
Source:test_rename_builtins.py
...6import sys7import pytest8from python_minifier import add_namespace, bind_names, resolve_names, allow_rename_locals, allow_rename_globals, \9 compare_ast, rename, CompareError, unparse10def do_rename(source):11 # This will raise if the source file can't be parsed12 module = ast.parse(source, 'test_rename_bultins')13 add_namespace(module)14 bind_names(module)15 resolve_names(module)16 allow_rename_locals(module, True)17 allow_rename_globals(module, True)18 rename(module)19 return module20def assert_code(expected_ast, actual_ast):21 try:22 compare_ast(expected_ast, actual_ast)23 except CompareError as e:24 print(e)25 print(unparse(actual_ast))26 raise27def test_rename_builtins():28 source = '''29sorted()30sorted()31sorted()32sorted()33sorted()34'''35 expected = '''36A=sorted37A()38A()39A()40A()41A()42'''43 expected_ast = ast.parse(expected)44 actual_ast = do_rename(source)45 assert_code(expected_ast, actual_ast)46def test_no_rename_assigned_builtin():47 source = '''48if random.choice([True, False]):49 sorted=str50sorted()51sorted()52sorted()53sorted()54sorted()55'''56 expected = source57 expected_ast = ast.parse(expected)58 actual_ast = do_rename(source)59 assert_code(expected_ast, actual_ast)60def test_rename_local_builtin():61 source = '''62def t():63 sorted()64 sorted()65 sorted()66 sorted()67 sorted()68'''69 expected = '''70A=sorted71def B():72 A()73 A()74 A()75 A()76 A()77'''78 expected_ast = ast.parse(expected)79 actual_ast = do_rename(source)80 assert_code(expected_ast, actual_ast)81def test_no_rename_local_assigned_builtin():82 source = '''83def a():84 if random.choice([True, False]):85 sorted=str86 sorted()87 sorted()88 sorted()89 sorted()90 sorted()91'''92 expected = '''93def A():94 if random.choice([True, False]):95 sorted=str96 sorted()97 sorted()98 sorted()99 sorted()100 sorted()101'''102 expected_ast = ast.parse(expected)103 actual_ast = do_rename(source)...
do_rename.py
Source:do_rename.py
...3import argparse4import os5import re67def do_rename(path, src, dst, recursive):8 success_count = 09 if not path.endswith('\\'):10 path = path + '\\'11 dir_list = os.listdir(path)12 for filename in dir_list:13 abs_path = os.path.join(path, filename) 14 if os.path.isdir(abs_path):15 if not recursive == None:16 success_count += do_rename(abs_path, src, dst, recursive)17 else:18 file_prefix = filename.split('.')[0:-1]19 file_prefix = '.'.join(file_prefix)20 file_ext = filename.split('.')[-1]2122 if src.startswith('.') and dst.startswith('.'):23 if src[1:] == file_ext:24 old_filename = path + filename25 new_filename = path + file_prefix + dst26 print ('%s ---> %s' % (old_filename, new_filename))27 success_count += 128 os.rename(old_filename, new_filename)2930 31 return success_count32333435def usage():36 desc = '''37 do_rename.py path src dst 38 do_rename.py path src dst -r 139 '''40 parser = argparse.ArgumentParser(usage=desc)41 42 parser.add_argument("-r", help="recursive. apply to subdirectory.")43 parser.add_argument("path", help="path")44 parser.add_argument("src", help="source")45 parser.add_argument("dst", help="destination")46 args = parser.parse_args()47 success_count = do_rename(args.path, args.src, args.dst, args.r)48 print ('rename count: %d' % success_count)4950def main():51 # todo 52 usage()5354if __name__== "__main__":
...
rename.py
Source:rename.py
1#!/usr/bin/python2import sys3import os4def rename_rom(path, f):5 suffix = f[f.rfind('.'):]6 newname = f7 do_rename = False8 if ' (' in f:9 newname = '{}{}'.format(newname[:newname.index(' (')], suffix)10 do_rename = True11 if len(newname) > 3 and newname[3] == ' ':12 try:13 space_index = newname.index(' ')14 nbr = int(newname[:space_index])15 newname = newname[space_index + 1:]16 do_rename = True17 except:18 pass19 if len(newname) > 3 and newname[3] == '.':20 try:21 space_index = newname.index('.')22 nbr = int(newname[:space_index])23 newname = newname[space_index + 1:]24 do_rename = True25 except:26 pass27 if newname[0] == ' ':28 newname = newname[1:]29 do_rename = True30 if do_rename:31 os.rename('{}/{}'.format(path, f), '{}/{}'.format(path, newname))32 return '{}'.format(newname)33 else:34 return f35def rename(path):36 if not os.path.isdir(path):37 print('Path "{}" is not a directory'.format(path))38 return39 for f in os.listdir(path):40 print('{} -> {}'.format(f, rename_rom(path, f)))41if __name__ == '__main__':42 if len(sys.argv) == 2:43 path = sys.argv[1]44 else:45 path = os.getcwd()46 print('Set path to "{}"'.format(path))...
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!!