回顾:通知渠道
Coddy 面向 Kotlin 开发者的 Python 课程的第 42 课,共 48 课。
挑战
入门实现 Email.send(text),使其返回 email: 加上文本;实现 Sms.send(text),使其返回 sms: 加上文本。实现 Dispatcher(channels),保存所提供列表的副本;实现 dispatch(text),按顺序为每个渠道返回一个结果。输入包含一行以空格分隔的渠道名称(email、sms 或 log),随后是消息。锁定的驱动程序会提供具有相同 send API 的日志渠道,创建分发器,清空原始渠道列表,并打印分发结果。如果没有渠道,则打印 none。重复的渠道会产生重复的结果。这些方法只负责格式化字符串,不会真正发送消息。
锁定的驱动程序负责处理输入和输出。只提交所需的类和函数定义,并使用准确的 API 名称。不要在你的类中读取输入或打印内容。
你的代码应写入可编辑文件 email.py、sms.py 和 dispatcher.py。driver.py 中包含提供的输入输出代码和 API 检查,无法编辑。保留提供的导入语句;不要在可编辑的模块中读取输入或打印内容。
亲手试试
from email import Email
from sms import Sms
from dispatcher import Dispatcher
class Log:
def send(self, text):
return "log:" + text
factories = {"email": Email, "sms": Sms, "log": Log}
channels = [factories[name]() for name in input().split()]
text = input()
dispatcher = Dispatcher(channels)
channels.clear()
results = dispatcher.dispatch(text)
if not results:
print("none")
for result in results:
print(result)
面向 Kotlin 开发者的 Python中的所有课
自己练习: Python 在线编译器