100% found this document useful (4 votes)
14K views5 pages

Decorators 2txt

1. The document defines several decorator functions that add formatting to other functions, such as adding bold or italic HTML tags. It also shows how to chain multiple decorators together. 2. Higher order functions and closures are demonstrated through a detecter function that returns an inner function to check if an element is in a sequence, and a factory function that returns two functions to get and increment a number. 3. Decorators, higher order functions, and closures allow for abstraction and reuse of common functionality across multiple functions.

Uploaded by

Senthil Lakshmi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
100% found this document useful (4 votes)
14K views5 pages

Decorators 2txt

1. The document defines several decorator functions that add formatting to other functions, such as adding bold or italic HTML tags. It also shows how to chain multiple decorators together. 2. Higher order functions and closures are demonstrated through a detecter function that returns an inner function to check if an element is in a sequence, and a factory function that returns two functions to get and increment a number. 3. Decorators, higher order functions, and closures allow for abstraction and reuse of common functionality across multiple functions.

Uploaded by

Senthil Lakshmi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 5

===================================================================================

===================================================================================
===================================================================================
===================================================================================
====================================================
Python 3 Handson

=================================================================================

1. define a decorator function log which logs information on a function and the
arguments passed to it
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)
2. define a decorator function log which logs information on a function and the
arguments passed to it
for average function
def log(func):
def inner(*args, **kwdargs):
str_template = "Accessed the function -'{}' with arguments {}
{}".format(func.__name__,

args,

kwdargs)
return str_template + "\n" + str(func(*args, **kwdargs))
return inner

#Add greet function definition here


@log
def average(n1,n2,n3):
return (n1+n2+n3)/3

3. Define a decorator function bold_tag which adds bold html tags <b> ...</b> to
return the value of some other function
mport sys
import os

#Define and implement bold_tag


def bold_tag(func):

def inner(*args, **kwdargs):


return '<b>'+func(*args, **kwdargs)+'</b>'
return inner

def say(msg):
return msg

say=bold_tag(say)

'''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(say(input()))
fout.write("{}".format(*res_lst))

4. Define a decorator function italic_tag which adds italic htmls tags <i> ...</i>
to return the value of some other function
import os
import sys

#Implement italic_tag below


def italic_tag(func):

def inner(*args, **kwdargs):


return '<i>'+func(*args, **kwdargs)+'</i>'

return inner

def say(msg):
return msg
say=italic_tag(say)
'''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(say(input()))
fout.write("{}".format(*res_lst))
5. Define a function greet which accepts a string and returns italic html tags
import os
import sys

def bold_tag(func):

def inner(*args, **kwdargs):


return '<b>'+func(*args, **kwdargs)+'</b>'

return inner

def italic_tag(func):

def inner(*args, **kwdargs):


return '<i>'+func(*args, **kwdargs)+'</i>'
return inner

#Add greet() function definition


@italic_tag
def greet():
msg=input()
return msg
'''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())
fout.write("{}".format(*res_lst))

6.Chain the two decorators created earlier, with greet function as shown in the
following code
import os
import sys

def bold_tag(func):

def inner(*args, **kwdargs):


return '<b>'+func(*args, **kwdargs)+'</b>'

return inner

def italic_tag(func):

def inner(*args, **kwdargs):


return '<i>'+func(*args, **kwdargs)+'</i>'

return inner

#Add greet() function definition


@italic_tag
@bold_tag
def greet():
msg=input()
return msg
'''check Tail section below for input / output'''

if __name__ == "__main__":
with open(os.environ['OUTPUT_PATH'], 'w') as fout:
res_lst = list()
res_lst.append(greet())
fout.write("{}".format(*res_lst))

Higher order functions and closures


1. Define a function named detecter with one parameter element, which returns an
inner function isIn
isIn accepts a parameter sequence, and returns True or False after checking the
presence of element in sequence
create two closure functions detect30 and detect45 usign detecter. Pass 30 and 45
respectively for implementations
def detecter (element):
e=element
def isIn(sequence):
a=0
for i in sequence:

if int(i)==e :
a=1

if a==1:
return True
else:
return False
return isIn

detect30 = detecter(30) # 'c1' is a closure

detect45 = detecter(45) # 'c2' is a closure

print(detect30)
print(detect30)
2. define a function factory, with a variable n initialized to zero and two inner
functions current and counter
current must return the current value of n
counter must increment the value of n by one
#!/bin/python3

import sys
import os

# Add the factory function implementation here


def factory(v):
n=0
def current():
n=v
print(n)
return n

def counter():

n = v + 1
print(n)
return n

return current, counter

f_current, f_counter = factory(int(input()))


print(f_current(),f_counter())

if __name__ == "__main__":
with open(os.environ['OUTPUT_PATH'], 'w') as fout:
func_lst = [f_current, f_counter]
res_lst = list()
for func in func_lst:
res_lst.append(func())
fout.write("{}\n{}".format(*res_lst))
===================================================================================
===================================================================================
===================================================================================
===================================================================================
=====================================

You might also like