Python : Using Decorators.

Decorators in Python (resemble the design pattern “decorator”) enhance the functionality of an existing function without modifying the function code.
Decorators achieve this by wrapping the function within its own code block.

Example:

Python Decorators used to find the time taken to execute a function.

Python3 provides

  • time module to get the current time.
    time.time_ns() → int returns time as an integer number of nanoseconds since the epoch.

Python3 : Program for finding the function execution time.

import time
from typing import List

def Find_Squares (number_list) -> List[int] :
    start = time.time_ns() # Returns time as integer nano seconds since epoch.
    result = []
    for num in number_list :
        result.append (num * num)
    end = time.time_ns()
    print ("Find_Squares took : " + str ((end-start)) + " nano seconds.")
    return result

def Find_Cubes (number_list) -> List[int] :
    start = time.time_ns() # Returns time as integer nano seconds since epoch.
    result = []
    for num in number_list :
        result.append (num * num * num)
    end = time.time_ns()
    print ("Find_Cubes took : " + str ((end-start)) + " nano seconds.")
    return result

def main() :
    numbers = range(10)
    result = Find_Squares (numbers)
    print (result)
    result = Find_Cubes (numbers)
    print (result)

if __name__ == "__main__" :
    main()

Output

Find_Squares took : 1703 nano seconds.
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
Find_Cubes took : 1775 nano seconds.
[0, 1, 8, 27, 64, 125, 216, 343, 512, 729]
import time
from typing import List

def Time_It (func) :

    def Wrapper (*args, **kwargs) :
        print ("Function " + func.__name__ + " got arguments : " + str(args[0]))
        start = time.time_ns() # Returns time as integer nano seconds since epoch.
        result = func (*args, **kwargs)
        end = time.time_ns()
        print ("Function " + func.__name__ + " took : " + str ((end-start)) + " nano seconds.")
        return result

    return Wrapper

@Time_It
def Find_Squares (number_list) -> List[int] :
    result = []
    for num in number_list :
        result.append (num * num)
    return result

@Time_It
def Find_Cubes (number_list) -> List[int] :
    result = []
    for num in number_list :
        result.append (num * num * num)
    return result

def main() :
    print ("Result : " + str(Find_Squares(range(2,21))))
    print ("Result : " + str(Find_Cubes(range(3,31))))

if __name__ == "__main__" :
    main()

Output

Function Find_Squares got arguments : range(2, 21)
Function Find_Squares took : 4091 nano seconds.
Result : [4, 9, 16, 25, 36, 49, 64, 81, 100, 121, 144, 169, 196, 225, 256, 289, 324, 361, 400]
Function Find_Cubes got arguments : range(3, 31)
Function Find_Cubes took : 8015 nano seconds.
Result : [27, 64, 125, 216, 343, 512, 729, 1000, 1331, 1728, 2197, 2744, 3375, 4096, 4913, 5832, 6859, 8000, 9261, 10648, 12167, 13824, 15625, 17576, 19683, 21952, 24389, 27000]


Copyright (c) 2019-2023, Algotree.org.
All rights reserved.