Mimicking events in Python

Python Methods and Functions

First of all, what are events?
Events — these are constructs that allow a class to notify other classes when something interesting happens. 
In layman's terms, this is basically like raising a flag to signal to others that something interesting has happened.

When to use events in Python?

Event-based programming is primarily used when working with a user interface (UI), where various components must signal some occurrence. for example, imagine a currency converter that should output the converted currency into box2 while the user enters some value into box1. 
How box2 knows that the user has entered something into box1 and what needs to be done in response to it.
This is one of the most basic examples of event-based programming.

Another possible example would be be a HomeSecurity system in which it is necessary to raise some possible actions, such as an alarm, send a message to the owner, and the police should be informed about the possible theft when breaking the lock. Systems of this type can be easily designed using an event-driven mechanism, where an event will be triggered as soon as someone breaks the lock, and in turn will notify the event handlers to play their part.

Now in this post we will use the last example to understand how such events can be generated in Python, where it is not supported by default.

class Event ( object ):

 

def __ init __ ( self ):

self .__ eventhandlers  = []

  

def __ iadd __ ( self , handler):

self .__ eventhandlers.append (handler)

return self

 

def __ isub __ ( self , handler):

self .__ eventhandlers.remove (handler)

return < / code> self

 

def __ call __ ( self , * args, * * keywargs):

for eventhandler in self .__ eventhandlers:

eventhandler ( * args, * * keywargs)

 

class Police ( object ):

  def __ init __ ( self , policeTelephoneNo):

self ._ telephone = policeTelephoneNo

  

def CallPolice ( self ):

print "police have been informed"

 

class Owner ( object ):

def __ init __ ( self , ownerMobile):

  self .__ mobile = ownerMobile

 

  def Message ( self ) :

print "owner has been messaged about the possible theft "

  

class Alarm ( object ):

  

  def StartAlarm ( self ):

  print "Alarm has started"

  
# LockClass

 

class Lock ( object ):

 

def __ init __ ( self ):

self . OnLockBroken = Event ()

  

  def LockBroken ( self ):

# This function will be executed after the lock is broken and

# raise the event

self . OnLockBroken ()

 

def AddSubscribersForLockBrokenEvent ( self , objMethod):

self . OnLockBroken + = objMethod

  

def RemoveSubscribersForLockBrokenEvent ( self , objMethod):

self . OnLockBroken - = objMethod

 

def Simulation (): 

# In the simulation we have a lock

# which will be broken and the police object

# owner and classes alarms that

# receive notifications as soon as the lock is broken

 

# Required objects

godrejLockObj = Lock ()

localPoliceObj = Police ( 100 )

ownerObj = Owner ( 99999999 )

  mainDoorAlarmObj = Alarm ()

 

# Setting these objects to receive blocking events

godrejLockObj.AddSubscribersForLockBrokenEvent (localPoliceObj.CallPolice)

godrejLockObj.AddSubscribersForLockBrokenEvent (ownerObage) / code>

godrejLockObj.AddSubscribersForLockBrokenEvent (mainDoorAlarmObj.StartAlarm)

 

# Now the lock is broken by some robber

# this will call the LockBroken function

godrej LockObj.LockBroken ()

 

# All three notifications must be printed

# as soon as the lock broken

 

# You can also remove any recipient

  # Call RemoveSubscribeersForLockBrokenEvent

godrejLockObj.RemoveSubscribersForLockBrokenEvent (mainDoorAlarmObj.StartAlarm)

if __ name__ = = "__ main__"

Simulation ()

Output:

 police have been informed owner has been messaged about the possible theft Alarm has started 

The class that raises the event is called the publisher and the classes that receive the event are called subscribers. 
Additionally, an event can have multiple subscribers, and a subscriber can handle multiple events from multiple publishers.

This article is provided by Ankit Singh . If you are as Python.Engineering and would like to contribute, you can also write an article using contribute.python.engineering or by posting an article contribute @ python.engineering. See my article appearing on the Python.Engineering homepage and help other geeks.

Please post comments if you find anything wrong or if you'd like to share more information on the topic discussed above.