sbs5@kangaroo:~/cs370/www/lectures$ date Wed Feb 12 02:29:35 PM EST 2025 sbs5@kangaroo:~/cs370/www/lectures$ pwd /home/accts/sbs5/cs370/www/lectures sbs5@kangaroo:~/cs370/www/lectures$ p Python 3.10.12 (main, Jan 17 2025, 14:35:34) [GCC 11.4.0] on linux Type "help", "copyright", "credits" or "license" for more information. >>> import time from timeit import * from functools import wraps >>> >>> >>> def slow_square(number): ''' Take a nap and then print the square of a number.''' print(f"Sleeping for {number} seconds") time.sleep(number) return number**2 ... ... ... ... ... >>> slow_square(3) Sleeping for 3 seconds 9 >>> from functools import lru_cache lru_cache >>> >>> slow_square = lru_cache(slow_square) >>> slow_square(3) Sleeping for 3 seconds 9 >>> slow_square(3) 9 >>> slow_square(4) Sleeping for 4 seconds 16 >>> slow_square(4) 16 >>> @lru_cache def slow_square(number): ''' Take a nap and then print the square of a number.''' print(f"Now sleeping for {number} seconds") time.sleep(number) return number**2 ... ... ... ... ... ... >>> slow_square(3) Now sleeping for 3 seconds 9 >>> slow_square(3) 9 >>> @lru_cache def slow_add(a,b): ''' Take a nap and then add two numbers.''' print(f"Sleeping for {a+b} seconds") time.sleep(a+b) return a+b ... ... ... ... ... ... >>> slow_add(3 5) File "", line 1 slow_add(3 5) ^^^ SyntaxError: invalid syntax. Perhaps you forgot a comma? >>> slow_add(3, 5) Sleeping for 8 seconds 8 >>> slow_add(3, 5) 8 >>> slow_add(5,3)) File "", line 1 slow_add(5,3)) ^ SyntaxError: unmatched ')' >>> slow_add(5,3) Sleeping for 8 seconds 8 >>> slow_add(5,3) 8 >>> dir(slow_add) ['__annotations__', '__call__', '__class__', '__copy__', '__deepcopy__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__get__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__name__', '__ne__', '__new__', '__qualname__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__wrapped__', 'cache_clear', 'cache_info', 'cache_parameters'] >>> slow_add.__name__ 'slow_add' >>> slow_add.__doc__ ' Take a nap and then add two numbers.' >>> slow_add.__wrapped__ >>> def bold(func): def g(*args): print ("", end="") func(*args) print ("", end="") return g ... ... ... ... ... ... >>> def italic(func): def g(*args): print ("", end="") func(*args) print ("", end="") return g ... ... ... ... ... ... >>> >>> test("this is a string.") Traceback (most recent call last): File "", line 1, in NameError: name 'test' is not defined >>> @bold @italic def test(s): 'this is test' print (str(s), end="") ... ... ... ... ... >>> test("this is a string.") this is a string.>>> test .g at 0x77e3f57e64d0> >>> test.__name__ 'g' >>> test.__doc__ >>> test.__wrapped__ Traceback (most recent call last): File "", line 1, in AttributeError: 'function' object has no attribute '__wrapped__' >>> def debug(f): def g(*args): print ("Calling: {} with args: {}".format(f.__name__, args)) val = f(*args) print ("Exiting: {} with value: {}".format(f.__name__, val)) return val return g ... ... ... ... ... ... ... \ ... 0 File "", line 9 0 ^ SyntaxError: invalid syntax >>> @debug def add2(a): return a + 2 ... ... ... Traceback (most recent call last): File "", line 1, in NameError: name 'debug' is not defined >>> def debug(f): def g(*args): print ("Calling: {} with args: {}".format(f.__name__, args)) val = f(*args) print ("Exiting: {} with value: {}".format(f.__name__, val)) return val return g ... ... ... ... ... ... ... @debug def add2(a): return a + 2 File "", line 8 @debug ^ SyntaxError: invalid syntax >>> ... ... >>> @debug def add2(a): return a + 2 ... ... ... Traceback (most recent call last): File "", line 1, in NameError: name 'debug' is not defined >>> def debug(f): def g(*args): print ("Calling: {} with args: {}".format(f.__name__, args)) val = f(*args) print ("Exiting: {} with value: {}".format(f.__name__, val)) return val return g ... ... ... ... ... ... ... >>> @debug def add2(a): return a + 2 ... ... ... >>> add2(8) Calling: add2 with args: (8,) Exiting: add2 with value: 10 10 >>> (8) 8 >>> (8,) (8,) >>> def trace(f): f.indent = 0 def g(x): print('| ' * f.indent + '|--', f.__name__, x) f.indent += 1 value = f(x) print('| ' * f.indent + '|--', 'return', repr(value)) f.indent -= 1 return value return g ... ... ... ... ... ... ... ... ... ... >>> @trace def fib(n): if n == 0: return 1 elif n == 1: return 1 else: return fib(n - 1) + fib(n - 2) ... ... ... ... ... ... ... ... >>> fib(4) |-- fib 4 | |-- fib 3 | | |-- fib 2 | | | |-- fib 1 | | | | |-- return 1 | | | |-- fib 0 | | | | |-- return 1 | | | |-- return 2 | | |-- fib 1 | | | |-- return 1 | | |-- return 3 | |-- fib 2 | | |-- fib 1 | | | |-- return 1 | | |-- fib 0 | | | |-- return 1 | | |-- return 2 | |-- return 5 5 >>> def memofib(n): table = [False for x in range(n+1)] return memofibaux(n,table) def memofibaux(n, table): if table[n]: return table[n] if n <= 1: return 1 else: table[n] = memofibaux(n-1, table) + memofibaux(n-2, table) return table[n] ... ... ... >>> ... ... ... ... ... ... >>> memofib(10) 89 >>> memofib(5) 8 >>> def trace2(f): f.indent = 0 def g(*x): print('| ' * f.indent + '|--', f.__name__, x) f.indent += 1 value = f(*x) print('| ' * f.indent + '|--', 'return', repr(value)) f.indent -= 1 return value return g ... ... ... ... ... ... ... ... ... ... >>> @trace2 def memofibaux(n, table): if table[n]: return table[n] if n <= 1: return 1 else: table[n] = memofibaux(n-1, table) + memofibaux(n-2, table) return table[n]@trace2 def memofibaux(n, table): if table[n]: return table[n] if n <= 1: return 1 else: table[n] = memofibaux(n-1, table) + memofibaux(n-2, table) return table[n] ... ... ... ... ... ... ... File "", line 8 def memofibaux(n, table): ^^^ SyntaxError: invalid syntax >>> File "", line 1 if table[n]: return table[n] IndentationError: unexpected indent >>> File "", line 1 if n <= 1: return 1 IndentationError: unexpected indent >>> File "", line 1 else: IndentationError: unexpected indent >>> File "", line 1 table[n] = memofibaux(n-1, table) + memofibaux(n-2, table) IndentationError: unexpected indent >>> File "", line 1 return table[n] IndentationError: unexpected indent >>> >>> @trace2 def memofibaux(n, table): if table[n]: return table[n] if n <= 1: return 1 else: table[n] = memofibaux(n-1, table) + memofibaux(n-2, table) return table[n] ... ... ... ... ... ... ... >>> memofib(5) |-- memofibaux (5, [False, False, False, False, False, False]) | |-- memofibaux (4, [False, False, False, False, False, False]) | | |-- memofibaux (3, [False, False, False, False, False, False]) | | | |-- memofibaux (2, [False, False, False, False, False, False]) | | | | |-- memofibaux (1, [False, False, False, False, False, False]) | | | | | |-- return 1 | | | | |-- memofibaux (0, [False, False, False, False, False, False]) | | | | | |-- return 1 | | | | |-- return 2 | | | |-- memofibaux (1, [False, False, 2, False, False, False]) | | | | |-- return 1 | | | |-- return 3 | | |-- memofibaux (2, [False, False, 2, 3, False, False]) | | | |-- return 2 | | |-- return 5 | |-- memofibaux (3, [False, False, 2, 3, 5, False]) | | |-- return 3 | |-- return 8 8 >>> memofib(5) |-- memofibaux (5, [False, False, False, False, False, False]) | |-- memofibaux (4, [False, False, False, False, False, False]) | | |-- memofibaux (3, [False, False, False, False, False, False]) | | | |-- memofibaux (2, [False, False, False, False, False, False]) | | | | |-- memofibaux (1, [False, False, False, False, False, False]) | | | | | |-- return 1 | | | | |-- memofibaux (0, [False, False, False, False, False, False]) | | | | | |-- return 1 | | | | |-- return 2 | | | |-- memofibaux (1, [False, False, 2, False, False, False]) | | | | |-- return 1 | | | |-- return 3 | | |-- memofibaux (2, [False, False, 2, 3, False, False]) | | | |-- return 2 | | |-- return 5 | |-- memofibaux (3, [False, False, 2, 3, 5, False]) | | |-- return 3 | |-- return 8 8 >>> sbs5@kangaroo:~/cs370/www/lectures$ exit Process shell finished