docs/library/index: Update docs after umodule rename.
- Update guide for extending built-in modules. - Remove any last trace of umodule in other docs. This work was funded through GitHub Sponsors. Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
This commit is contained in:
parent
5fd042e7d1
commit
8211d56712
@ -50,7 +50,7 @@ low all of the time.
|
|||||||
|
|
||||||
* Example of a smooth frequency change::
|
* Example of a smooth frequency change::
|
||||||
|
|
||||||
from utime import sleep
|
from time import sleep
|
||||||
from machine import Pin, PWM
|
from machine import Pin, PWM
|
||||||
|
|
||||||
F_MIN = 500
|
F_MIN = 500
|
||||||
@ -75,7 +75,7 @@ low all of the time.
|
|||||||
|
|
||||||
* Example of a smooth duty change::
|
* Example of a smooth duty change::
|
||||||
|
|
||||||
from utime import sleep
|
from time import sleep
|
||||||
from machine import Pin, PWM
|
from machine import Pin, PWM
|
||||||
|
|
||||||
DUTY_MAX = 2**16 - 1
|
DUTY_MAX = 2**16 - 1
|
||||||
|
@ -191,34 +191,27 @@ The following libraries are specific to the Zephyr port.
|
|||||||
Extending built-in libraries from Python
|
Extending built-in libraries from Python
|
||||||
----------------------------------------
|
----------------------------------------
|
||||||
|
|
||||||
Many built-in modules are actually named ``umodule`` rather than ``module``, but
|
A subset of the built-in modules are able to be extended by Python code by
|
||||||
MicroPython will alias any module prefixed with a ``u`` to the non-``u``
|
providing a module of the same name in the filesystem. This extensibility
|
||||||
version. This means that, for example, ``import time`` will first attempt to
|
applies to the following Python standard library modules which are built-in to
|
||||||
resolve from the filesystem, and then failing that will fall back to the
|
the firmware: ``array``, ``binascii``, ``collections``, ``errno``, ``hashlib``,
|
||||||
built-in ``utime``. On the other hand, ``import utime`` will always go directly
|
``heapq``, ``io``, ``json``, ``os``, ``platform``, ``random``, ``re``,
|
||||||
to the built-in.
|
``select``, ``socket``, ``ssl``, ``struct``, ``time`` ``zlib``, as well as the
|
||||||
|
MicroPython-specific ``machine`` module. All other built-in modules cannot be
|
||||||
|
extended from the filesystem.
|
||||||
|
|
||||||
This allows the user to provide an extended implementation of a built-in library
|
This allows the user to provide an extended implementation of a built-in library
|
||||||
(perhaps to provide additional CPython compatibility or missing functionality).
|
(perhaps to provide additional CPython compatibility or missing functionality).
|
||||||
The user-provided module (in ``module.py``) can still use the built-in
|
This is used extensively in :term:`micropython-lib`, see :ref:`packages` for
|
||||||
functionality by importing ``umodule`` directly (e.g. typically an extension
|
more information. The filesystem module will typically do a wildcard import of
|
||||||
module ``time.py`` will do ``from utime import *``). This is used extensively
|
the built-in module in order to inherit all the globals (classes, functions and
|
||||||
in :term:`micropython-lib`. See :ref:`packages` for more information.
|
variables) from the built-in.
|
||||||
|
|
||||||
This extensibility applies to the following Python standard library modules
|
In MicroPython v1.21.0 and higher, to prevent the filesystem module from
|
||||||
which are built-in to the firmware: ``array``, ``binascii``, ``collections``,
|
importing itself, it can force an import of the built-in module it by
|
||||||
``errno``, ``hashlib``, ``heapq``, ``io``, ``json``, ``os``, ``platform``,
|
temporarily clearing ``sys.path`` during the import. For example, to extend the
|
||||||
``random``, ``re``, ``select``, ``socket``, ``ssl``, ``struct``, ``sys``,
|
``time`` module from Python, a file named ``time.py`` on the filesystem would
|
||||||
``time``, ``zlib``, as well as the MicroPython-specific libraries: ``bluepy``,
|
do the following::
|
||||||
``bluetooth``, ``machine``, ``timeq``, ``websocket``. All other built-in
|
|
||||||
modules cannot be extended from the filesystem.
|
|
||||||
|
|
||||||
*Other than when you specifically want to force the use of the built-in module,
|
|
||||||
we recommend always using* ``import module`` *rather than* ``import umodule``.
|
|
||||||
|
|
||||||
**Note:** In MicroPython v1.21.0 and higher, it is now possible to force an
|
|
||||||
import of the built-in module by clearing ``sys.path`` during the import. For
|
|
||||||
example, in ``time.py``, you can write::
|
|
||||||
|
|
||||||
_path = sys.path
|
_path = sys.path
|
||||||
sys.path = ()
|
sys.path = ()
|
||||||
@ -228,6 +221,25 @@ example, in ``time.py``, you can write::
|
|||||||
sys.path = _path
|
sys.path = _path
|
||||||
del _path
|
del _path
|
||||||
|
|
||||||
This is now the preferred way (instead of ``from utime import *``), as the
|
def extra_method():
|
||||||
``u``-prefix will be removed from the names of built-in modules in a future
|
pass
|
||||||
version of MicroPython.
|
|
||||||
|
The result is that ``time.py`` contains all the globals of the built-in ``time``
|
||||||
|
module, but adds ``extra_method``.
|
||||||
|
|
||||||
|
In earlier versions of MicroPython, you can force an import of a built-in module
|
||||||
|
by appending a ``u`` to the start of its name. For example, ``import utime``
|
||||||
|
instead of ``import time``. For example, ``time.py`` on the filesystem could
|
||||||
|
look like::
|
||||||
|
|
||||||
|
from utime import *
|
||||||
|
|
||||||
|
def extra_method():
|
||||||
|
pass
|
||||||
|
|
||||||
|
This way is still supported, but the ``sys.path`` method described above is now
|
||||||
|
preferred as the ``u``-prefix will be removed from the names of built-in
|
||||||
|
modules in a future version of MicroPython.
|
||||||
|
|
||||||
|
*Other than when it specifically needs to force the use of the built-in module,
|
||||||
|
code should always use* ``import module`` *rather than* ``import umodule``.
|
||||||
|
@ -34,5 +34,5 @@ Methods
|
|||||||
|
|
||||||
These methods implement the simple and extended
|
These methods implement the simple and extended
|
||||||
:ref:`block protocol <block-device-interface>` defined by
|
:ref:`block protocol <block-device-interface>` defined by
|
||||||
:class:`uos.AbstractBlockDev`.
|
:class:`os.AbstractBlockDev`.
|
||||||
|
|
||||||
|
@ -37,4 +37,4 @@ Methods
|
|||||||
|
|
||||||
These methods implement the simple and extended
|
These methods implement the simple and extended
|
||||||
:ref:`block protocol <block-device-interface>` defined by
|
:ref:`block protocol <block-device-interface>` defined by
|
||||||
:class:`uos.AbstractBlockDev`.
|
:class:`os.AbstractBlockDev`.
|
||||||
|
@ -12,9 +12,8 @@ To list supported modules, please enter::
|
|||||||
|
|
||||||
help('modules')
|
help('modules')
|
||||||
|
|
||||||
Especially `machine` module and class :ref:`machine.Pin <machine.Pin>` are very important for using
|
Especially `machine` module and class :ref:`machine.Pin <machine.Pin>` are very
|
||||||
peripherals. Note that prefix 'u' is added to the module for MicroPython,
|
important for using peripherals.
|
||||||
so you can see "umachine" in the list but you can use it like "import machine".
|
|
||||||
|
|
||||||
Using "from machine import Pin", Pin name is available corresponding to
|
Using "from machine import Pin", Pin name is available corresponding to
|
||||||
the RA MCU's pin name which are Pin.cpu.P000 and 'P000'.
|
the RA MCU's pin name which are Pin.cpu.P000 and 'P000'.
|
||||||
|
@ -19,7 +19,7 @@ See the corresponding section of the tutorial: :ref:`intro`.
|
|||||||
Delay and timing
|
Delay and timing
|
||||||
----------------
|
----------------
|
||||||
|
|
||||||
Use the :mod:`time <utime>` module::
|
Use the :mod:`time <time>` module::
|
||||||
|
|
||||||
import time
|
import time
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user