Package: messageit
messageitMessage (Command and Event) passing for PythonExamplesCreate custom eventCustom events could be used to notify multiple subscribers about something that happened to the the application or to the world. To create custom event, you subclass the Event class and mark it asdataclass:fromdataclassesimportdataclassfrommessageitimportEvent@dataclassclassMessageReceived(Event):message:str=NoneEach message hasmessage_idproperty automatically set to an UUID. If you do not
passcorrelation_idduring message creation,correlation_idis set to themessage_id.
Thecorrelation_idcould be used to track the execution of complex flows.event=MessageReceived(message="Hello world")event.message_id# UUID('ddeb3e81-3b75-403b-9b1e-13007b1f3abe')event.correlation_id# UUID('ddeb3e81-3b75-403b-9b1e-13007b1f3abe')Create custom commandCommands are used to perform actions. To create custom command, you subclass the Command class and mark it asdataclass:fromdataclassesimportdataclassfrommessageitimportCommand@dataclassclassCleanMessage(Command):message:str=NoneIfcorrelation_idis passed during message creation the passed value is set to the message'scorrelation_idattribute. With proper logging this could be used to track complex flows, e.g. distributed event and message handling.clean=CleanMessage(message="Hello world",correlation_id=event.correlation_id)clean.message_id# UUID('e0812459-5145-4de8-8d96-d0fce7fefb42')clean.correlation_id# UUID('ddeb3e81-3b75-403b-9b1e-13007b1f3abe')event.correlation_id# UUID('ddeb3e81-3b75-403b-9b1e-13007b1f3abe')Handle Commands - Assign algorithm to perform the actionBuilding on the previous example, we could assign algorithm to be executed when action is requested:frommessageitimportExecutordefclean_message(command:CleanMessage):returncommand.message.strip()commands=Executor()commands.register(CleanMessage,clean_message)command=CleanMessage(message="   Hello   ")commands.handle(command)# 'Hello'Subscribe to Events - Get notified when event takes placeBuilding on previous examples:frommessageitimportPublisherdefon_message_received(event:MessageReceived):clean_command=CleanMessage(message=event.message,correlation_id=event.message_id)cleaned=commands.handle(clean_command)print(f"Cleaned message:{repr(cleaned)}")defecho_message(event:MessageReceived):print(f"Your input is: '{event.message}'")raiseNotImplementedError()events=Publisher()events.register(MessageReceived,echo_message)events.register(MessageReceived,on_message_received)result=events.handle(MessageReceived(message="Hello World!!!"))# Your input is: 'Hello World!!!''# Cleaned message: 'Hello World!!!'result# [None, NotImplementedError()]The two registered subscribers were called and produced output. The result fromhandle()is a collection from the results returned by subscribers invoked byhandle(). If subscriber raised an exception, the exception is used as return result.Complete exampleHere is a complete example putting all the pieces from above together. It is a simple command line applicationo that reads a line from the user and prints the cleansed message.fromdataclassesimportdataclassfrommessageitimportCommand,Event,Executor,Publisher@dataclassclassCleanMessage(Command):message:str=None@dataclassclassMessageReceived(Event):message:str=Nonedefon_message_received(event:MessageReceived):print(f"Received message:{repr(event.message)}")clean_command=CleanMessage(message=event.message,correlation_id=event.message_id)cleaned=commands.handle(clean_command)print(f"Cleaned message:{repr(cleaned)}")defclean_message(command:CleanMessage):returncommand.message.strip()events=Publisher()events.register(MessageReceived,on_message_received)commands=Executor()commands.register(CleanMessage,clean_message)whileTrue:print("Type your message or leave empty to exit and press <Enter>: ")message=input()ifmessage=="":breakresult=events.handle(MessageReceived(message=message))Package and publishTo run the tests:$pytestTo package and publish:$pipinstall-Ubuildtwine
$python-mbuild
$python-mtwineuploaddist/*