Question: 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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!