Question

In: Computer Science

Python 3.7: Write the following decorators and apply them to a single function (applying multiple decorators...

Python 3.7:

Write the following decorators and apply them to a single function (applying multiple decorators to a single function):

1. The first decorator is called strong and has an inner function called wrapper. The purpose of this decorator is to add the html tags of <strong> and </strong> to the argument of the decorator. The return value of the wrapper should look like: return “<strong>” + func() + “</strong>”
2. The decorator will return the wrapper per usual.
3. The second decorator is called emphasis and has an inner function called wrapper. The purpose of this decorator is to add the html tags of <em> and </em> to the argument of the decorator similar to step 1. The return value of the wrapper should look like: return “<em>” + func() + “</em>.
4. Use the greetings() function in problem 1 as the decorated function that simply prints “Hello”.
5. Apply both decorators (by @ operator to greetings()).
6. Invoke the greetings() function and capture the result.

Solutions

Expert Solution

'''

Python version : 3.7

decorator.py : Python program to create a decorator functions strong and emphasis

'''           

# decorator function strong that takes a function as input.

def strong(func):

              

               # inner function called wrapper with no input argument.

               def wrapper():

                              #adds the html tags <strong> </strong> to the argument of the decorator and returns it

                              return "<strong>"+func()+"</strong>"

              

               return wrapper  

# decorator function emphasis that takes a function as input.        

def emphasis(func):

              

               # inner function called wrapper with no input argument.

               def wrapper():

                              #adds the html tags <em> </em> to the argument of the decorator and returns it

                              return "<em>"+func()+"</em>"

                             

               return wrapper

#end of decorator.py

Code Screenshot:

'''

Python version : 3.7

decorator_implement.py : Python program to implement the decorator functions

'''

import decorator

# function greetings that is used to return Hello

@decorator.strong

@decorator.emphasis

def greetings ():

               return "Hello"    

#end of decorator_implement.py

Code Screenshot:

Output:


Related Solutions

USING PYTHON 3.7 AND USING def functions. Write a function called GPA that calculates your grade...
USING PYTHON 3.7 AND USING def functions. Write a function called GPA that calculates your grade point average (GPA) on a scale of 0 to 4 where A = 4, B = 3, C = 2, D = 1, and F = 0. Your function should take as input two lists. One list contains the grades received in each course, and the second list contains the corresponding credit hours for each course. The output should be the calculated GPA. To...
(IN PYTHON) Write a function that accepts a line of text and a single letter as...
(IN PYTHON) Write a function that accepts a line of text and a single letter as input (case insensitive) and returns the number of times the letter is the last character of a word. Note your program should be able to handle different cases. And check if the user input is a single letter.
#Python 5. Write function called evaluate() that evaluates the following Python expressions or assignments as specified:...
#Python 5. Write function called evaluate() that evaluates the following Python expressions or assignments as specified: Request input from the user for three variables (floating-point or integer) x, y, z, and myAverage. If the average of the first three numbers equals the fourth number, print 'Your average is correct.'. If not print 'Your average is not correct'. and print the correct average. Print the largest value among x, y, and z. Print the minimum value of x, y, y. >>>...
LANGUAGE PYTHON 3.7 Write a collection class named "Jumbler". Jumbler takes in an optional list of...
LANGUAGE PYTHON 3.7 Write a collection class named "Jumbler". Jumbler takes in an optional list of strings as a parameter to the constuctor with various strings. Jumbler stores random strings and we access the items based on the methods listed below. Jumbler supports the following methods: add() : Add a string to Jumbler get() : return a random string from Jumbler max() : return the largest string in the Jumbler based on the length of the strings in the Jumbler....
In python please write the following code the problem. Write a function called play_round that simulates...
In python please write the following code the problem. Write a function called play_round that simulates two people drawing cards and comparing their values. High card wins. In the case of a tie, draw more cards. Repeat until someone wins the round. The function has two parameters: the name of player 1 and the name of player 2. It returns a string with format '<winning player name> wins!'. For instance, if the winning player is named Rocket, return 'Rocket wins!'.
how to write a unit test for a write function in python? is there a better...
how to write a unit test for a write function in python? is there a better way than running main once and checking whether the new file path exists?
In python 3.7: You’re going to program a simulation of the following game. Like many probability...
In python 3.7: You’re going to program a simulation of the following game. Like many probability games, this one involves an infinite supply of ping-pong balls. No, this game is "not quite beer pong." The balls are numbered 1 through N. There is also a group of N cups, labeled 1 through N, each of which can hold an unlimited number of ping-pong balls (all numbered 1 through N). The game is played in rounds. A round is composed of...
Using Python write a function that implements the following two-dimensional objective function: F (x, y) =...
Using Python write a function that implements the following two-dimensional objective function: F (x, y) = (x^2 + y − 11)^2 + (x + y^2 − 7 )^22 . Determine how many local minimums and maximums and their location for this objective function.
PYTHON: Write a function insertInOrder that takes in a list and a number. This function should...
PYTHON: Write a function insertInOrder that takes in a list and a number. This function should assume that the list is already in ascending order. The function should insert the number into the correct position of the list so that the list stays in ascending order. It should modify the list, not build a new list. It does not need to return the list, because it is modifying it.   Hint: Use a whlie loop and list methods lst = [1,3,5,7]...
Write a Python function that takes a list of string as arguments. When the function is...
Write a Python function that takes a list of string as arguments. When the function is called it should ask the user to make a selection from the options listed in the given list. The it should get input from the user. Place " >" in front of user input. if the user doesn't input one of the given choices, then the program should repeatedly ask the user to pick from the list. Finally, the function should return the word...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT