Question

In: Computer Science

Write a decorator function that has the following property: 1.  The name of a decorator is uppercase...

Write a decorator function that has the following property:

1.  The name of a decorator is uppercase and takes a function as input.

2.  The decorator has an inner function called wrapper with no input argument.

3.  The wrapper function has two local variables: original and modified.

4.  The input function argument of decorator gets assigned to the original variable inside the wrapper function and the upper case version of the input function gets assigned to the modified variable.

5.  The wrapper function returns the modified variable.

6.  The decorator returns the wrapper (per the usual of the inner functions in decorators)

7.  Write a new function called greetings that prints “Hello”.

8.  Decorate the greetings function with the uppercase decorator function.

9.  Run the program by invoking the greetings function. Record the output and include it with your code.

10. Save the decorator uppercase as a separate Python file and import it to a new file that defines the greetings function as a decorated function that simply prints the “Hello” message.

11. Generate the output by invoking the greetings function.

Solutions

Expert Solution

'''

Python version : 2.7

uppercase_decorator.py : Python program to create a decorator function uppercase

'''

# decorator function uppercase that takes a function as input.

def uppercase(fun):

              

               # inner function called wrapper with no input argument.

               def wrapper():

                              # original assigned to the input function argumnet

                              original = fun()

                              # uppercase version of the function gets assigned to modified

                              modified = original.upper()

                              # return the modified variable

                              return modified

               # return the wrapper

               return wrapper

#end of program             

Code Screenshot:

'''

Python version : 2.7

uppercase_implement.py : Python program to implement the decorator function

'''

from uppercase_decorator import uppercase

# function greetings that is used to print hello

@uppercase           

def greetings ():

               return "Hello!"  

#call the greetings function         

greetings()

#end of program

Code Screenshot:

Output:


Related Solutions

Write a Data Element Class named Property that has fields tohold the property name, the...
Write a Data Element Class named Property that has fields to hold the property name, the city where the property is located, the rent amount, the owner's name, and the Plot to be occupied by the property, along with getters and setters to access and set these fields. Write a parameterized constructor (i.e., takes values for the fields as parameters) and a copy constructor (takes a Property object as the parameter). Follow the Javadoc file provided.Write a Data Element Class...
Write the following easy Python functions: 1) Write the function named roundDollars(). The function has one...
Write the following easy Python functions: 1) Write the function named roundDollars(). The function has one input, a String named amountStr which consists of a dollar-formatted amount, such as "$ 1,256.86". The returned value is an int representing the number of rounded "dollars" in the amount, (1257 in the sample shown here). You will need to scrub, format and parse the input, then use arithmetic to determine how many rounded dollars the amount contains. roundDollars("$ 1,256.86") → 1257 roundDollars("$ 0.42")...
Python Programming 1. Write a function name split_name which takes one parameter called name. If the...
Python Programming 1. Write a function name split_name which takes one parameter called name. If the name parameter value is a name in the form LastName, FirstName(e.g., ”Grounds, Nic”) then the function should return a list of two elements where FirstName is the first element and LastName is the second element. If the name parameter value is a name in the form FirstName LastName(e.g., ”Nic Grounds”) then the function should return a list of two elements where FirstName is the...
Write assembly programs with the following I/O Enter a sentence: Project No Three. No. of uppercase...
Write assembly programs with the following I/O Enter a sentence: Project No Three. No. of uppercase letters = 3 No. of lowercase letters = 11 You can use c++ for the Inputting and outputting
In ArcGIS, What is conforming property? Name something that has this property and something that does...
In ArcGIS, What is conforming property? Name something that has this property and something that does not-
Using C#: Write a class named Employee that has the following properties: Name - The Name...
Using C#: Write a class named Employee that has the following properties: Name - The Name property holds the employee's name IdNumber - The IdNumber property holds the employee's ID number Department - The Department property holds the name of the department in which the employee works Position - The Position property holds the employee's job title The class should have the following overloaded constructors: A constructor that accepts the following values as arguments and assigns them to the appropriate...
Use Python for this quetions: Write a python functions that use Dictionary to: 1) function name...
Use Python for this quetions: Write a python functions that use Dictionary to: 1) function name it addToDictionary(s,r) that take a string and add it to a dictionary if the string exist increment its frequenc 2) function named freq(s,r) that take a string and a record if the string not exist in the dictinary it return 0 if it exist it should return its frequancy.
Write the following classes: Class Entry: 1. Implement the class Entry that has a name (String),...
Write the following classes: Class Entry: 1. Implement the class Entry that has a name (String), phoneNumber (String), and address (String). 2. Implement the initialization constructor . 3. Implement the setters and getters for all attributes. 4. Implement the toString() method to display all attributes. 5. Implement the equals (Entry other) to determine if two entries are equal to each other. Two entries are considered equal if they have the same name, phoneNumber, and address. 6. Implement the compareTo (Entry...
Write a Python application to find the longest ‘A’ path on a map of capital (uppercase)...
Write a Python application to find the longest ‘A’ path on a map of capital (uppercase) letters. The map is represented as a matrix (2-dimensional list) of capital letters. Starting from any point, you can go left, right, up and down (but not diagonally). A path is defined as the unbroken sequence of letters that only covers the spots with the letter A. The length of the A path is defined as the number of ‘A’s on the path. Your...
Write short notes on the following concepts; Property right Concave function Diminishing marginal rate of substitution...
Write short notes on the following concepts; Property right Concave function Diminishing marginal rate of substitution Mixed strategy Contract curve Marginal physical products.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT