Recap: Notification Channels
Lesson 42 of 48 in Coddy's Python for Swift Developers course.
Challenge
BeginnerImplement Email.send(text) returning email: plus text and Sms.send(text) returning sms: plus text. Implement Dispatcher(channels) storing a copy of the supplied list, and dispatch(text) returning one result per channel in order. Input has a space-separated line of channel names (email, sms, or log), then the message. The locked driver supplies the log channel with the same send API, creates the dispatcher, clears the original channel list, and prints dispatched results. With no channels, print none. Repeated channels produce repeated results. These methods format strings only; they never send real messages.
The locked driver handles input and output. Submit only the required class and function definitions with the exact API names. Do not read input or print from your classes.
Your code goes in the editable files email.py and sms.py and dispatcher.py. driver.py holds the supplied input/output code and API checks and cannot be edited. Keep the supplied imports; do not read input or print from the editable modules.
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 Swift Developers
1From Swift to Python
Run Your First Python ProgramPrinting and Stringslet, var, and RebindingInt Range and DivisionIndentation and Block ScopeRecap: Rewrite a Receipt4Iteration and Transformations
for and Half-Open Rangesenumerated to enumeratewhile and Loop Controlmap, filter, ComprehensionsRecap: Build a Ranking2Values and Decisions
Optionals and NoneEquatable and IdentityBool Conditions and FallbacksBranches and TernariesRecap: Eligibility Rules5Functions and Closures
Functions and Explicit ReturnArgument Labels and DefaultsVariadics and Call ExpansionCapture Lists and DefaultsRecap: Refactor a Pricing Tool8Composition and Dispatch
Composition and Value CopiesInheritance and superProtocols and Duck TypingEquatable to DataclassRecap: Notification Channels3Value Types to Shared Objects
Array to listTuples and UnpackingDictionary Lookup and OrderValue Copies and AliasesRecap: Clean an Inventory6Errors and Modules
Input and String Formattingthrows, defer, and withModules and Main GuardsHints and Runtime TypesRecap: Validate a Batch9Project: Order Processing
Create the Order ModelAdd Pricing PoliciesPractice on your own: Online Python compiler