human
4. Context managers
Create a Timer context manager that measures the running time of the with block. The context manager takes an optional name argument and prints the block's name at the end too."
Please write code following the instructions in jupyter notebook style.

Give me a code to 4?
assistant
#Here is code to 4.

from datetime import datetime

class Timer(object):
    def __init__(self, name="unnamed"):
        self.name = name
        
    def __enter__(self):
        self.start = datetime.now()
        
    def __exit__(self, *args):
        duration = datetime.now() - self.start
        print("{} ran for {} seconds".format(self.name, duration.total_seconds()))
        
        
# prints "slow code ran for F seconds
# F is the total_seconds the block took to finish (float)
with Timer("slow code"):
    s = sum(range(100000))
    
# prints "unnamed ran for F seconds
with Timer():
    s = sum(range(100000))