We're sorry but this app doesn't work properly without JavaScript enabled. Please enable it to continue.

This lesson's interactive features are locked, please to keep using them

Decorators Practice

You can stack decorators, and you can use currying with decorators.

from collections.abc import Callable

TextFunc = Callable[[str], None]


def to_uppercase(func: TextFunc) -> TextFunc:
    def wrapper(document: str) -> None:
        func(document.upper())

    return wrapper


def get_truncate(length: int) -> Callable[[TextFunc], TextFunc]:
    def truncate(func: TextFunc) -> TextFunc:
        def wrapper(document: str) -> None:
            func(document[:length])

        return wrapper

    return truncate


@to_uppercase
@get_truncate(9)  # currying
def print_input(input: str) -> None:
    print(input)


print_input("Keep Calm and Carry On")
# prints: "KEEP CALM"

Notice that get_truncate(9) first returns a decorator, which wraps print_input. Then to_uppercase wraps that already-wrapped function. When print_input is called, the text is converted to uppercase, then truncated to 9 characters before printing.

Assignment

Doc2Doc needs a feature that can take care of encoding characters as escape sequences in HTML documents.

You might not know anything about HTML. That's fine. This assignment isn't about HTML directly.

Just understand that it's a markup language like Markdown. Certain characters are interpreted as part of HTML syntax. In order to show those characters without interpreting them, they must be escaped. For example, < is replaced with &lt;.

Complete the replacer function.