Python and MicroPython compatibility

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
User avatar
rdagger
Posts: 143
Joined: Tue Feb 28, 2017 6:16 pm
Contact:

Python and MicroPython compatibility

Post by rdagger » Thu Oct 11, 2018 5:08 pm

I’m porting a library from MicroPython to Python. Is there a programmatic way to determine if the code is running in MicroPython or Python? My code has a lot of const() definitions. Is there a strategy to improve compatibility so I don’t have to redo all the const() statements?

User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: Python and MicroPython compatibility

Post by Roberthh » Thu Oct 11, 2018 6:58 pm

you could use:
const = lambda x:x
to avoid re-doing const statements. and you can query like:
if sys.implementation.name == "micropython":
......
to implement variants.

User avatar
rdagger
Posts: 143
Joined: Tue Feb 28, 2017 6:16 pm
Contact:

Re: Python and MicroPython compatibility

Post by rdagger » Thu Oct 11, 2018 8:13 pm

Roberthh wrote:
Thu Oct 11, 2018 6:58 pm
you could use:
const = lambda x:x
to avoid re-doing const statements. and you can query like:
if sys.implementation.name == "micropython":
......
to implement variants.
Thanks, that's very helpful. Would you use the same approach to handle different I2C implementations?

User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: Python and MicroPython compatibility

Post by Roberthh » Fri Oct 12, 2018 5:28 am

You can bundle functions with differ in separate modules and import either of them, depending on the python version.

Post Reply