Jump to content

Recommended Posts

Posted

Hallo liebes Tinkerforge-Team.

 

Ich bin gerade wieder darüber gestolpert und möchte heute an dieser Stelle gern mal nachfragen. Würde es zukünftig nicht Sinn machen die Python-API dahingehend zu erweitern, dass man die verschiedenen Klassen auch im Rahmen des Kontext-Managers mit dem with-Statement ansprechen könnte?

 

from tinkerforge.ip_connection import IPConnection as _IPConnection
from tinkerforge.brick_master import BrickMaster as _BrickMaster

class IPConnection(_IPConnection):
    def __init__(self, host=None, port=4223):
        _IPConnection.__init__(self)
        if host is not None:
            self.connect(host, port)
        self.__host = host
        self.__port = port

    def __enter__(self):
        if not self.get_connection_state():
            self.connect(self.__host, self.__port)
        return self

    def __exit__(self, type, value, traceback):
        if self.get_connection_state():
            self.disconnect()


def test(ipcon):
    print(ipcon.get_connection_state())
    ipcon.set_auto_reconnect(True)
    ipcon.disconnect()
    print(ipcon.get_connection_state())


if __name__ == '__main__':
    HOST = '127.0.0.1'
    PORT = 4223

    with IPConnection(HOST, PORT) as ipcon:
        test(ipcon)

 

Viele Grüße

__LC__

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...