Trying to understand uos vs os

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
User avatar
liudr
Posts: 211
Joined: Tue Oct 17, 2017 5:18 am

Trying to understand uos vs os

Post by liudr » Sat Apr 07, 2018 5:15 am

I don't quite understand the distinction between umodule and module. Should I use uos or os instead? Can someone please explain the pros and cons of using either? If uos is only a subset of os, then how come they have the same functions?

Code: Select all

>>> os.
__name__        uname           urandom         ilistdir
listdir         mkdir           rmdir           chdir
getcwd          getdrive        remove          rename
stat            statvfs         mountsd         umountsd
sdconfig        SDMODE_SPI      SDMODE_1LINE    SDMODE_4LINE
>>> uos.
__name__        uname           urandom         ilistdir
listdir         mkdir           rmdir           chdir
getcwd          getdrive        remove          rename
stat            statvfs         mountsd         umountsd
sdconfig        SDMODE_SPI      SDMODE_1LINE    SDMODE_4LINE
Will uos save memory?

OutoftheBOTS_
Posts: 847
Joined: Mon Nov 20, 2017 10:18 am

Re: Trying to understand uos vs os

Post by OutoftheBOTS_ » Sat Apr 07, 2018 5:46 am

As far as I am aware os is 6 and uos is half a dozen ;)

stijn
Posts: 735
Joined: Thu Apr 24, 2014 9:13 am

Re: Trying to understand uos vs os

Post by stijn » Sat Apr 07, 2018 7:20 am

There is a distinction, might depend on the port used though. IIRC the idea is uos implements the core MicroPython functions (ilistdir) and os provides CPython-like functions (listdir)

Code: Select all

MicroPython v1.9.3-383-g2cabe34 on 2018-02-27; win32 version
Use Ctrl-D to exit, Ctrl-E for paste mode
>>> import os
>>> import uos
>>> os.
__class__       __file__        __name__        __path__
getenv          mkdir           remove          system
uos             _syss           makedirs        listdir
getcwd
>>> uos.
__class__       __name__        errno           getenv
ilistdir        mkdir           stat            system
unlink
Only real way to know if uos saves memory is to measure it, but I'd assume so.

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: Trying to understand uos vs os

Post by dhylands » Sat Apr 07, 2018 2:43 pm

On the pyboard, os is a "weak link" to uos. uos is a micro implementation of the os module and uos is included with the firmware.

If desired, you can provide your own os module which would add additional functionality to uos. The intention is that your code would use the os module, which is mapped to uos. If you add an extended os, then it would override the weak link and it would call uos. Your code just needs to continue using os.

On the pyboard with no overrides, os and uos are exactly the same.

User avatar
liudr
Posts: 211
Joined: Tue Oct 17, 2017 5:18 am

Re: Trying to understand uos vs os

Post by liudr » Sat Apr 07, 2018 7:28 pm

OK, thanks. I think I am beginning to understand. For better portability and future-proof, I should use os, although it might cost a bit more memory. I am using ESP32 port with pSRAM firmware forked by lobo. It has enough memory for everything so I don't have to worry about running out of memory for typical tasks.

I made an attempt to revise my data logging script so it would work on both computers (PC/Mac/nix/RPI/BBB) and MicroPython boards. It turns out to be too complicated, like the mess you see inside Arduino header files. I think it's OK with C/C++ because we have pre-compiler processor and code is largely unchanged except for the added #if #ifdef. In Python I had to insert if (implementation=='micropython'): everywhere and change indentations because there are lots of modules I use on cpython that micropython doesn't have, such as urllib and datetime.

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: Trying to understand uos vs os

Post by dhylands » Sat Apr 07, 2018 8:45 pm

If you don't load any extensions to the os module, then using it will use exactly the same amount of memory as using uos.

If you choose to load extensions, then those extensions, will, of course, use more memory.

