<jupyter_start><jupyter_text>25 - Advanced Exercises*   Decorators  Function review exercises  🎎🎎🎎1.assign **```myfuction```** to a new variable, then access the function from that variable and print it using inputs 2 and 3.<jupyter_code># Write your own code in this cell
def myfunction(a, b):
    return a + b
  = myfunction
print()<jupyter_output><empty_output><jupyter_text>🎎🎎🎎2.Define a function named **```inner_function```** that is nested within another function named **```outer_function```**.try to execute the inner_function outside of the outer_function . What error do you encounter and why?<jupyter_code># Write your own code in this cell<jupyter_output><empty_output><jupyter_text>🎎🎎🎎3.Add a line of code to the functions below to make **```factorial_10()```** callable.<jupyter_code># Write your own code in this cell
import math
def outer_function(n):
    description = f'Calculating the factorial of {n}.'
    def inner_function():
        print(description)
        print(f"Result = {math.factorial(n)}")


factorial_10 = outer_function(10)
factorial_10()<jupyter_output><empty_output><jupyter_text>🎎🎎🎎4.Complete the following functions to allow them to call **```myreminder```** function with **```do```** function as an argument.<jupyter_code># Write your own code in this cell
def myreminder(    ):
    
    print('Remember to bring your glasses!')

def do():
    print('I\'m going to read a book.')

myreminder(do)<jupyter_output><empty_output><jupyter_text>Decorator exercises  🎎🎎5.Create a decorator called **```retry```** that repeats the functions a given number of times and at a particular interval(In seconds).Take a keyword argument for the number of iterations **"repeat"** and a keyword argument for the distance between iterations **"interval"**. (Using the **```time.sleep(n)```** method, you can delay n seconds until the next line is executed)<jupyter_code># Write your own code in this cell
import math
import time

def retry(func):


@retry
def factorial_calculation(n):
    r = math.factorial(n)
    print(r)

factorial_calculation(10, repeat=3, interval=1)<jupyter_output><empty_output><jupyter_text>🎎6.Write a decorator named **```log```** that adds the function name , function execution time and date  and execution result to a file in the **"./Files/decorators/log.txt"** path. **```the sample contents of a log file:```**```The factorial_calculation function was executed at 11/15/2021, 19:37:30 with input 6 and output 720.The factorial_calculation function was executed at 11/15/2021, 19:37:34 with input 7 and output 5040.The factorial_calculation function was executed at 11/15/2021, 19:37:37 with input 8 and output 40320.The factorial_calculation function was executed at 11/15/2021, 19:37:41 with input 13 and output 6227020800.The factorial_calculation function was executed at 11/15/2021, 19:37:46 with input 24 and output 620448401733239439360000```<jupyter_code># Write your own code in this cell
import math
import time
from pathlib import Path

def log(func):
    def wraper(*args, **kwargs):

        mytuple = time.localtime() 
        time_string = time.strftime("%m/%d/%Y, %H:%M:%S", mytuple)


@log
def factorial_calculation(n):
    r = math.factorial(n)
    print(r)
    return r<jupyter_output><empty_output><jupyter_text>🎎🎎🎎7.Create a decorator that prints the execution time of a function in seconds. Use **time.time()** method  to calculate how long it takes to execute a function. Record the time before and after the execution of the function, then subtract them to calculate execution time.Can you add the amount of ram used to this decorator?<jupyter_code># Write your own code in this cell
import time

def timeit(func):
    
    
@timeit
def factorial_calculation(n):
    r = math.factorial(n)
    print(f"{n}! = {r}")

factorial_calculation(1)
factorial_calculation(10)
factorial_calculation(20)<jupyter_output><empty_output><jupyter_text>🎎🎎8.Function **```get_coin_price```** takes as input the symbol of each cryptocurrency and displays the current price.Create a decorator that allows you to give function **```get_coin_price```** multiple inputs instead of just one.<jupyter_code># Write your own code in this cell
import urllib
import json

def multiple_inputs(func):
    
    
    
    

@multiple_inputs
def get_coin_price(coin):
    url = f"https://data.messari.io/api/v1/assets/{coin}/metrics"
    response=urllib.request.urlopen(url)
    if response.getcode() == 200:
        myjson = response.read()
        mydict = json.loads(myjson)
        price = mydict["data"]["market_data"]["price_usd"]
        coin_name = mydict["data"]['name']
        print(f"{coin_name} price = {price:0.5f}")
    else:
        print("An error has occurred!")     


        
get_coin_price('btc')
print("---------------------------")
get_coin_price('btc', 'ada', 'xrp', 'link')<jupyter_output><empty_output>
StarcoderdataJupyterStructuredCleanDedup