Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Write A Separate Logging Decorator That Saves All Function Calls And Their Results In The File Log.Txt. This Requires Opening The File For Appending: Logfile

Write A Separate Logging Decorator That Saves All Function Calls And Their Results In The File \"Log.Txt\". This Requires Opening The File For Appending: Logfile = Open('Log.Txt', 'A') You Will Need The Wrapper Function To Open The File For Each Function Call, Write The Information, And Close The File. When You Decorate Fib With Only The Log Decorator, Log.Txt

,){} found in cache (7,){} found in cache (8,){} found in cache 55, calls = 11 from the trace decorator, and log.txt should contain: fib((1,){}) = 1 fib((0,){}) = 0 fib((2,){}) = 1 fib((3,){}) = 2 fib((4,){}) = 3 fib((5,){}) = 5 fib((,){}) = 8 fib((7,){}) = 13 fib((8,){}) = 21 fib((9,){}) = 34 fib((10,){}) = 55 = Submit your log.txt and dec.py for the latter, composed-decorated case. PS - Note the order of the composition: @track @log def fib(n): def track(f): def wrapper(*args, **kwargs) : wrapper.count += 1 print(args, kwargs, \"found in cache\") return f(*args, **kwargs) wrapper.count = 0 return wrapper

output below: @log def fib(n): return n if n in (0,1) else fib(n-1) + fib(n-2) print(fib(10)) # Prints 55 The file log.txt contains: fib((1,){}) = 1 fib(0,){}) = 0 fib((2,){}) = 1 fib((1,){}) = 1 fib((3,){}) = 2 fib((1,){}) = 1 fib(0,){}) = 0 fib((2,){}) = 1 fib((4,){}) = 3 fib((1,){}) = 1 fib((0,){}) = 0 fib((2,){}) = 1 fib((1,){}) = 1 fib((3,){}) = 2 fib((5,){}) = 5 fib((1,){}) = 1 fib(0,){}) = 0 fib((2,){}) = 1 fib((1,){}) = 1 fib((3,){}) = 2 fib((1,){}) = 1 fib(0,){}) = 0 fib((2,){}) = 1 fib((4,){}) = 3 fib((6,){}) = 8 fib((1,){}) = 1 fib(0,){}) = 0 fib((2,){}) = 1 fib((1,){}) - 1 fib((3,){}) = 2 fib((1,){}) = 1 fib(0,){}) = 0 fib((2,){}) = 1 fib((4,){}) = 3 fib((1,){}) = 1 fib(0,){}) = 0 fib((2,){}) = 1 fib((1,){}) = 1 fib((3,){}) = 2 fib((5,){}) = 5 fib((7,){}) = 13 fib((1,){}) = 1 fib(0,){}) = 0 fib(C2,){}) = 1 fib((1,){}) - 1 fib((3,){}) = 2 fib((1,){}) - 1 fib(0,){}) = 0 fib((2,){}) = 1 fib((4,){}) = 3 fib((1,){}) = 1 fib(0,){}) = 0 fib((2,){}) = 1 fib((1,){}) = 1 fib((3,){}) = 2 fib((5,){}) = 5 fib((1,){}) = 1 fib(0,){}) = 0 fib((2,){}) - 1 fib((1

fib((2,){}) = 1 fib((1,){}) = 1 fib((3,){}) = 2 fib((5,){}) = 5 fib((1,){}) = 1 fib(0,){}) = 0 fib((2,){}) - 1 fib((1,){}) = 1 fib((3,){}) = 2 fib((1,){}) = 1 fib(0,){}) = 0 fib(C2,){}) = 1 fib((4,){}) - 3 fib(C6,){}) = 8 fib((8,){}) = 21 fib((1,){}) = 1 fib(0,){}) = 0 fib((2,){}) = 1 fib((1,){}) = 1 fib((3,){}) = 2 fib((1,){}) = 1 fib(0,){}) = 0 fib(C2,){}) = 1 fib((4,){}) = 3 fib((1,){}) = 1 fib(0,){}) = 0 fib((2,){}) = 1 fib((1,){}) - 1 fib((3,){}) = 2 fib((5,){}) = 5 fib((1,){}) = 1 fib(0,){}) = 0 fib((2,){}) = 1 fib((1,){}) - 1 fib((3,){}) = 2 fib((1,){}) = 1 fib(0,){}) = 0 fib((2,){}) = 1 fib((4,){}) = 3 fib((6.){}) = 8 fib((1,){}) - 1 fib(0,){}) = 0 fib((2,){}) = 1 fib((1,){}) = 1 fib((3,){}) = 2 fib((1,){}) - 1 fib(0,){}) = 0 fib((2,){}) = 1 fib((4,){}) = 3 fib((1,){}) = 1 fib(0,){}) = 0 fib((2,){}) - 1 fib((1,){}) = 1 fib((3,){}) = 2 fib((5,){}) = 5 fib((7,){}) = 13 fib((9,){}) = 34 fib((1,){}) = 1 fib(0,){}) = 0 fib((2,){}) = 1 fib((1,){}) = 1 fib((3,){}) = 2 fib((1,){}) - 1 fib(0,){}) = 0 fib(C2,){}) = 1 fib((4,){}) = 3 fib((1,){}) = 1 fib(0,){}) = 0 fib((2,){}) = 1 fib((1,){}) =

fib((1,){}) = 1 fib((3,){}) = 2 fib((1,){}) - 1 fib(0,){}) = 0 fib(C2,){}) = 1 fib((4,){}) = 3 fib((,){}) = 8 fib((8,){}) = 21 fib((10,){}) = 55 Now decorate fib with both track and log: @track @log def fib(n): return n if n in (0,1) else fib(n-1) + fib(n-2) print(fib(10), 'calls =', fib.count) The output should then be (1,){} found in cache (2,){} found in cache (3,){} found in cache (4,){} found in cache (5,){} found in cache (6,){} found in cache (7,){} found in cache (8,){} found in cache 55, calls = 11 from the trace decorator, and log.txt should contain: fib((1,){}) = 1 fib((0,){}) = 0 fib((2,){}) = 1 fib((3,){}) = 2 fib((4,){}) = 3 fib((5,){}) = 5 fib((,){}) = 8 fib((7,){}) = 13 fib((8,){}) = 21 fib((9,){}) = 34 fib((10,){}) = 55 = Submit your log.txt and dec.py for the latter, composed-decorated case. PS - Note the order of the composition: @track @log def fib(n): def track(f): def wrapper(*args, **kwargs) : wrapper.count += 1 print(args, kwargs, \"found in cache\") return f(*args, **kwargs) wrapper.count = 0 return wrapper

Here I have a @track decorator but it's not complete:

def track(f):

def wrapper(*args, **kwargs):

wrapper.count += 1

print(args,kwargs, \"found in cache\")

return f(*args, **kwargs)

wrapper.count = 0

return wrapper

I need help writing @track and @log decorator that will append to log.txt the required output from the guidelines.

Please see the part \"The output should be\"!

Thank you!

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Introduction to Wireless and Mobile Systems

Authors: Dharma P. Agrawal, Qing An Zeng

4th edition

1305087135, 978-1305087132, 9781305259621, 1305259629, 9781305537910 , 978-130508713

More Books

Students also viewed these Programming questions

Question

What is the focus of a usage variance?

Answered: 1 week ago