I'm a former c++ coder and have made the plunge into python for several months, which has been great. I'm doing a bit of code porting and have come across a problem of which i'm not sure the best path. There are many ways to skin a cat - but i'm looking for advice on what would be the 'best' and/or most pythonic way to do something similar to the section of c++ code below.
I've cut the code to a trivial 'reproducer' just to highlight what was happening. Essentially there is a well-defined interface of callbacks the server will call when certain events happen. When a server is created, it is provided a callback interface as an argument.
In the below case, the client has implemented these callbacks on itself, and thus when it is creating the server, it provides the *this pointer.
Is this something similar in python? Any suggestions would get greatly appreciated.
#include <iostream>
// Client Feed Handler function Interface
class ClientFeedHandlersI
{
public:
virtual void HandlerFeed1() = 0;
virtual void HandlerFeed2() = 0;
};
// A very basic server
class Server
{
public:
Server(ClientFeedHandlersI& handlers)
: mHandlers(handlers) {}
void Start()
{
// Create some random events to illustrate
for (int i = 0; i < 10; ++i)
EventLoopHandler();
}
private:
void EventLoopHandler()
{
if (rand() % 10 > 5)
mHandlers.HandlerFeed1();
else
mHandlers.HandlerFeed2();
}
ClientFeedHandlersI& mHandlers;
};
// A really simple client
class Client : private ClientFeedHandlersI
{
public:
Client()
: mMyServer(*this)
{
mMyServer.Start();
}
private:
void HandlerFeed1() override { std::cout << "HandlerFeed1 called\n"; }
void HandlerFeed2() override { std::cout << "HandlerFeed2 called\n"; }
Server mMyServer;
};
int main()
{
auto c = Client();
}
So here's an attempt at porting to python. Note that in the real world example, there are 20+ client feed handler functions, hence why i want to force the interface using the @abstractmethod.
# python 3.6
from abc import ABC, abstractmethod
class ClientFeedHandlersI(ABC):
def __init__(self):
pass
@abstractmethod
def handler_feed_1(self):
pass
@abstractmethod
def handler_feed_2(self):
pass
class Server:
def __init__(self, clientCallBacks:ClientFeedHandlersI):
self.clientCallBacks = clientCallBacks
def start(self):
for ii in range(10):
self.event_loop_handler()
def event_loop_handler(self):
import random
if random.randint(0,10) > 5:
self.clientCallBacks.handler_feed_1()
else:
self.clientCallBacks.handler_feed_2()
class Client(ClientFeedHandlersI):
def __init__(self):
self.server = Server(self)
self.server.start()
def handler_feed_1(self):
print("HandlerFeed1 called\n")
def handler_feed_2(self):
print("HandlerFeed2 called\n")
if __name__ == '__main__':
c = Client()
Edit: The above code sample now works as per the c++ code.