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 3.7: Give the following sample code, write the following programs: • An iterator class called...
Python 3.7: Give the following sample code, write the following programs: • An iterator class called Iteratefirstn that implements __iter__() and __next__() methods where next method should raise an exception for the cur index if it is passed the last element otherwise set cur index to the next index and advance the index by one, i.e. cur, num = num, num+1. Instantiate the class to calculate the sum of 1000000 elements similar to the example. • Instead of implementing the...
Please use the python 3.7 for this assigments Q1 Write a program that accepts the lengths...
Please use the python 3.7 for this assigments Q1 Write a program that accepts the lengths of three sides of a triangle as inputs. The program output should indicate whether or not the triangle is an equilateral triangle. Q2 Write a program that accepts the lengths of three sides of a triangle as inputs. The program output should indicate whether or not the triangle is a right triangle. Recall from the Pythagorean theorem that in a right triangle, the square...
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")...
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....
Python 3.7: 1. Write a generator expression that repeats each character in a given string 4...
Python 3.7: 1. Write a generator expression that repeats each character in a given string 4 times. i.e. given “gone”, every time you call the next method on the generator expression it will display “gggg”, then “oooo”, … etc and instantiate it to an object called G.  G = (…..) # generatorExpression object 2. Test and verify that iter(G) and G are the same things. What does this tell you about the nature of a Generator object/expression? 3. Assign Iter(G) to...
#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. >>>...
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!'.
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT