Project 21: Now-Playing Bot
AI Engineer
Current Task
Objective
Review day: combine the pipe operator from day 6 with a third step, chaining three Runnables together.
Task: build a three-step Runnable pipeline for a song title and print the result.
index.py
class Runnable:
def __init__(self, func):
self.func = func
def __or__(self, other):
return Runnable(lambda value: other.invoke(self.invoke(value)))
def invoke(self, value):
return self.func(value)
trim = Runnable(lambda text: text.strip())
title_case = Runnable(lambda text: text.title())
add_prefix = Runnable(lambda text: f"Now Playing: {text}")
pipeline = trim | title_case | add_prefix
print(pipeline.invoke(" bohemian rhapsody "))
* Hint: Correct characters turn green, incorrect ones turn red.
Live Preview
🖼️
Verify your code to see the preview