Welcome, guest | Sign In | My Account | Store | Cart

This small code demonstrates how state driven programming can be done in python.

Python, 66 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/usr/bin/env python
                                                                                                                                                                                                                                                         
                                                                                                                                                                                                                                                         
class EventObject:
        """Abstrac EventObject"""
        def __init__(self, eventSource):
                self._eventSource = eventSource
                                                                                                                                                                                                                                                         
        def getSource(self):
                return self._eventSource
                                                                                                                                                                                                                                                         
class HackingEvent(EventObject):
        def __init__(self, eventSource, file, machine):
                EventObject.__init__(self, eventSource)
                self._file = file
                self._machine = machine
                                                                                                                                                                                                                                                         
        def getFile(self):
                return self._file
                                                                                                                                                                                                                                                         
        def getMachine(self):
                return self._machine
                                                                                                                                                                                                                                                         
                                                                                                                                                                                                                                                         
class HackingListener:
        def fileHacked(self, hackingEvent):
                raise "fileHacked - not implemented"
                                                                                                                                                                                                                                                         
class Logger(HackingListener):
        def fileHacked(self, hackingEvent):
                print "Hacking Event occured..."
                print hackingEvent.getSource().getName(), " hacked: ", hackingEvent.getFile(), " @", hackingEvent.getMachine()
                                                                                                                                                                                                                                                         
                                                                                                                                                                                                                                                         
class Hacker:
        def __init__(self, name):
                self._name = name
                self._hackingListeners = []
                                                                                                                                                                                                                                                         
        def getName(self):
                return self._name
                                                                                                                                                                                                                                                         
        def addHackingListener(self, listener):
                self._hackingListeners.append( listener )
                                                                                                                                                                                                                                                         
        def removeHackingListener(self, listener):
                self._hackingListeners.remove( listener )
                                                                                                                                                                                                                                                         
        def hackFile(self, filename, machine):
                myHackingEvent = HackingEvent(self, filename, machine)
                self._fireEvent(myHackingEvent)
                                                                                                                                                                                                                                                         
        def _fireEvent(self, event):
                print "fireing event"
                for listener in self._hackingListeners:
                        listener.fileHacked(event)
                                                                                                                                                                                                                                                         
                                                                                                                                                                                                                                                         
def main():
        Neo = Hacker('Neo')
        myLogger = Logger()
        Neo.addHackingListener(myLogger)
        Neo.hackFile('secureFile.txt', 'HAL9000')
                                                                                                                                                                                                                                                         
if __name__ == '__main__':
        main()
Created by Elmar Bschorer on Wed, 16 Jun 2004 (PSF)
Python recipes (4591)
Elmar Bschorer's recipes (2)

Required Modules

  • (none specified)

Other Information and Tasks