0% found this document useful (0 votes)
212 views4 pages

This Study Resource Was

The document discusses Python decorators and higher order functions. It defines several decorator functions like log, bold_tag, italic_tag that add logging or HTML tags to other functions. It also defines a detecter function that returns a closure to check for an element in a sequence, and a factory function that returns current and counter closures to increment and return a value.

Uploaded by

yihinaf
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
212 views4 pages

This Study Resource Was

The document discusses Python decorators and higher order functions. It defines several decorator functions like log, bold_tag, italic_tag that add logging or HTML tags to other functions. It also defines a detecter function that returns a closure to check for an element in a sequence, and a factory function that returns current and counter closures to increment and return a value.

Uploaded by

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

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

m
def log(func):

er as
def inner(*args, **kwdargs):

co
str_template = "Accessed the function -'{}' with arguments {}

eH w
{}".format(func.__name__,

o.
args, rs e
ou urc
kwdargs)
return str_template + "\n" + str(func(*args, **kwdargs))
return inner
o

#Add greet function definition here


aC s

@log
v i y re

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
ed d

mport sys
ar stu

import os

#Define and implement bold_tag


def bold_tag(func):
sh is

def inner(*args, **kwdargs):


Th

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()

This study source was downloaded by 100000825773299 from CourseHero.com on 06-16-2021 22:48:38 GMT -05:00

https://www.coursehero.com/file/51450869/decoratorstxt/
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)

m
'''Check the Tail section for input/output'''

er as
co
if __name__ == "__main__":

eH w
with open(os.environ['OUTPUT_PATH'], 'w') as fout:
res_lst = list()

o.
res_lst.append(say(input()))
rs e
fout.write("{}".format(*res_lst))
ou urc
5. Define a function greet which accepts a string and returns italic html tags
import os
import sys
o

def bold_tag(func):
aC s
v i y re

def inner(*args, **kwdargs):


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

return inner
ed d

def italic_tag(func):
ar stu

def inner(*args, **kwdargs):


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

return inner
sh is

#Add greet() function definition


Th

@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

This study source was downloaded by 100000825773299 from CourseHero.com on 06-16-2021 22:48:38 GMT -05:00

https://www.coursehero.com/file/51450869/decoratorstxt/
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

m
'''check Tail section below for input / output'''

er as
co
if __name__ == "__main__":

eH w
with open(os.environ['OUTPUT_PATH'], 'w') as fout:
res_lst = list()

o.
res_lst.append(greet()) rs e
fout.write("{}".format(*res_lst))
ou urc
Higher order functions and closures
1. Define a function named detecter with one parameter element, which returns an
inner function isIn
o

isIn accepts a parameter sequence, and returns True or False after checking the
aC s

presence of element in sequence


v i y re

create two closure functions detect30 and detect45 usign detecter. Pass 30 and
45 respectively for implementations
def detecter (element):
e=element
ed d

def isIn(sequence):
ar stu

a=0
for i in sequence:

if int(i)==e :
a=1
sh is
Th

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

This study source was downloaded by 100000825773299 from CourseHero.com on 06-16-2021 22:48:38 GMT -05:00

https://www.coursehero.com/file/51450869/decoratorstxt/
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

m
er as
co
f_current, f_counter = factory(int(input()))

eH w
print(f_current(),f_counter())

o.
if __name__ == "__main__": rs e
with open(os.environ['OUTPUT_PATH'], 'w') as fout:
ou urc
func_lst = [f_current, f_counter]
res_lst = list()
for func in func_lst:
res_lst.append(func())
o

fout.write("{}\n{}".format(*res_lst))
aC s
v i y re
ed d
ar stu
sh is
Th

This study source was downloaded by 100000825773299 from CourseHero.com on 06-16-2021 22:48:38 GMT -05:00

https://www.coursehero.com/file/51450869/decoratorstxt/
Powered by TCPDF (www.tcpdf.org)

You might also like