Skip to main content



      Home
Home » Eclipse Projects » Paho » Python client - subclassing(correct way to subclass client)
Python client - subclassing [message #1233523] Sun, 19 January 2014 17:29 Go to next message
Eclipse UserFriend
The example that ships with the Python client does this:
class MyMQTTClass:
    def __init__(self, clientid=None):
        self._mqttc = mqtt.Client(clientid)


Before I saw the example I had developed a module that subclasses the client like:
class MqClient(client.Client):
    def __init__(self, name):
        client.Client.__init__(self, name)


Is there any reason why the subclassing method shown in the repo example is preferable to the one I'm using? Mine seems to work fine at this point.
The major difference is that the repo example creates an internal _mqttc object where my method subclasses the client

The (admittedly minor) advantage of my approach is that I can do:
mqClient = MqClient({'name':'test123', etc etc})
mqClient.subscribe(mytopic, myqos)

as opposed to the repo example requiring:
mqClient = MyClient('test123')
mqClient._mqtcc.subscribe(mytopic, myqos)
Re: Python client - subclassing [message #1234392 is a reply to message #1233523] Tue, 21 January 2014 16:58 Go to previous messageGo to next message
Eclipse UserFriend
Hi Roy,

from a purely OO point of view, both approaches have their own characteristics which might suit different circumstances. Roger's example is not a subclass at all really, it's a class which has the mqtt client as an attribute. This means that the methods on the mqtt client are not directly available on MyMQTTClass, whereas on your class MqClient they are.

It depends what you want from your class. Maybe Roger will explain his reasoning...

Ian
Re: Python client - subclassing [message #1234555 is a reply to message #1234392] Wed, 22 January 2014 03:54 Go to previous messageGo to next message
Eclipse UserFriend
Hi Roy,

I think the confusion here is just some unfortunate naming. The sub-class.py name doesn't actually refer to subclassing, it's a duplicate of the sub[scribing-client].py example but using functions within a class instead of standalone. By that measure, the subclassing example would be sub-subclass.py - elegant! Smile

In general, I agree with Ian that it depends what you want with your class. It may make more sense to have the MQTT features hidden in your application, or it may not.

Cheers,

Roger
Re: Python client - subclassing [message #1718416 is a reply to message #1233523] Wed, 23 December 2015 08:47 Go to previous messageGo to next message
Eclipse UserFriend
Sorry for resurrecting an old thread.

Any advice on how to subclass the Client?

I'm having troubles with callbacks.

This does not work for example:
class MQTTClient(mqtt.Client):

    def __init__(self, client_id='', clean_session=True,  userdata=None, protocol=mqtt.MQTTv31):
        super(MQTTClient, self).__init__(client_id, clean_session,  userdata, protocol)
        self.on_message = self.on_message_cb

    def on_message_cb(self, userdata, message):
        print mesage.payload


The best thing would be if on_message would not be defined in __init__ but it would be an emptu funtion in mqtt.Client. Then you would just redefine the function in your own class.
Re: Python client - subclassing [message #1718462 is a reply to message #1234555] Thu, 24 December 2015 02:21 Go to previous message
Eclipse UserFriend
oops, double posted

[Updated on: Thu, 24 December 2015 02:22] by Moderator

Previous Topic:Delivery Action erro (Cast Error)
Next Topic:Picking the latest paho JAR
Goto Forum:
  


Current Time: Tue Jul 15 08:39:49 EDT 2025

Powered by FUDForum. Page generated in 0.04467 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top