Advanced use of the typed Python library

Advanced use of the typed Python library

Nine months ago, we introduced a new Yoctopuce programming library for Python, completely rewritten to take advantage of the latest Python features, in particular typing and asynchronous coroutines. We had the opportunity to test it on several projects, where it performed very well, but we realized that some additional information might be useful. So here it is...


Integration into an asynchronous application

The library example that demonstrates the use of the asynchronous API assumes that it can launch the main event loop itself, using the classic Python command:

asyncio.run(main(), debug=False)


When adding a plug-in to an existing asynchronous application, the scenario is usually different: the asynchronous event loop has already been launched by the application, and the plug-in must be integrated into it.

For direct sensor reading or to activate an output in response to an application callback, it is usually sufficient to simply call our library with await. However, if you want to be able to react to unexpected events (plug/unplug or notified values), you will probably want to create a new independent asynchronous task when initializing your plug-in, to manage events and periodically call YAPI.UpdateDeviceList():

async def handleAsyncEvents():
    errmsg: YRefParam = YRefParam()
    while True:
        await YAPI.Sleep(3000, errmsg)
        await YAPI.UpdateDeviceList(errmsg)

async def setup():
    asyncio.get_running_loop().create_task(handleAsyncEvents)


In addition, to facilitate the use of our library in asynchronous mode, we have completed the interactive HTML reference documentation to include prototypes of both APIs: the standard version and the asynchronous version.

Integration without PyPI

Our library has no dependencies on third-party libraries: it is built entirely on Python's base classes. So if you want to maintain a greater control over the code, it is easy to integrate it into your project by copying only the minimum number of files required. In practice, this means adding a yoctolib subdirectory to your source code tree, including the base files (yocto_api) and the specific function files you use (for example, yocto_relay for the YRelay class):

yoctolib/
    yocto_api.py
    yocto_api_aio.py
    yocto_relay.py
    yocto_relay_aio.py
    # and so on


No other files are necessary. However, note that even if you are not using the new asynchronous API, the _aio files must be included, as they are used internally by the standard API. On the other hand, if you are working directly with the asynchronous API, you do not need the files providing the standard API.

Still in beta

Although this library is now fully stabilized and its features are equivalent, for example, to our official TypeScript library, we have chosen to keep it in beta until it includes all the features of the original Python library. The only thing missing is native USB support, without having to launch VirtualHub, which we plan to add but for which you need to wait a little longer...

Add a comment No comment yet Back to blog












Yoctopuce, get your stuff connected.