pybricks._common.System: add storage method

This adds stubs, docs and code completion tests for the System.storage()
method.

This was added to Pybricks MicroPython in:
https://github.com/pybricks/pybricks-micropython/commit/246ebea25d25d5a3e4a4e62ebd031491a409c923
This commit is contained in:
David Lechner
2022-10-21 12:35:34 -05:00
parent 96be47f21b
commit 40fc2588f9
14 changed files with 101 additions and 1 deletions
+1
View File
@@ -6,6 +6,7 @@
### Added
- Code auto-completion for `EssentialHub`.
- Added `System.storage` method.
### Fixed
- Fixed some type hints in `parameters` submodule.
+2
View File
@@ -33,6 +33,8 @@ City Hub
.. automethod:: pybricks.hubs::CityHub.system.name
.. automethod:: pybricks.hubs::CityHub.system.storage
.. automethod:: pybricks.hubs::CityHub.system.shutdown
.. automethod:: pybricks.hubs::CityHub.system.reset_reason
+2
View File
@@ -65,6 +65,8 @@ Essential Hub
.. automethod:: pybricks.hubs::EssentialHub.system.name
.. automethod:: pybricks.hubs::EssentialHub.system.storage
.. automethod:: pybricks.hubs::EssentialHub.system.shutdown
.. automethod:: pybricks.hubs::EssentialHub.system.reset_reason
+2
View File
@@ -45,6 +45,8 @@ Move Hub
.. automethod:: pybricks.hubs::MoveHub.system.name
.. automethod:: pybricks.hubs::MoveHub.system.storage
.. automethod:: pybricks.hubs::MoveHub.system.shutdown
.. automethod:: pybricks.hubs::MoveHub.system.reset_reason
+2
View File
@@ -104,6 +104,8 @@ Prime Hub / Inventor Hub
.. automethod:: pybricks.hubs::PrimeHub.system.name
.. automethod:: pybricks.hubs::PrimeHub.system.storage
.. automethod:: pybricks.hubs::PrimeHub.system.shutdown
.. automethod:: pybricks.hubs::PrimeHub.system.reset_reason
+2
View File
@@ -47,6 +47,8 @@ Technic Hub
.. automethod:: pybricks.hubs::TechnicHub.system.name
.. automethod:: pybricks.hubs::TechnicHub.system.storage
.. automethod:: pybricks.hubs::TechnicHub.system.shutdown
.. automethod:: pybricks.hubs::TechnicHub.system.reset_reason
+1
View File
@@ -79,4 +79,5 @@ def test_hub_dot_system_dot():
"reset_reason",
"set_stop_button",
"shutdown",
"storage",
]
@@ -106,4 +106,5 @@ def test_hub_dot_system_dot():
"reset_reason",
"set_stop_button",
"shutdown",
"storage",
]
+1
View File
@@ -135,4 +135,5 @@ def test_hub_dot_system_dot():
"reset_reason",
"set_stop_button",
"shutdown",
"storage",
]
+1
View File
@@ -90,4 +90,5 @@ def test_hub_dot_system_dot():
"reset_reason",
"set_stop_button",
"shutdown",
"storage",
]
+1
View File
@@ -135,4 +135,5 @@ def test_hub_dot_system_dot():
"reset_reason",
"set_stop_button",
"shutdown",
"storage",
]
+1
View File
@@ -94,4 +94,5 @@ def test_hub_dot_system_dot():
"reset_reason",
"set_stop_button",
"shutdown",
"storage",
]
+48 -1
View File
@@ -228,6 +228,15 @@ METHOD_PARAMS = [
),
pytest.param("pybricks.hubs", "MoveHub", "system.name", [([], "str")]),
pytest.param("pybricks.hubs", "MoveHub", "system.shutdown", [([], "None")]),
pytest.param(
"pybricks.hubs",
"MoveHub",
"system.storage",
[
(["offset: int", "*", "read: int"], "bytes"),
(["offset: int", "*", "write: bytes"], "None"),
],
),
pytest.param("pybricks.hubs", "MoveHub", "system.reset_reason", [([], "int")]),
pytest.param("pybricks.hubs", "CityHub", "light.on", [(["color: Color"], "None")]),
pytest.param("pybricks.hubs", "CityHub", "light.off", [([], "None")]),
@@ -256,6 +265,15 @@ METHOD_PARAMS = [
),
pytest.param("pybricks.hubs", "CityHub", "system.name", [([], "str")]),
pytest.param("pybricks.hubs", "CityHub", "system.shutdown", [([], "None")]),
pytest.param(
"pybricks.hubs",
"CityHub",
"system.storage",
[
(["offset: int", "*", "read: int"], "bytes"),
(["offset: int", "*", "write: bytes"], "None"),
],
),
pytest.param("pybricks.hubs", "CityHub", "system.reset_reason", [([], "int")]),
pytest.param(
"pybricks.hubs", "TechnicHub", "light.on", [(["color: Color"], "None")]
@@ -307,6 +325,15 @@ METHOD_PARAMS = [
),
pytest.param("pybricks.hubs", "TechnicHub", "system.name", [([], "str")]),
pytest.param("pybricks.hubs", "TechnicHub", "system.shutdown", [([], "None")]),
pytest.param(
"pybricks.hubs",
"TechnicHub",
"system.storage",
[
(["offset: int", "*", "read: int"], "bytes"),
(["offset: int", "*", "write: bytes"], "None"),
],
),
pytest.param("pybricks.hubs", "TechnicHub", "system.reset_reason", [([], "int")]),
pytest.param("pybricks.hubs", "PrimeHub", "light.on", [(["color: Color"], "None")]),
pytest.param("pybricks.hubs", "PrimeHub", "light.off", [([], "None")]),
@@ -408,6 +435,15 @@ METHOD_PARAMS = [
),
pytest.param("pybricks.hubs", "PrimeHub", "system.name", [([], "str")]),
pytest.param("pybricks.hubs", "PrimeHub", "system.shutdown", [([], "None")]),
pytest.param(
"pybricks.hubs",
"PrimeHub",
"system.storage",
[
(["offset: int", "*", "read: int"], "bytes"),
(["offset: int", "*", "write: bytes"], "None"),
],
),
pytest.param("pybricks.hubs", "PrimeHub", "system.reset_reason", [([], "int")]),
pytest.param(
"pybricks.hubs", "EssentialHub", "light.on", [(["color: Color"], "None")]
@@ -464,6 +500,15 @@ METHOD_PARAMS = [
),
pytest.param("pybricks.hubs", "EssentialHub", "system.name", [([], "str")]),
pytest.param("pybricks.hubs", "EssentialHub", "system.shutdown", [([], "None")]),
pytest.param(
"pybricks.hubs",
"EssentialHub",
"system.storage",
[
(["offset: int", "*", "read: int"], "bytes"),
(["offset: int", "*", "write: bytes"], "None"),
],
),
pytest.param("pybricks.hubs", "EssentialHub", "system.reset_reason", [([], "int")]),
# TODO: iodevices module here
pytest.param("pybricks.pupdevices", "DCMotor", "dc", [(["duty: Number"], "None")]),
@@ -830,6 +875,8 @@ def test_get_signature_for_methods(
# ensure signatures are stripped from doc comment
assert not sig["documentation"]["value"].startswith(f"{method}(")
for pi, p in zip_longest(sig["parameters"], params):
for pi, p in zip_longest(
sig["parameters"], filter(lambda p: p != "*" and p != "/", params)
):
assert pi["label"] == p
assert pi["documentation"]["value"]
+36
View File
@@ -68,6 +68,42 @@ class System:
The hub name.
"""
@overload
def storage(self, offset: int, *, read: int) -> bytes:
...
@overload
def storage(self, offset: int, *, write: bytes) -> None:
...
def storage(self, offset, read=None, write=None):
"""
storage(self, offset, read=) -> bytes
storage(self, offset, write=)
Reads or writes binary data to persistent storage.
The available storage size is 128 bytes on Move, City and Technic hubs
and 512 bytes on SPIKE and MINDSTORMS hubs.
.. note:: The data is written to flash memory on shutdown, so will only
be saved if the hub is properly shut down. It will not be saved if
the batteries are removed while the hub is still running.
Args:
offset (int): The offset from the start of the user storage memory in bytes.
read (int): The number of bytes to read. Omit this argument when writing.
write (bytes): The bytes to write. Omit this argument when reading.
Returns:
The bytes read if reading, otherwise ``None``.
Raises:
ValueError:
The offset and/or the size of bytes to be read/written are
outside of the range of the allocated memory.
"""
class DCMotor:
"""Generic class to control simple motors without rotation sensors, such