Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

This is the 3rd time that I post this question, so if you're not sure please don't answer This should be done using python function

This is the 3rd time that I post this question, so if you're not sure please don't answer

This should be done using python function not by hand

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.

To implement this, write a function camel_case(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)

(code: is a multiline input string )

How to convert a given function to camel case in python? for example

def sum_two_digits(a, b): return a + b print(sum_two_digits(10, 20))

THE potential function should process the above function and return it in camel case like below:

def SumTwoDigit(a, b): return a + b print(SumTwoDigit(10, 20))

the nested dictionary is

The nested dictionary is given by d = {'sum_two_digits': {'hash':-9214996652071026704, 'camelcase':'SumTwoDigits', 'allcaps':'SUM_TWO_DIGITS'} }

If the function name starts with _ like

_sum_digits(a,b)

it should be converted to _SumDigits(a,b)

if the function name is already in camel case leave it as its.

function needs to return the tuple (d, newcode): d is 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's hash function) o camelcase: camel case version of original function name o allcaps: all caps version of original function name newcode is 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).

WITH EXPLAINTATION PLEASE

If you run camel_case(code) , the following testcases will be tested

if __name__ == '__main__': # Example 1 testcases = { 'example 1': """ def add_two_numbers(a, b): return a + b

print(add_two_numbers(10, 20)) """, 'example 2' : """ 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) """ } for key, code in testcases.items(): print(f'--- {key} ---') out = function_renamer(code) if not isinstance(out, tuple) or len(out)!=2: raise TypeError('function_renamer should return a tuple of length 2') d, newcode = out if not isinstance(d, dict): raise TypeError('return argument d should be a dictionary') if not isinstance(newcode, str): raise TypeError('return argument code should be a string') print('d = ', d) print(' code:') print(newcode)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Understanding Databases Concepts And Practice

Authors: Suzanne W Dietrich

1st Edition

1119827949, 9781119827948

More Books

Students also viewed these Databases questions

Question

=+ Are unions company-wide, regional, or national?

Answered: 1 week ago