Best way to "detect" MicroPython
Posted: Sun Jul 22, 2018 5:11 pm
I'm writing a library which is targeted to be able to run under MicroPython, but also "regular" python and development/unit testing is largely happening under regular python.
There are a few things which need to have differences between them. Typical, of course, is the import; "ucollections" vs. "collections", etc. Since I'll likely be using CircuitPython these are slightly less bothersome, but I'd like to work with all of them. The obvious way is to try importing ucollections (for example) and on import error import collections, but that seems a bit clunky. Is there a better way?
const() is another thing to look out for; I'll need to define it for regular python. What is the most efficient way to detecting if it exists?
A few other places can get icky; for example, pySerial read() is readchar() in UART, but I guess isinstance or the like is a way to tackle those. Or could wrap UART to be minimally pySerial like (or vice versa of course), but would still need someway of telling which python.
I guess I can see solutions for most of these, but was looking for something like a "best practices" to write cross-platform apps.
There are a few things which need to have differences between them. Typical, of course, is the import; "ucollections" vs. "collections", etc. Since I'll likely be using CircuitPython these are slightly less bothersome, but I'd like to work with all of them. The obvious way is to try importing ucollections (for example) and on import error import collections, but that seems a bit clunky. Is there a better way?
const() is another thing to look out for; I'll need to define it for regular python. What is the most efficient way to detecting if it exists?
A few other places can get icky; for example, pySerial read() is readchar() in UART, but I guess isinstance or the like is a way to tackle those. Or could wrap UART to be minimally pySerial like (or vice versa of course), but would still need someway of telling which python.
I guess I can see solutions for most of these, but was looking for something like a "best practices" to write cross-platform apps.