Teacher's Guide

Chapter 6 / Level 3: More complex Decorators

Teaching Objective:

  • Student learns how to work with multiple more complex decorated functions as to get the most out of decorators
  • Learns how to add and use arguments in decorators, and how these arguments can make the functions more dynamic
  • Understand what uses and best practices are best for decorators and how to make the most out of decorated functions

Guide

In this exercise we’re going to be working with arguments in decorators. The objective is to place pizzas in the oven and check the temperature of the ovens, placing firewood on the ovens that require it, then making salads that go with the pizzas.

First set up the decorator to place() pizzas in oven and adjust the temperatures on ovens if their temperature is below 450 degrees. The core function is decorated by using @ symbol to wrap the function.

def place_oven(func):
    def check(temp):

        if temp < 450:
            player.speak("Oven is not hot enough")
            player.open()
            player.place("firewood")
            player.close()
            return

        return func(temp)
    return check

# Set up pre decorated oven function

@place_oven
def bake_pizza(temp):
  player.open()
  player.place("pizza")
  player.close()
  player.speak("I am baking Pizza at %d degrees" % (temp))

Do the same when setting up a decorator to prepare salads, this is done by combining collected ingredients.

def add_toppings(func):
    def add(dressing, vegetable):

        player.place(dressing)
        player.place(vegetable)

        func(dressing, vegetable)
    return add

# Set up salad function
@add_toppings
def toss_salad(dressing, vegetable):
  player.combine("lettuce", dressing, vegetable)
  player.speak("I am preparing salad with %s dressing and %s"
  % (dressing, vegetable))

Walk to the bottom left of the level to collect the two bundles of logs, then walk to the light X marks and read() the temperature. Afterwards set up the read temperature with the bake_pizza() decorated function.

player.read()
bake_pizza(450)

While completing this task, stop by the gold X mark and create a list comprehension named ingredients in order to pack all items in a single list.

cabinet = [
    ["lettuce"], ["carrot" ,"tomato"] ,
    ["oil" , "ranch", "cesar"] , ["cheese"]]

ingredients = [
    val for sublist in cabinet for val in sublist]

Once all pizzas and wood have been set in the oven and collected the ingredients, walk to the dark X marks in the colored carpets and put together salads. Use the toss_salad() decorated function, and set the correct combination of ingredients as arguments. On the yellow carpet, create a salad using: "cesar" and "cheese" , on the green carpet create a salad using: "oil" and "tomato" , on the blue carpet create a salad using: "ranch" and "carrot" .

toss_salad("cesar","cheese")

Map overview and path

Map overview and path Map overview and path

Code Solution

import player

def place_oven(func):
    def check(temp):
        if temp < 450:
            player.speak("Oven is not hot enough")
            player.open()
            player.place("firewood")
            player.close()
            return
        return func(temp)
    return check

@place_oven
def bake_pizza(temp):
    player.open()
    player.place("pizza")
    player.close()
    player.speak("I am baking Pizza at %d degrees" % temp)

def add_toppings(func):
    def add(dressing, vegetable):
        player.place(dressing)
        player.place(vegetable)
        func(dressing, vegetable)
    return add

@add_toppings
def toss_salad(dressing, vegetable):
    player.combine("lettuce", dressing, vegetable)
    player.speak("I am preparing salad with %s dressing and %s" % (dressing, vegetable))

player.turn_right()
player.move_forward(2)
player.turn_left()
player.move_forward(4)
player.turn_right()
player.move_forward()
player.turn_right()
player.move_forward(4)
player.turn_left()
player.move_forward()
player.turn_right()
player.move_forward(2)
player.turn_right()
player.move_forward(2)
player.turn_left()
player.move_forward()
await player.read()
bake_pizza(450)
player.turn_right()
player.move_forward(3)
player.turn_left()
await player.read()
bake_pizza(380)
player.turn_right()
player.turn_right()
player.move_forward()
player.turn_left()
player.move_forward(2)
player.turn_left()

cabinet = [
    ["lettuce"], ["carrot", "tomato"],
    ["oil", "ranch", "cesar"], ["cheese"]
]

ingredients = [val for sublist in cabinet for val in sublist]
player.speak(ingredients)
player.turn_right()
player.move_forward(2)
player.turn_left()
player.move_forward()
await player.read()
bake_pizza(400)
player.turn_right()
player.move_forward(3)
player.turn_left()
await player.read()
bake_pizza(450)
player.turn_right()
player.turn_right()
player.move_forward()

toss_salad("cesar", "cheese")
player.turn_right()
player.move_forward(4)
player.turn_left()
toss_salad("oil", "tomato")
player.turn_right()
player.move_forward(2)
player.turn_left()
toss_salad("ranch", "carrot")

Chapter 6 / Level 3: More complex Decorators | Teacher's Guide