Python Decorators - 1 - Hands On
Python / Python Classes and Objects
1618
1. Decorators - 1
1. Define a decorator function log which logs information about a function and the arguments passed to it.
2. Ensure that the log message is written to STDOUT.
3. Use the Test against custom input box to output the result for debugging.
4. Provide a text string (refer test case samples) to display the output/error.
Given Input:
hello world!
Expected Output:
Accessed the function -'greet' with arguments ('hello world!',) {}
Program:
#!/bin/python3
import sys
import os
import datetime as dt
#Add log function and inner function implementation here
def log(func):
def inner(*args, **kwdargs):
STDOUT= "Accessed the function -'{}' with arguments {} {}".format(func.__name__,args,kwdargs)
return STDOUT
return inner
def greet(msg):
'Greeting Message : ' + msg
greet = log(greet)
'''Check the Tail section for input/output'''
if __name__ == "__main__":
with open(os.environ['OUTPUT_PATH'], 'w') as fout:
res_lst = list()
res_lst.append(greet(str(input())))
fout.write("{}".format(*res_lst))
Output:
Accessed the function -'greet' with arguments ('Python is awesome',) {}
This Particular section is dedicated to Programs only. If you want learn more about Python. Then you can visit below links to get more depth on this subject.
# C Tutorials
# JAVA Tutorials
# HTML Tutorials
# Computer Fundamental
# Data Structure
# DBMS Tutorials
SQL
# C# Language
# R Language
# PHP
# Python
# Vue JS
Accessed the function -'greet' with arguments ('hello world!',) {}
Program:
#!/bin/python3 import sys import os import datetime as dt #Add log function and inner function implementation here def log(func): def inner(*args, **kwdargs): STDOUT= "Accessed the function -'{}' with arguments {} {}".format(func.__name__,args,kwdargs) return STDOUT return inner def greet(msg): 'Greeting Message : ' + msg greet = log(greet) '''Check the Tail section for input/output''' if __name__ == "__main__": with open(os.environ['OUTPUT_PATH'], 'w') as fout: res_lst = list() res_lst.append(greet(str(input()))) fout.write("{}".format(*res_lst))
Output:
Accessed the function -'greet' with arguments ('Python is awesome',) {}
This Particular section is dedicated to Programs only. If you want learn more about Python. Then you can visit below links to get more depth on this subject.
# C Tutorials
# JAVA Tutorials
# HTML Tutorials
# Computer Fundamental
# Data Structure
# DBMS Tutorials
SQL
# C# Language
# R Language
# PHP
# Python
# Vue JS