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...
Python Write a function str_slice(s) to slice sentence into single word by their spaces and punctuations...
Python Write a function str_slice(s) to slice sentence into single word by their spaces and punctuations and returns a list. You cannot use split(), join() method for this define function Example: >>>str_slice(‘Hi, I like it!!’) [‘Hi’, ‘, ’, 'I', ‘ ‘, ‘like’, ‘ ‘, ‘it’, ‘!!’]
To be done in Python 3.7 Some Web sites impose certain rules for passwords. Write a...
To be done in Python 3.7 Some Web sites impose certain rules for passwords. Write a function that checks whether a string is a valid password. Suppose the password rules are as follows: A password must have at least eight characters. A password must consist of only letters and digits. A password must contain at least two digits. Write a program that prompts the user to enter a password and displays valid password if the rules are followed or invalid...
Write the following Python script: Write a function called linsolve() that will be useful in categorizing...
Write the following Python script: Write a function called linsolve() that will be useful in categorizing and solving linear algebra problems. The function will have up to three parameters: • A required 2D array representing the coefficient matrix of a linear algebra equation, • A required 1D or 2D array representing the right-side constants of a linear algebra equations, and • An optional parameter used to determine which condition number to use in determining the condition of the system. The...
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")...
Write a Python module that must satisfy the following- Define a function named rinsert. This function...
Write a Python module that must satisfy the following- Define a function named rinsert. This function will accept two arguments, the first a list of items to be sorted and the second an integer value in the range 0 to the length of the list, minus 1. This function shall insert the element corresponding to the second parameter into the presumably sorted list from position 0 to one less than the second parameter’s index.   Define a function named rinsort. This...
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....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT