<jupyter_start><jupyter_text>Python module/package imports for this chapter<jupyter_code>import sys, os, math, random, functools

import numpy as np

import matplotlib
import matplotlib.pyplot as pp
import matplotlib.animation as anim

%matplotlib inline

from IPython.display import display, HTML<jupyter_output><empty_output><jupyter_text>Code from previous videos (enhanced)<jupyter_code>from turtle import Turtle, Terrarium<jupyter_output><empty_output><jupyter_text>More functional Python<jupyter_code>def printargs(func):
    def decorated(*args,**kwargs):
        print("Function {} called with args = {} and kwargs = {}".format(func.__name__,args,kwargs))
        
        return func(*args,**kwargs)
    
    return decorated
@printargs
def fibonacci(n):
    """Return a number in the Fibonacci sequence."""
    
    if n < 2:
        return n
    
    return fibonacci(n-1) + fibonacci(n-2)
fibonacci(5)
def fibonacci(n):
    """Return a number in the Fibonacci sequence."""
    
    if n < 2:
        return n
    
    return fibonacci(n-1) + fibonacci(n-2)

fibonacci = printargs(fibonacci)
fibonacci
help(fibonacci)
def printargs(func):
    @functools.wraps(func)
    def decorated(*args,**kwargs):
        print("Function {} called with args = {} and kwargs = {}".format(func.__name__,args,kwargs))
        
        return func(*args,**kwargs)
    
    return decorated

@printargs
def fibonacci(n):
    """Return a number in the Fibonacci sequence."""
    
    if n < 2:
        return n
    
    return fibonacci(n-1) + fibonacci(n-2)
fibonacci
help(fibonacci)
fibonacci(5)
@functools.lru_cache(maxsize=None)
@printargs
def fibonacci(n):
    """Return a number in the Fibonacci sequence."""
    
    if n < 2:
        return n
    
    return fibonacci(n-1) + fibonacci(n-2)
fibonacci(5)
fibonacci(5)
def quartercircle(t1,dist):
    for a in range(90):
        t1.forward(dist)
        t1.left(1)

with Terrarium(animate=True) as t:
    t1 = Turtle(t)
    
    for dist in [fibonacci(i) for i in range(10)]:
        quartercircle(t1,dist)
def twice(a):
    return 2*a

for i in map(twice,range(5)):
    print(i)
for i in map(lambda a: 2*a,range(5)):
    print(i)
with Terrarium(animate=True) as t:
    t1 = Turtle(t)
    
    for dist in map(lambda x: x**2,map(fibonacci,range(10))):
        quartercircle(t1,dist)
with Terrarium() as t:
    t1 = Turtle(t)
    
    for dist in filter(lambda x: x % 2 == 0,map(fibonacci,range(10))):
        quartercircle(t1,dist)
functools.reduce(lambda x,y: x+y,map(fibonacci,range(10)))
sum(map(fibonacci,range(10)))<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12509622786759045, 'fraction_numerical': 0.017321016166281754, 'mean_word_length': 3.52, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '36511', 'license': 'MIT', 'n_tokens_mistral': 972, 'n_tokens_neox': 926, 'n_words': 225, 'path': 'sandbox/efficientPy/Exercise Files/chapter4/04_05/04_05_decorators_end.ipynb', 'repo_name': 'IvanaEscobar/sandbox', 'size': '2598'}