Menu
CoddyTech

Recap: Pluggable Channels

Lesson 42 of 48 in Coddy's Python for C Developers course.

challenge icon

Challenge

Beginner

Implement Email.send(text) and Sms.send(text), returning email: or sms: plus text. Dispatcher(channels) stores a shallow copy of the list; dispatch(text) returns each channel's send result in order. The driver reads email/sms/log names and a message, supplies log, clears the original list, then prints results or none if no channels. Repeated channels remain repeated.

Implement the named definitions in the editable modules without input or printing. The driver owns input/output and may call the APIs again on fresh data without producing extra output.

Copy the outer list while retaining channel objects, like copying an array of C pointers without cloning their pointees. No messages are sent.

Your code goes in email.py, sms.py, dispatcher.py. The locked driver.py is the entry file, supplies execution and checks, and cannot be edited.

Try it yourself

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)

All lessons in Python for C Developers

Practice on your own: Online Python compiler