

0 / 2 embers
0 / 3000 xp
click for more info
Complete a lesson to start your streak
click for more info
Still calibrating
click for more info
Not enough gems
Cost: 6 gems
1: Decorators
incomplete
2: Args and Kwargs
incomplete
3: Args and Kwargs Practice
incomplete
4: Decorators
incomplete
5: Decorators Review
incomplete
6: LRU Cache
incomplete
7: Decorators Practice
incomplete
Back
ctrl+,
Next
ctrl+.
This lesson's interactive features are locked, please to keep using them
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.
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 <.
Complete the replacer function.