From 0982d5d65b712f70ce33c5ea9cbd6f886cc2f5a6 Mon Sep 17 00:00:00 2001 From: Laurens Valk Date: Fri, 10 Jun 2022 15:43:09 +0200 Subject: [PATCH] ubuiltins: Document int. --- doc/main/micropython/builtins.rst | 16 +---------- src/ubuiltins/__init__.py | 45 +++++++++++++++++-------------- 2 files changed, 26 insertions(+), 35 deletions(-) diff --git a/doc/main/micropython/builtins.rst b/doc/main/micropython/builtins.rst index 7e5fed1..3ce655f 100644 --- a/doc/main/micropython/builtins.rst +++ b/doc/main/micropython/builtins.rst @@ -36,26 +36,12 @@ Basic types .. pybricks-requirements:: -.. - _comment: The int class is defined manually because otherwise the classmethod is hidden, see https://github.com/pybricks/pybricks-api/issues/86 - .. autoclass:: ubuiltins.int :no-members: .. automethod:: ubuiltins.int.to_bytes - .. classmethod:: from_bytes(bytes: Union[bytes, bytearray], byteorder: Literal["little", "big"]) -> int - - Returns the integer represented by the given bytes. - - :param bytes: The bytes to convert. - :param byteorder: Determines the byte order used to represent the - integer. If byteorder is ``"big"``, the most significant byte is at - the beginning of the byte sequence. If byteorder is ``"little"``, - the most significant byte is at the end of the byte sequence. - - :returns: The integer represented by the bytes. - + .. automethod:: ubuiltins.int.from_bytes .. pybricks-requirements:: diff --git a/src/ubuiltins/__init__.py b/src/ubuiltins/__init__.py index 20a5c37..375d23a 100644 --- a/src/ubuiltins/__init__.py +++ b/src/ubuiltins/__init__.py @@ -436,13 +436,10 @@ class float: def __init__(self, *args) -> None: """float(x=0.0) - Converts an object to a floating point number. + Creates a floating point number from a given object. Arguments: x (int or float or str): Number or string to be converted. - - Returns: - The resulting floating point value. """ @@ -564,36 +561,44 @@ class int: ... def __init__(self, *args) -> None: - """int() - int(x: Union[int, float, str]) + """int(x=0) - Converts the argument to an integer. If no argument is given, this - returns ``0``. + Creates an integer. Arguments: - x: Number or string that will be converted. - - Returns: - The input argument ``x`` converted to an integer. + x (int or float or str): Object to be converted. """ def to_bytes(self, length: _int, byteorder: Literal["little", "big"]) -> _bytes: """ - Returns a :class:`bytes` object representing the integer. + to_bytes(length, byteorder) -> bytes + + Get a :class:`bytes` representation of the integer. Arguments: - length: How many bytes to use. - byteorder: Choose ``"little"`` for little-endian encoding - or ``"big"`` for big-endian encoding. + length (int): How many bytes to use. + byteorder (str): Choose ``"big"`` to put the most significant byte + first. Choose ``"little"`` to put the least significant byte + first. Returns: - The integer represented by a sequence of bytes. + Byte sequence that represents the integer. """ - @classmethod + # @classmethod def from_bytes(cls, _bytes: _bytes, byteorder: Literal["little", "big"]) -> _int: - """ - Returns the integer represented by the given array of bytes. + """from_bytes(bytes, byteorder) -> int + + Convert a byte sequence to the number it represents. + + Arguments: + bytes (bytes): The bytes to convert. + byteorder (str): Choose ``"big"`` if the most significant byte is + the first element. Choose ``"little"`` if the least significant + byte is the first element. + + Returns: + The number represented by the bytes. """