How to get machine-readable MicroPython API for particular firmware variant?

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
User avatar
aivarannamaa
Posts: 171
Joined: Fri Sep 22, 2017 3:19 pm
Location: Estonia
Contact:

How to get machine-readable MicroPython API for particular firmware variant?

Post by aivarannamaa » Tue Dec 26, 2017 8:06 am

Hi!

I want to enhance MicroPython support in my Python IDE Thonny (currently it only supports Micro:bit).

One aspect of the support would be code completion in scripts. It would be rather easy to do with jedi if I had bunch of Python files corresponding to MicroPython modules containing stubs of the functions (preferably together with docstrings). For add-on libraries I could use the actual Python source, but the problem is with built-in modules.

How can I generate these scripts for each Micropython variant (device * version)? Can I somehow use the source of the docs? The main source?

Do you see other options for providing code completion in scripts (as opposed to dynamic completion in the REPL)?
Aivar Annamaa
https://thonny.org

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

Re: How to get machine-readable MicroPython API for particular firmware variant?

Post by pfalcon » Tue Dec 26, 2017 12:39 pm

Build unix coverage version ("make coverage"). Then:

Code: Select all

$ ./micropython_coverage
MicroPython v1.9.3-183-ge37ccfe59-dirty on 2017-12-16; linux version
Use Ctrl-D to exit, Ctrl-E for paste mode
>>> import sys
>>> help(sys)
object <module 'sys'> is of type module
  __name__ -- sys
  path -- ['', '/home/pfalcon/.micropython/lib', '/usr/lib/micropython']
  argv -- []
  version -- 3.4.0
  version_info -- (3, 4, 0)
  implementation -- (name='micropython', version=(1, 9, 3))
  platform -- linux
  byteorder -- little
  maxsize -- 9223372036854775807
  exit -- <function>
  stdin -- <io.TextIOWrapper 0>
  stdout -- <io.TextIOWrapper 1>
  stderr -- <io.TextIOWrapper 2>
  modules -- {}
  exc_info -- <function>
  getsizeof -- <function>
  print_exception -- <function>
>>> 
As usual, not much different than with CPython, adjusted for "micro" in MicroPython.
Awesome MicroPython list
Pycopy - A better MicroPython https://github.com/pfalcon/micropython
MicroPython standard library for all ports and forks - https://github.com/pfalcon/micropython-lib
More up to date docs - http://pycopy.readthedocs.io/

User avatar
aivarannamaa
Posts: 171
Joined: Fri Sep 22, 2017 3:19 pm
Location: Estonia
Contact:

Re: How to get machine-readable MicroPython API for particular firmware variant?

Post by aivarannamaa » Wed Dec 27, 2017 9:55 am

I'm afraid I don't understand your advice. I see how I can fetch the list of modules and list of functions, but what about signatures?
Aivar Annamaa
https://thonny.org

User avatar
josverl
Posts: 15
Joined: Fri Nov 24, 2017 4:22 pm

Re: How to get machine-readable MicroPython API for particular firmware variant?

Post by josverl » Fri Jan 05, 2018 2:49 pm

If you run the below on the board and capture the output then you should be able to get the basics


import sys
sys.implementation.name
sys.implementation.version
sys.implementation
sys.platform


this will return something like:

'micropython'
(1, 9, 3)
'esp32'
(name='micropython', version=(1, 9, 3))

If you need to get the details of the build, then the only way I know of is to send a Ctrl-D ;) to be board, that will trigger a REPL reset
and then on most firmwares you get some additional info on the welcome prompt

MicroPython v1.9.3-220-g7a9a73ee on 2017-12-27; ESP32 module with ESP32

HTH
Last edited by josverl on Fri Jan 05, 2018 11:34 pm, edited 1 time in total.

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

Re: How to get machine-readable MicroPython API for particular firmware variant?

Post by dhylands » Fri Jan 05, 2018 3:24 pm

Typo: Control-D does a soft reset and shows the banner (Control-B is used to end the raw repl and switch back to the "friendly" repl).

Post Reply