Recap: Pluggable Channels
Lesson 42 of 48 in Coddy's Python for Go Developers course.
Challenge
BeginnerImplement 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. Like a Go dispatcher holding interface values, call each supplied channel; copy the outer Python list so clearing the caller list cannot remove the channels. No real messages are sent.
Editable files: email.py, sms.py, dispatcher.py. Keep the provided imports and the required API names. The locked driver.py supplies execution and checks; do not edit it.
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.
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 Go Developers
1From Go to Python
Run Your First Python ProgramPrinting, Quotes, and CommentsBindings Beyond Go TypesDivision and Growing IntegersIndentation and ScopeRecap: A Python Receipt4Iteration and Transformations
range and Value IterationIndexes and Map EntriesSentinels Without Truth TrapsComprehensions and Stable SortRecap: Stable Ranking2Values, Absence, and Branches
None, nil, and Zero ValuesEquality Is Not IdentityTruth Tests Beyond boolBranches and Value SelectionRecap: Missing or Eligible5Functions and Call Contracts
Functions and Tuple ReturnsDefaults and Named ArgumentsVariadics and Keyword OptionsClosures and Callable ValuesRecap: Pricing Functions8Interfaces and Object Design
Interface Fields and CallsEmbedding Is Not InheritanceInterfaces and Duck TypingStructs and Data ClassesRecap: Pluggable Channels3Slices, Maps, and Sharing
Slices Versus Python ListsMultiple Values and TuplesGo Maps and Python dictsSets and Shallow CopiesRecap: Inventory Collections6Errors, Modules, and Hints
Text and Exact OutputErrors Versus ExceptionsPackages and Module ExecutionHints Versus Static TypesRecap: Validate a Batch9Project: An Order Processor
Project: Model an OrderProject: Pricing PoliciesPractice on your own: Online Python compiler