What I do when I want emulation, is that I tend to create 2 different implementations of a function, like this:
https://github.com/dhylands/bioloid3/bl ... .py#L3-L12

If the functions are large enough, then I'll put them in a separate file.

I'll often create a CPython main function and a micropython main function that both use the same core python modules and use abstractions for the actual hardware. So I'll wrap the serial comms and have a micropython USB version, a micropython UART version, a CPython UART version, and a TCP/IP socket version, and the core code uses whatever one it's passed.

stijn
Posts: 735
Joined: Thu Apr 24, 2014 9:13 am

Re: Trying to understand uos vs os

Post by stijn » Sun Apr 08, 2018 7:03 am

liudr wrote:
Sat Apr 07, 2018 7:28 pm
(PC/Mac/nix/RPI/BBB)
These should all be able to run MicroPython as well, so instead of spending/wasting a lot of time trying to get everything to work with both MicroPython and CPython you could just stick to the former, if that is an option?

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Trying to understand uos vs os

Post by pythoncoder » Sun Apr 08, 2018 8:03 am

The MicroPython library is largely a subset of the CPython one so porting an application to Unix MicroPython can involve some work. But it then doesn't usually take much effort to backport it to CPython.

Running on a mixture of bare metal hardware platforms can involve a degree of hardware specific code. Pin definitions and the disparate approaches of pyb and machine come to mind here.
Peter Hinch
Index to my micropython libraries.

User avatar
liudr
Posts: 211
Joined: Tue Oct 17, 2017 5:18 am

Re: Trying to understand uos vs os

Post by liudr » Sun Apr 08, 2018 3:41 pm

pythoncoder wrote:
Sun Apr 08, 2018 8:03 am
The MicroPython library is largely a subset of the CPython one so porting an application to Unix MicroPython can involve some work. But it then doesn't usually take much effort to backport it to CPython.

Running on a mixture of bare metal hardware platforms can involve a degree of hardware specific code. Pin definitions and the disparate approaches of pyb and machine come to mind here.
Yep, xPython is only platform transparent as long as you don't involve platform-specific things such as pins. That was the exact reason I don't use any raspberry pi GPIO pins for anything other than some trivial tasks such as a button. I would design boards with USB-serial chips and MCU so they do the digital interface with sensors, instead of the computer via Python. MicroPython is extremely useful on MCUs but also extremely limited if you wish to use on computers (I didn't know this option even existed, will investigate). No or hard-to-find well-documented exception handling, for instance, prevents developers from preparing programs for exceptions say from networking. Everything is assuming ideal situations in sample code, like that of Arduino. That will work for hours or even for days in reality but will break down when you think you've done a good job. I'd like to see MicroPython tying up loose ends like these. In Arduino, many things have begin but no end.

For now I'll have two versions, one for cPython and another for MicroPython. They look very different on the init part (starting serial port, starting sd, mounting sd, setting up network etc.) but then start to look the same on actual processing part and then exception handling is different. There is no __str__() for exceptions :) Good thing is I'm already done writing processing part so it's just the tweaking, such as MicroPython doesn't have this and I'll use that, then back port to cPython to use only that.

One example: MicroPython can't turn byte string into float but cPython 3.x can. OK, then I'll add encode() to both script versions.

User avatar
liudr
Posts: 211
Joined: Tue Oct 17, 2017 5:18 am

Re: Trying to understand uos vs os

Post by liudr » Mon Apr 09, 2018 5:43 am

stijn wrote:
Sun Apr 08, 2018 7:03 am
liudr wrote:
Sat Apr 07, 2018 7:28 pm
(PC/Mac/nix/RPI/BBB)
These should all be able to run MicroPython as well, so instead of spending/wasting a lot of time trying to get everything to work with both MicroPython and CPython you could just stick to the former, if that is an option?
OK I got MicroPython compiled and running on a raspberry pi. Thing is, I can't import regular modules, only u-version can be imported. I'll see how useful this port is.

Post Reply