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...
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 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...
Please solve the following problem for MATLAB Write a user-defined function (name it F to C)...
Please solve the following problem for MATLAB Write a user-defined function (name it F to C) that converts temperature in degrees F to temperature in degrees C. Use the function to solve the following problem. The change in the length of an object, ∆L, due to a change in the temperature, ∆T, is given by: ∆L = αL∆T, where a is the coefficient of thermal expansion. Determine the change in the area of a rectangular(4.5 m by 2.25m) aluminum (α=23·10-61/˚C)...
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.
Write a pseudocode for the following code: /*every item has a name and a cost */...
Write a pseudocode for the following code: /*every item has a name and a cost */ public class Item {    private String name;    private double cost;    public Item(String name, double cost) {        this.name = name;        this.cost = cost;    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public double getCost() {        return cost;...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT