Recap: Notification Channels
Lesson 42 of 48 in Coddy's Python for Lua 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 Lua Developers
1From Lua to Python
Run Your First Python ProgramPrinting and TextNames and Local ScopeNumbers and ConversionSuites Instead of endRecap: Rewrite a Receipt4Iteration and Transformations
Numeric for to rangepairs, ipairs, and itemsLoop Control and SkipsComprehensions and SortingRecap: Build a Ranking2Values and Decisions
nil Versus NoneTable Identity and EqualityTruthiness Is Differentelif and Value SelectionRecap: Eligibility Rules5Functions and Closures
Returns and Shared ParametersDefaults and Named CallsVarargs and Call UnpackingUpvalues and Late BindingRecap: Refactor a Pricing Tool8Composition and Dispatch
Composition Shares ObjectsPrototype Chains to superDuck Typing Beyond TablesMetamethods to DataclassesRecap: Notification Channels3Tables to Typed Collections
Sequence Tables to ListsTuples and Multiple ReturnsRecords to DictionariesSets and Shared TablesRecap: Clean an Inventory6Errors and Modules
Input and Exact Textpcall to Exceptionsrequire to importHints and Runtime TypesRecap: Validate a Batch9Project: Order Processing
Create the Order ModelAdd Pricing PoliciesPractice on your own: Online Python compiler