diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 61a49101e..815d7373c 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -87,6 +87,40 @@ specific conventions and guidelines for micropython-lib: packages](README.md#installing-packages-from-forks) in your Pull Request description. +### Module documentation + +Each package in micropython-lib is encouraged to include documentation in +the form of docstrings in the code itself. The top-level docstring in the main +module or package should provide an overview of the package's functionality, +usage examples, and any important notes or caveats. + +Note that the docstrings should be concise and relevant, even though they will +not be included in the cross-compiled bytecode and will not impact the size of +the module when installed via `mip`. + +When writing docstrings, please follow these guidelines: +* Use triple double quotes (`"""`) for docstrings. +* Multi-line docstrings should place the starting and ending triple quotes on + their own lines. +* Start with a brief summary of the module's purpose. +* Include usage examples where appropriate (indent examplecode blocks with 4 + spaces). +* Use proper indentation for multi-line docstrings to align with the code block. + +### Module versioning + +Each package in micropython-lib should include a `manifest.py` file that +specifies metadata about the package, including its version. The version +management is intentionally manual to ensure package stability and prevent +accidental version bumps. When making changes to a package that affect its +functionality or API, please update the version in `manifest.py`. The +`tools/build.py` script will detect new versions and add them to the index, but +won't increment versions automatically + +> [!NOTE] +> Changes to docstrings or comments that do not change the generated bytecode +> should not change the version. + ### Publishing packages from forks You can easily publish the packages from your micropython-lib diff --git a/micropython/bluetooth/aioble/README.md b/micropython/bluetooth/aioble/README.md index 83ae00209..559594850 100644 --- a/micropython/bluetooth/aioble/README.md +++ b/micropython/bluetooth/aioble/README.md @@ -93,7 +93,7 @@ async with aioble.scan(duration_ms=5000, interval_us=30000, window_us=30000, act # Either from scan result device = result.device # Or with known address -device = aioble.Device(aioble.PUBLIC, "aa:bb:cc:dd:ee:ff") +device = aioble.Device(aioble.ADDR_PUBLIC, "aa:bb:cc:dd:ee:ff") try: connection = await device.connect(timeout_ms=2000) diff --git a/micropython/bluetooth/aioble/examples/temp_sensor.py b/micropython/bluetooth/aioble/examples/temp_sensor.py index 54580f595..9a4ec26de 100644 --- a/micropython/bluetooth/aioble/examples/temp_sensor.py +++ b/micropython/bluetooth/aioble/examples/temp_sensor.py @@ -20,7 +20,7 @@ _ADV_APPEARANCE_GENERIC_THERMOMETER = const(768) # How frequently to send advertising beacons. -_ADV_INTERVAL_MS = 250_000 +_ADV_INTERVAL_US = 250_000 # Register GATT server. @@ -50,7 +50,7 @@ async def sensor_task(): async def peripheral_task(): while True: async with await aioble.advertise( - _ADV_INTERVAL_MS, + _ADV_INTERVAL_US, name="mpy-temp", services=[_ENV_SENSE_UUID], appearance=_ADV_APPEARANCE_GENERIC_THERMOMETER, diff --git a/micropython/drivers/imu/bmi270/bmi270.py b/micropython/drivers/imu/bmi270/bmi270.py index 64f819ec2..c76962f7e 100644 --- a/micropython/drivers/imu/bmi270/bmi270.py +++ b/micropython/drivers/imu/bmi270/bmi270.py @@ -21,29 +21,31 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -Basic example usage: +Basic example usage:: + + import time + from bmi270 import BMI270 + from machine import Pin, SPI, I2C + + # Init in I2C mode. + imu = BMI270(I2C(1, scl=Pin(15), sda=Pin(14))) + + # Or init in SPI mode. + # TODO: Not supported yet. + # imu = BMI270(SPI(5), cs=Pin(10)) + + while (True): + print('Accelerometer: x:{:>6.3f} y:{:>6.3f} z:{:>6.3f}'.format(*imu.accel())) + print('Gyroscope: x:{:>6.3f} y:{:>6.3f} z:{:>6.3f}'.format(*imu.gyro())) + print('Magnetometer: x:{:>8.3f} y:{:>8.3f} z:{:>8.3f}'.format(*imu.magnet())) + print("") + time.sleep_ms(100) -import time -from bmi270 import BMI270 -from machine import Pin, SPI, I2C - -# Init in I2C mode. -imu = BMI270(I2C(1, scl=Pin(15), sda=Pin(14))) - -# Or init in SPI mode. -# TODO: Not supported yet. -# imu = BMI270(SPI(5), cs=Pin(10)) - -while (True): - print('Accelerometer: x:{:>6.3f} y:{:>6.3f} z:{:>6.3f}'.format(*imu.accel())) - print('Gyroscope: x:{:>6.3f} y:{:>6.3f} z:{:>6.3f}'.format(*imu.gyro())) - print('Magnetometer: x:{:>8.3f} y:{:>8.3f} z:{:>8.3f}'.format(*imu.magnet())) - print("") - time.sleep_ms(100) """ import array import time + from micropython import const _DEFAULT_ADDR = const(0x68) diff --git a/micropython/drivers/imu/bmm150/bmm150.py b/micropython/drivers/imu/bmm150/bmm150.py index a4845c961..aea4348b5 100644 --- a/micropython/drivers/imu/bmm150/bmm150.py +++ b/micropython/drivers/imu/bmm150/bmm150.py @@ -21,22 +21,22 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -Basic example usage: +Basic example usage:: -import time -from bmm150 import BMM150 -from machine import Pin, SPI, I2C + import time + from bmm150 import BMM150 + from machine import Pin, SPI, I2C -# Init in I2C mode. -imu = BMM150(I2C(1, scl=Pin(15), sda=Pin(14))) + # Init in I2C mode. + imu = BMM150(I2C(1, scl=Pin(15), sda=Pin(14))) -# Or init in SPI mode. -# TODO: Not supported yet. -# imu = BMM150(SPI(5), cs=Pin(10)) + # Or init in SPI mode. + # TODO: Not supported yet. + # imu = BMM150(SPI(5), cs=Pin(10)) -while (True): - print('magnetometer: x:{:>8.3f} y:{:>8.3f} z:{:>8.3f}'.format(*imu.magnet())) - time.sleep_ms(100) + while (True): + print('magnetometer: x:{:>8.3f} y:{:>8.3f} z:{:>8.3f}'.format(*imu.magnet())) + time.sleep_ms(100) """ import array diff --git a/micropython/drivers/imu/lsm6dsox/lsm6dsox.py b/micropython/drivers/imu/lsm6dsox/lsm6dsox.py index ca1397c66..b932ff006 100644 --- a/micropython/drivers/imu/lsm6dsox/lsm6dsox.py +++ b/micropython/drivers/imu/lsm6dsox/lsm6dsox.py @@ -25,23 +25,24 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -Basic example usage: +Basic example usage:: -import time -from lsm6dsox import LSM6DSOX + import time + from lsm6dsox import LSM6DSOX + + from machine import Pin, SPI, I2C + # Init in I2C mode. + lsm = LSM6DSOX(I2C(0, scl=Pin(13), sda=Pin(12))) -from machine import Pin, SPI, I2C -# Init in I2C mode. -lsm = LSM6DSOX(I2C(0, scl=Pin(13), sda=Pin(12))) + # Or init in SPI mode. + #lsm = LSM6DSOX(SPI(5), cs=Pin(10)) -# Or init in SPI mode. -#lsm = LSM6DSOX(SPI(5), cs=Pin(10)) + while (True): + print('Accelerometer: x:{:>8.3f} y:{:>8.3f} z:{:>8.3f}'.format(*lsm.accel())) + print('Gyroscope: x:{:>8.3f} y:{:>8.3f} z:{:>8.3f}'.format(*lsm.gyro())) + print("") + time.sleep_ms(100) -while (True): - print('Accelerometer: x:{:>8.3f} y:{:>8.3f} z:{:>8.3f}'.format(*lsm.accel())) - print('Gyroscope: x:{:>8.3f} y:{:>8.3f} z:{:>8.3f}'.format(*lsm.gyro())) - print("") - time.sleep_ms(100) """ import array diff --git a/micropython/drivers/imu/lsm9ds1/lsm9ds1.py b/micropython/drivers/imu/lsm9ds1/lsm9ds1.py index e5a96ad5c..a45e73039 100644 --- a/micropython/drivers/imu/lsm9ds1/lsm9ds1.py +++ b/micropython/drivers/imu/lsm9ds1/lsm9ds1.py @@ -27,21 +27,21 @@ The sensor contains an accelerometer / gyroscope / magnetometer Uses the internal FIFO to store up to 16 gyro/accel data, use the iter_accel_gyro generator to access it. -Example usage: +Example usage:: -import time -from lsm9ds1 import LSM9DS1 -from machine import Pin, I2C + import time + from lsm9ds1 import LSM9DS1 + from machine import Pin, I2C -imu = LSM9DS1(I2C(1, scl=Pin(15), sda=Pin(14))) + imu = LSM9DS1(I2C(1, scl=Pin(15), sda=Pin(14))) -while (True): - #for g,a in imu.iter_accel_gyro(): print(g,a) # using fifo - print('Accelerometer: x:{:>8.3f} y:{:>8.3f} z:{:>8.3f}'.format(*imu.accel())) - print('Magnetometer: x:{:>8.3f} y:{:>8.3f} z:{:>8.3f}'.format(*imu.magnet())) - print('Gyroscope: x:{:>8.3f} y:{:>8.3f} z:{:>8.3f}'.format(*imu.gyro())) - print("") - time.sleep_ms(100) + while (True): + #for g,a in imu.iter_accel_gyro(): print(g,a) # using fifo + print('Accelerometer: x:{:>8.3f} y:{:>8.3f} z:{:>8.3f}'.format(*imu.accel())) + print('Magnetometer: x:{:>8.3f} y:{:>8.3f} z:{:>8.3f}'.format(*imu.magnet())) + print('Gyroscope: x:{:>8.3f} y:{:>8.3f} z:{:>8.3f}'.format(*imu.gyro())) + print("") + time.sleep_ms(100) """ import array diff --git a/micropython/drivers/sensor/ds18x20/ds18x20.py b/micropython/drivers/sensor/ds18x20/ds18x20.py index ad2d9f52c..066a05fb9 100644 --- a/micropython/drivers/sensor/ds18x20/ds18x20.py +++ b/micropython/drivers/sensor/ds18x20/ds18x20.py @@ -1,5 +1,8 @@ -# DS18x20 temperature sensor driver for MicroPython. -# MIT license; Copyright (c) 2016 Damien P. George +""" +DS18x20 temperature sensor driver for MicroPython. + +MIT license; Copyright (c) 2016 Damien P. George +""" from micropython import const diff --git a/micropython/drivers/sensor/hs3003/hs3003.py b/micropython/drivers/sensor/hs3003/hs3003.py index 003501649..332df8de7 100644 --- a/micropython/drivers/sensor/hs3003/hs3003.py +++ b/micropython/drivers/sensor/hs3003/hs3003.py @@ -22,21 +22,23 @@ THE SOFTWARE. HS3003 driver for MicroPython. +------------------------------ Example usage: -import time -from hs3003 import HS3003 -from machine import Pin, I2C + import time + from hs3003 import HS3003 + from machine import Pin, I2C -bus = I2C(1, scl=Pin(15), sda=Pin(14)) -hts = HS3003(bus) + bus = I2C(1, scl=Pin(15), sda=Pin(14)) + hts = HS3003(bus) + + while True: + rH = hts.humidity() + temp = hts.temperature() + print ("rH: %.2f%% T: %.2fC" %(rH, temp)) + time.sleep_ms(100) -while True: - rH = hts.humidity() - temp = hts.temperature() - print ("rH: %.2f%% T: %.2fC" %(rH, temp)) - time.sleep_ms(100) """ import struct diff --git a/micropython/drivers/sensor/hts221/hts221.py b/micropython/drivers/sensor/hts221/hts221.py index c6cd51f48..5be268d40 100644 --- a/micropython/drivers/sensor/hts221/hts221.py +++ b/micropython/drivers/sensor/hts221/hts221.py @@ -23,22 +23,24 @@ THE SOFTWARE. HTS221 driver driver for MicroPython. +------------------------------------- + Original source: https://github.com/ControlEverythingCommunity/HTS221/blob/master/Python/HTS221.py Example usage: -import time -import hts221 -from machine import Pin, I2C + import time + import hts221 + from machine import Pin, I2C -bus = I2C(1, scl=Pin(15), sda=Pin(14)) -hts = hts221.HTS221(bus) + bus = I2C(1, scl=Pin(15), sda=Pin(14)) + hts = hts221.HTS221(bus) -while (True): - rH = hts.humidity() - temp = hts.temperature() - print ("rH: %.2f%% T: %.2fC" %(rH, temp)) - time.sleep_ms(100) + while (True): + rH = hts.humidity() + temp = hts.temperature() + print ("rH: %.2f%% T: %.2fC" %(rH, temp)) + time.sleep_ms(100) """ import struct diff --git a/python-ecosys/requests/example_xively.py b/python-ecosys/requests/example_quote.py similarity index 85% rename from python-ecosys/requests/example_xively.py rename to python-ecosys/requests/example_quote.py index 60e139b98..cfbe8ac0e 100644 --- a/python-ecosys/requests/example_xively.py +++ b/python-ecosys/requests/example_quote.py @@ -1,6 +1,6 @@ import requests -r = requests.get("http://api.xively.com/") +r = requests.get("https://dummyjson.com/quotes/1") print(r) print(r.content) print(r.text) diff --git a/python-stdlib/cmd/cmd.py b/python-stdlib/cmd/cmd.py index 447ea1489..b05fc245d 100644 --- a/python-stdlib/cmd/cmd.py +++ b/python-stdlib/cmd/cmd.py @@ -1,20 +1,21 @@ -"""A generic class to build line-oriented command interpreters. +""" +A generic class to build line-oriented command interpreters. Interpreters constructed with this class obey the following conventions: 1. End of file on input is processed as the command 'EOF'. 2. A command is parsed out of each line by collecting the prefix composed of characters in the identchars member. -3. A command `foo' is dispatched to a method 'do_foo()'; the do_ method +3. A command 'foo' is dispatched to a method 'do_foo()'; the do_ method is passed a single argument consisting of the remainder of the line. 4. Typing an empty line repeats the last command. (Actually, it calls the - method `emptyline', which may be overridden in a subclass.) -5. There is a predefined `help' method. Given an argument `topic', it - calls the command `help_topic'. With no arguments, it lists all topics + method 'emptyline', which may be overridden in a subclass.) +5. There is a predefined 'help' method. Given an argument 'topic', it + calls the command 'help_topic'. With no arguments, it lists all topics with defined help_ functions, broken into up to three topics; documented commands, miscellaneous help topics, and undocumented commands. -6. The command '?' is a synonym for `help'. The command '!' is a synonym - for `shell', if a do_shell method exists. +6. The command '?' is a synonym for 'help'. The command '!' is a synonym + for 'shell', if a do_shell method exists. 7. If completion is enabled, completing commands will be done automatically, and completing of commands args is done by calling complete_foo() with arguments text, line, begidx, endidx. text is string we are matching @@ -23,21 +24,21 @@ indexes of the text being matched, which could be used to provide different completion depending upon which position the argument is in. -The `default' method may be overridden to intercept commands for which there +The 'default' method may be overridden to intercept commands for which there is no do_ method. -The `completedefault' method may be overridden to intercept completions for +The 'completedefault' method may be overridden to intercept completions for commands that have no complete_ method. -The data member `self.ruler' sets the character used to draw separator lines +The data member 'self.ruler' sets the character used to draw separator lines in the help messages. If empty, no ruler line is drawn. It defaults to "=". -If the value of `self.intro' is nonempty when the cmdloop method is called, +If the value of 'self.intro' is nonempty when the cmdloop method is called, it is printed out on interpreter startup. This value may be overridden via an optional argument to the cmdloop() method. -The data members `self.doc_header', `self.misc_header', and -`self.undoc_header' set the headers used for the help function's +The data members 'self.doc_header', 'self.misc_header', and +'self.undoc_header' set the headers used for the help function's listings of documented functions, miscellaneous topics, and undocumented functions respectively. @@ -48,7 +49,7 @@ One of the notable deviations is that since MicroPython strips doc strings, this means that that help by doc string feature doesn't work. -completions have also been stripped out. +Completions have also been stripped out. """ import sys diff --git a/python-stdlib/contextlib/contextlib.py b/python-stdlib/contextlib/contextlib.py index 3e598b4b6..2f6d4a928 100644 --- a/python-stdlib/contextlib/contextlib.py +++ b/python-stdlib/contextlib/contextlib.py @@ -1,10 +1,10 @@ -"""Utilities for with-statement contexts. See PEP 343. +""" +Utilities for with-statement contexts. See PEP 343. Original source code: https://hg.python.org/cpython/file/3.4/Lib/contextlib.py Not implemented: - redirect_stdout; - """ import sys @@ -15,12 +15,12 @@ class closing(object): """Context to automatically close something at the end of a block. - Code like this: + Code like this:: with closing(.open()) as f: - is equivalent to this: + is equivalent to this:: f = .open() try: diff --git a/python-stdlib/copy/copy.py b/python-stdlib/copy/copy.py index 0a9283777..9d56d6cbd 100644 --- a/python-stdlib/copy/copy.py +++ b/python-stdlib/copy/copy.py @@ -191,7 +191,7 @@ def deepcopy(x, memo=None, _nil=[]): if copier: y = copier(memo) else: - reductor = dispatch_table.get(cls) + reductor = _deepcopy_dispatch.get(cls) if reductor: rv = reductor(x) else: diff --git a/python-stdlib/copy/manifest.py b/python-stdlib/copy/manifest.py index b22ebeb90..f0849295f 100644 --- a/python-stdlib/copy/manifest.py +++ b/python-stdlib/copy/manifest.py @@ -1,4 +1,4 @@ -metadata(version="3.3.4") +metadata(version="3.3.5") require("types") diff --git a/python-stdlib/heapq/heapq.py b/python-stdlib/heapq/heapq.py index b11853b8d..792497e68 100644 --- a/python-stdlib/heapq/heapq.py +++ b/python-stdlib/heapq/heapq.py @@ -1,4 +1,5 @@ -"""Heap queue algorithm (a.k.a. priority queue). +""" +Heap queue algorithm (a.k.a. priority queue). Heaps are arrays for which a[k] <= a[2*k+1] and a[k] <= a[2*k+2] for all k, counting elements from 0. For the sake of comparison, @@ -7,13 +8,13 @@ Usage: -heap = [] # creates an empty heap -heappush(heap, item) # pushes a new item on the heap -item = heappop(heap) # pops the smallest item from the heap -item = heap[0] # smallest item on the heap without popping it -heapify(x) # transforms list into a heap, in-place, in linear time -item = heapreplace(heap, item) # pops and returns smallest item, and adds - # new item; the heap size is unchanged + heap = [] # creates an empty heap + heappush(heap, item) # pushes a new item on the heap + item = heappop(heap) # pops the smallest item from the heap + item = heap[0] # smallest item on the heap without popping it + heapify(x) # transforms list into a heap, in-place, in linear time + item = heapreplace(heap, item) # pops and returns smallest item, and adds + # new item; the heap size is unchanged Our API differs from textbook heap algorithms as follows: @@ -40,7 +41,7 @@ property of a heap is that a[0] is always its smallest element. The strange invariant above is meant to be an efficient memory -representation for a tournament. The numbers below are `k', not a[k]: +representation for a tournament. The numbers below are 'k', not a[k]:: 0 @@ -53,7 +54,7 @@ 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 -In the tree above, each cell `k' is topping `2*k+1' and `2*k+2'. In +In the tree above, each cell 'k' is topping '2*k+1' and '2*k+2'. In an usual binary tournament we see in sports, each cell is the winner over the two cells it tops, and we can trace the winner down the tree to see all opponents s/he had. However, in many computer applications @@ -108,7 +109,7 @@ effective! In a word, heaps are useful memory structures to know. I use them in -a few applications, and I think it is good to keep a `heap' module +a few applications, and I think it is good to keep a `heap` module around. :-) -------------------- @@ -377,7 +378,7 @@ def _siftup_max(heap, pos): def merge(*iterables): """Merge multiple sorted inputs into a single sorted output. - Similar to sorted(itertools.chain(*iterables)) but returns a generator, + Similar to `sorted(itertools.chain(*iterables))` but returns a generator, does not pull the data into memory all at once, and assumes that each of the input streams is already sorted (smallest to largest). diff --git a/python-stdlib/textwrap/textwrap.py b/python-stdlib/textwrap/textwrap.py index 4e9f35069..c5771bec6 100644 --- a/python-stdlib/textwrap/textwrap.py +++ b/python-stdlib/textwrap/textwrap.py @@ -1,4 +1,5 @@ -"""Text wrapping and filling. +""" +Text wrapping and filling. """ # Copyright (C) 1999-2001 Gregory P. Ward. @@ -169,7 +170,8 @@ def _split(self, text): return chunks def _fix_sentence_endings(self, chunks): - """_fix_sentence_endings(chunks : [string]) + """ + _fix_sentence_endings(chunks : [string]) Correct for sentence endings buried in 'chunks'. Eg. when the original text contains "... foo.\nBar ...", munge_whitespace()