Recap: Pluggable Channels
Lesson 42 of 48 in Coddy's Python for C 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.
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
1From C Programs to Python
Run Python Without mainPrinting Without Format CodesNames Versus Typed StorageDivision and Integer LimitsSuites Instead of BracesRecap: A Python Receipt4Iteration and Transformations
Iteration Without PointersIndexes Without Pointer MathSentinels and Loop ControlComprehensions and SortingRecap: Stable Ranking2Values and Truth Tests
Absence Is Not a Null PointerContent Versus Object IdentityTruth Tests and OperandsConditional Value SelectionRecap: Missing or Eligible5Functions and Callbacks
Functions and Object ArgumentsDefaults and Named ArgumentsVarargs Without va_listCallbacks and Captured NamesRecap: Pricing Functions8Composition and Inheritance
Callbacks to CompositionEmbedding Versus InheritanceBehavior Without VtablesEquality and Data ClassesRecap: Pluggable Channels3Containers and Aliasing
Lists Versus C ArraysTuples and Struct ValuesMappings and Lookup LoopsPointers, Copies, and AliasesRecap: Inventory Collections6Contracts, Errors, and Modules
Text Input and Exact OutputError Codes Versus ExceptionsImports Are Not IncludesAnnotations Versus C TypesRecap: Validate a Batch9Project: An Order Processor
Project: Model an OrderProject: Pricing PoliciesPractice on your own: Online Python compiler