Recap: Dispatch by Behavior
Lesson 42 of 48 in Coddy's Python for C# Developers course.
Challenge
BeginnerImplement Email.send(text) returning email: plus text and Sms.send(text) returning sms: plus text. Implement Dispatcher(channels) storing a shallow copy of the list and dispatch(text) returning one result per channel in original order. The driver reads email/sms/log tokens and a message, supplies its own log object, creates the dispatcher, then clears the original list before dispatching. Repeats remain repeats. It prints results or none for no channels. All methods format strings only; no messages are actually sent.
C# transfer checkpoint
Like a List<T> copy constructor, a Python shallow copy isolates outer-list edits while retaining the channel objects.
C# reference excerpt: executable statements assume using System; type-only excerpts illustrate the declared contract.
var channels = new System.Collections.Generic.List<string> { "email" };
var stored = new System.Collections.Generic.List<string>(channels);
channels.Clear();
Console.WriteLine(stored.Count);Python pattern
channels = ["email"]
stored = channels.copy()
channels.clear()
print(len(stored))Editable implementation files: email.py, sms.py, dispatcher.py. The locked driver.py is the supplied launcher or input/output harness and must remain unchanged.
Keep the supplied imports and exact API names. Return results or raise the required exceptions; do not read input or print from these implementation 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 C# Developers
1C# Foundations in Python
Run the Python WorkspaceOutput, Strings, and CommentsTyped Locals to Python NamesDivision and RemaindersIndentation Instead of BracesRecap: Port a Receipt4Loops and Transformations
From foreach to forIndexes and Dictionary ItemsSentinels and Loop ControlFrom LINQ to ComprehensionsRecap: Stable Ranking2None and Decisions
Nullable Values and NoneEquality, Identity, and TypesTruth Tests and FallbacksBranch PriorityRecap: Eligibility Rules5Functions and Scope
Define Functions and ReturnNamed Arguments and DefaultsStar Arguments and UnpackingCallbacks and Python ScopeRecap: Pricing Functions8Composition and Inheritance
Replaceable CollaboratorsOverrides and superInterfaces and Duck TypingRecords and Value EqualityRecap: Dispatch by Behavior3Collections and References
From List<T> to listTuples and UnpackingDictionary Keys and ValuesSets, Aliases, and CopiesRecap: Stock Lookup6Reliable Python Programs
Input and Exact OutputExceptions and ValidationImports and the Main GuardType Hints and Runtime TypesRecap: Validate a Batch9Project: Order Processor
Project 1: Order ModelProject 2: Pricing PoliciesPractice on your own: Online Python compiler