From 52b3b4c007a71f386a94d8abc167121a2bb730a2 Mon Sep 17 00:00:00 2001 From: Laurens Valk Date: Thu, 4 Apr 2024 21:40:55 +0200 Subject: [PATCH] pybricks.tools.read_input_byte: Add last and chr options. --- CHANGELOG.md | 1 + ...s_blockReadInput_read_input_first_byte.svg | 989 ++++++++++++++++++ ...s_blockReadInput_read_input_first_char.svg | 989 ++++++++++++++++++ ...ks_blockReadInput_read_input_last_byte.svg | 989 ++++++++++++++++++ ...ks_blockReadInput_read_input_last_char.svg | 989 ++++++++++++++++++ doc/main/tools/index.rst | 16 + jedi/tests/test_get_signature.py | 6 +- src/pybricks/tools.py | 17 +- 8 files changed, 3991 insertions(+), 5 deletions(-) create mode 100644 doc/main/blockimg/pybricks_blockReadInput_read_input_first_byte.svg create mode 100644 doc/main/blockimg/pybricks_blockReadInput_read_input_first_char.svg create mode 100644 doc/main/blockimg/pybricks_blockReadInput_read_input_last_byte.svg create mode 100644 doc/main/blockimg/pybricks_blockReadInput_read_input_last_char.svg diff --git a/CHANGELOG.md b/CHANGELOG.md index db4b263..a8b064d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ - Added `pybricks.pupdevices.Remote.disconnect` method. - Added blocks for `up`, `ready` and `stationary` for IMUs. +- Added `last` and `chr` parameters to `read_input_byte` and add blocks. ## 3.5.0b1 - 2024-03-11 diff --git a/doc/main/blockimg/pybricks_blockReadInput_read_input_first_byte.svg b/doc/main/blockimg/pybricks_blockReadInput_read_input_first_byte.svg new file mode 100644 index 0000000..0e7dfea --- /dev/null +++ b/doc/main/blockimg/pybricks_blockReadInput_read_input_first_byte.svg @@ -0,0 +1,989 @@ + +readfirst byte \ No newline at end of file diff --git a/doc/main/blockimg/pybricks_blockReadInput_read_input_first_char.svg b/doc/main/blockimg/pybricks_blockReadInput_read_input_first_char.svg new file mode 100644 index 0000000..1096f59 --- /dev/null +++ b/doc/main/blockimg/pybricks_blockReadInput_read_input_first_char.svg @@ -0,0 +1,989 @@ + +readfirst key \ No newline at end of file diff --git a/doc/main/blockimg/pybricks_blockReadInput_read_input_last_byte.svg b/doc/main/blockimg/pybricks_blockReadInput_read_input_last_byte.svg new file mode 100644 index 0000000..bd0e8d7 --- /dev/null +++ b/doc/main/blockimg/pybricks_blockReadInput_read_input_last_byte.svg @@ -0,0 +1,989 @@ + +readlast byte \ No newline at end of file diff --git a/doc/main/blockimg/pybricks_blockReadInput_read_input_last_char.svg b/doc/main/blockimg/pybricks_blockReadInput_read_input_last_char.svg new file mode 100644 index 0000000..7c978b5 --- /dev/null +++ b/doc/main/blockimg/pybricks_blockReadInput_read_input_last_char.svg @@ -0,0 +1,989 @@ + +readlast key \ No newline at end of file diff --git a/doc/main/tools/index.rst b/doc/main/tools/index.rst index 232b812..2316b68 100644 --- a/doc/main/tools/index.rst +++ b/doc/main/tools/index.rst @@ -37,8 +37,24 @@ Timing tools Input tools ----------- +.. blockimg:: pybricks_blockReadInput_read_input_first_byte + +.. blockimg:: pybricks_blockReadInput_read_input_first_char + :stack: + +.. blockimg:: pybricks_blockReadInput_read_input_last_byte + :stack: + +.. blockimg:: pybricks_blockReadInput_read_input_last_char + :stack: + .. autofunction:: pybricks.tools.read_input_byte +.. versionchanged:: 3.3 + + Added ``last`` and ``chr`` options. + + .. pybricks-requirements:: light-matrix .. autofunction:: pybricks.tools.hub_menu diff --git a/jedi/tests/test_get_signature.py b/jedi/tests/test_get_signature.py index d6c24ee..b64ce15 100644 --- a/jedi/tests/test_get_signature.py +++ b/jedi/tests/test_get_signature.py @@ -29,7 +29,11 @@ def _get_function_signature(module: str, function: str) -> SignatureHelp: FUNCTION_PARAMS = [ - pytest.param("pybricks.tools", "read_input_byte", [([], "Optional[int]")]), + pytest.param( + "pybricks.tools", + "read_input_byte", + [(["last: bool=False", "chr: bool=False"], "Optional[int | str]")], + ), pytest.param("pybricks.tools", "wait", [(["time: Number"], "MaybeAwaitable")]), pytest.param( "pybricks.tools", diff --git a/src/pybricks/tools.py b/src/pybricks/tools.py index 44acc07..ff02af3 100644 --- a/src/pybricks/tools.py +++ b/src/pybricks/tools.py @@ -211,14 +211,23 @@ def cross(a: Matrix, b: Matrix) -> Matrix: """ -def read_input_byte() -> Optional[int]: +def read_input_byte(last: bool = False, chr: bool = False) -> Optional[int | str]: """ - read_input_byte() -> int | None + read_input_byte() -> int | str | None - Reads one byte from standard input without blocking. + Reads one byte from standard input without blocking and removes it from the + input buffer. + + Arguments: + last (bool): Choose ``True`` to read the last (most recent) byte in the buffer and discard the rest. + Choose ``False`` to read only the first (oldest) byte. + chr (bool): Choose ``True`` to convert the result to a one-character string. Returns: - The numeric value of the byte read or ``None`` if no data is available. + The byte that was read, as a numeric value (``0`` to ``255``) or + string (e.g. ``"B"``). Returns ``None`` if no data is available. If + ``chr=True``, it also return ``None`` if the byte that was read is not + printable as a character. """