Question: You are working on a large-scale software project involving thousands of different Python files. The files have been written by different programmers and the naming

You are working on a large-scale software project involving thousands of different Python files. The files have been written by different programmers and the naming of functions is somewhat inconsistent. You receive a new directive stating that the function names all have to be in camel case. In camel case, function names consisting of multiple words have a capital letter in each word and most underscores are removed. For instance, def MyArithmeticCalculator is in camel case but def my_arithmetic_calculator is not in camel case. You want to write a Python program that automatically processes Python code and renames the function names instead of doing it by hand. The instruction states that: 1. All functions names need to be changed to camel case. E.g. a function calculate_speed_of_vehicle needs to be renamed to CalculateSpeedOfVehicle. 2. If the function has one or more leading '_' (underscores) they need to be preserved. All other underscores need to be removed. E.g. a function __calc_size is renamed to __CalcSize. 3. If the function is already in camel case, you do not need to change it but it still needs to appear in the dictionary d specified below. 4. You can assume that there will be no name clashes. That is, a given function name which is not in camel case will not already appear in camel case elsewhere. E.g. if there is a function print_all_strings then there is no function PrintAllStrings elsewhere in the code. 5. Tip: You can use regular expressions to find the function names. To implement this, write a function function_renamer(code) that takes as input a string code that represents the Python code. It is typically a multi-line Python string. Your function needs to return the tuple (d, newcode): dis a nested dictionary where each key corresponds to the original function name. The value is a nested dictionary that has the following items: o hash: hash code of the original function name (tip: use Python'shashfunction) o camelcase: camel case version of original function name o allcaps: all caps version of original function name newcodeis a string containing the code wherein all function names have been renamed by their camel case versions. Note that you need to change the function name and also all other locations where the function name is used (e.g. function calls). You should not change anything else in the code (e.g. the contents of any strings). To clarify this, a few examples are given next. Example 1: Assume your input code is the multi-line string def add_two_numbers(a, b): return a + b print(add_two_numbers(10, 20)) After processing it with your function, the code should be changed to def AddTwoNumbers(a, b): return a + b print(AddTwoNumbers(10, 20)) The nested dictionary is given by d = {'add_two_numbers': {'hash':-9214996652071026704, 'camelcase':'AddTwoNumbers', 'allcaps':'ADD_TWO_NUMBERS'} } Example 2: Assume your input code is the multi-line string def _major_split(*args): return (args[:2], args[2:]) def CheckTruth(t = True): print('t is', t) return _major_split([t]*10) x, y = _major_split((10, 20, 30, 40, 50)) CheckTruth(len(x) == 10) After processing it with your function, the code should be changed to def _MajorSplit(*args): return (args[:2], args[2:]) def CheckTruth(t = True): print('t is', t) return _MajorSplit([t]*10) x, y = _MajorSplit((10, 20, 30, 40, 50)) CheckTruth(len(x) == 10) The nested dictionary is given by d = {'CheckTruth': {'hash':-6410081306665365595, 'camelcase':'CheckTruth', 'allcaps':'CHECKTRUTH'}, '_major_split': {'hash':484498917506710667, 'camelcase':'_MajorSplit', 'allcaps':'_MAJOR_SPLIT'} } Testbed: If you run function_renamer in the testbed, the following testcases will be tested: - Example 1 (see above) - Example 2 (see above)

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!