Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Paho » Python client - subclassing(correct way to subclass client)
Python client - subclassing [message #1233523] Sun, 19 January 2014 22:29 Go to next message
Roy Barkas is currently offline Roy BarkasFriend
Messages: 1
Registered: January 2014
Junior Member
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 21:58 Go to previous messageGo to next message
Ian Craggs is currently offline Ian CraggsFriend
Messages: 34
Registered: March 2013
Member
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 08:54 Go to previous messageGo to next message
Roger Light is currently offline Roger LightFriend
Messages: 16
Registered: September 2013
Junior Member
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 13:47 Go to previous messageGo to next message
Martin Frlin is currently offline Martin FrlinFriend
Messages: 2
Registered: December 2015
Junior Member
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 07:21 Go to previous message
Martin Frlin is currently offline Martin FrlinFriend
Messages: 2
Registered: December 2015
Junior Member
oops, double posted

[Updated on: Thu, 24 December 2015 07:22]

Report message to a moderator

Previous Topic:Delivery Action erro (Cast Error)
Next Topic:MQTT Subscriber Parallel Processing
Goto Forum:
  


Current Time: Sat Feb 06 09:28:04 GMT 2016

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

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