micropython-lib project: CPython backports

Discussion about programs, libraries and tools that work with MicroPython. Mostly these are provided by a third party.
Target audience: All users and developers of MicroPython.
Post Reply
pfalcon
Posts: 1090
Joined: Fri Feb 28, 2014 2:05 pm

micropython-lib project: CPython backports

Post by pfalcon » Sat Oct 13, 2018 11:15 am

Recently, with the increasing number of packages which work both on MicroPython and CPython, I figured it might be a good idea to provide "MicroPython compatibility layer" for CPython. Put simply, you could "import utime" on CPython and use MicroPython's extended functions available in this module (and vice-versa, wouldn't be able to use functions not available in MicroPython).

The usecases enabled by this are:
  • "MicroPython first" module development. I.e. modules can be developed and optimized for MicroPython, the efficient Python language implementation, while still work on bloated implementations like CPython.
  • Reuse of the wealth of tools available for CPython, like debuggers, syntax/style checkers, coverage, etc.
  • Porting large MicroPython libs/apps to CPython. For example, uasyncio used to work on CPython by monkey-patching CPython's asyncio, but as it evolved, it became impractical/inefficient to do that. Implementing uasyncio dependencies, like utime, uselect, etc. is a better way.
  • Overall proof that despite differences in standard libraries, MicroPython and CPython are members of one big Python ecosystem.
For more information, see https://github.com/pfalcon/micropython- ... -backports

An example:

Code: Select all

$ pip3 install --user micropython-cpython-uhashlib
...
$ python3
...
>>> import uhashlib
>>> uhashlib.sha1(b"test").hexdigest()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'sha1' object has no attribute 'hexdigest'
# MicroPython's uhashlib doesn't have hexdigest(), use ubinascii.hexlify(.digest())
>>> uhashlib.sha1(b"test").digest()
b'\xa9J\x8f\xe5\xcc\xb1\x9b\xa6\x1cL\x08s\xd3\x91\xe9\x87\x98/\xbb\xd3'

To remind, the master micropython-lib repository is https://github.com/pfalcon/micropython-lib . Issues reports and patches adhering to Contributor Guidelines are welcome.
Awesome MicroPython list
MicroPython standard library for all ports and forks - https://github.com/pfalcon/micropython-lib
Oftentimes more up to date docs than mainline - http://pycopy.readthedocs.io/

mattyt
Posts: 143
Joined: Mon Jan 23, 2017 6:39 am

Re: micropython-lib project: CPython backports

Post by mattyt » Sat Oct 13, 2018 11:32 pm

This is a very helpful contribution, thanks! I see it as being especially useful for unit testing where it can be much easier to run tests (at least first) on a PC.

Note that there's a similar effort in the CircuitPython space - adafruit_blinka. It's a little different since, as this guide explains, the main purpose is to allow CircuitPython code to run on Raspberry Pi, BeagleBone Blacks and generally Linux - but with support for GPIO, SPI and I2C. Maybe there's something in there that you might find useful?

In any case, thanks again @pfalcon!

pfalcon
Posts: 1090
Joined: Fri Feb 28, 2014 2:05 pm

Re: micropython-lib project: CPython backports

Post by pfalcon » Fri Oct 19, 2018 6:17 am

mattyt wrote:
Sat Oct 13, 2018 11:32 pm
This is a very helpful contribution, thanks! I see it as being especially useful for unit testing where it can be much easier to run tests (at least first) on a PC.
Right. But actually, it's possible for quite some to do unit testing using MicroPython itself (with Unix port if doing on a PC). The corresponding module (unittest) is in micropython-lib: https://github.com/pfalcon/micropython- ... r/unittest . It surely doesn't have all the features CPython's counterpart has, but a few people contributed to it, which is a sign they actually find it useful.

For more complex tools, like code coverage or debuggers, running the code using CPython may be the most "low-hanging" way indeed.
Note that there's a similar effort in the CircuitPython space - adafruit_blinka. It's a little different since, as this guide explains, the main purpose is to allow CircuitPython code to run on Raspberry Pi, BeagleBone Blacks and generally Linux - but with support for GPIO, SPI and I2C. Maybe there's something in there that you might find useful?
Sure, it's great to see more MicroPython related projects around!
Awesome MicroPython list
MicroPython standard library for all ports and forks - https://github.com/pfalcon/micropython-lib
Oftentimes more up to date docs than mainline - http://pycopy.readthedocs.io/

Post Reply