1.9.0 to_bytes() method issue



  • in build 1.7.8
    >>> a=100
    >>> a.to_bytes(1)
    b'd'

    In the last build 1.9.0.b1

        >>> a = 100
                >>> a.to_bytes(1)
                Traceback (most recent call last):
                  File "<stdin>", line 1, in <module>
                TypeError: function missing 1 required positional arguments
    

    The problem is that no matter what we put additionally after is rejected

                       >>> a.to_bytes(1,''big)
                    Traceback (most recent call last):
                      File "<stdin>", line 1
                    SyntaxError: invalid syntax
                    >>> a.to_bytes(1,'big')
                    Traceback (most recent call last):
                      File "<stdin>", line 1, in <module>
                    NotImplementedError: 
                    >>> a.to_bytes(1,byteorder='little')
                    Traceback (most recent call last):
                      File "<stdin>", line 1, in <module>
                    TypeError: function does not take keyword arguments
                    >>> a.to_bytes(1,byteorder='little')
                    Traceback (most recent call last):
                      File "<stdin>", line 1, in <module>
                    TypeError: function does not take keyword arguments
                    >>> 
                    >>> 
                    >>> a.to_bytes(1,byteorder='little', signed+False)
                    Traceback (most recent call last):
                      File "<stdin>", line 1, in <module>
                    SyntaxError: non-keyword arg after keyword arg
                    >>> a.to_bytes(1,byteorder='little', signed=False)
                    Traceback (most recent call last):
                      File "<stdin>", line 1, in <module>
                    TypeError: function does not take keyword arguments
                    >>> a.to_bytes(1,signed=False)
                    Traceback (most recent call last):
                      File "<stdin>", line 1, in <module>
                    TypeError: function does not take keyword arguments
                    >>> a.to_bytes(1,'big',True)
                    Traceback (most recent call last):
                      File "<stdin>", line 1, in <module>
                    NotImplementedError: 
                    >>>


  • I was dealing with same issue before understanding why "big" was not working

    Anyway, I found in my case that pack with parameters to choose format (endianess, number size, ... ) it's so easy, that this page saved my day

    # >=Big Endian h=Short (2 bytes)
    >>> from struct import *
    >>> pack(">h", 100)
    b'\x00d'
    # <=Little Endian h=Short (2 bytes)
    >>> pack("<h", 100)
    b'd\x00'
    # >=Big Endian i=Int (4 bytes)
    >>> pack(">i", 100)
    b'\x00\x00\x00d'
    # <=Little Endian i=Int (4 bytes)
    >>> pack("<i", 100)
    b'd\x00\x00\x00'
    

    example in my pytrack sending GPS data + battery level as byte array

    lat = int(lat*1000000)
    lon = int(lon*1000000)
    alt = int(alt)
    bat = int(bat)
    
    # Big Endian, int, int, short, short
    payload = pack('>iihh', lat, lon, alt, bat)
    print(payload)
    


  • Than you. This wasn't tested... however..... we have a lot of changes in the code because of it.



  • @colateral

    >>> a = 100
    >>> a.to_bytes(1, "little")
    b'd'
    >>> a.to_bytes(2, "little")
    b'd\x00'
    >>> uos.uname()
    (sysname='LoPy', nodename='LoPy', release='1.9.0.b1', version='3f93fc2 on 2017-10-03', machine='LoPy with ESP32', lorawan='1.0.0')
    
    

    "big" is not implemented, "little" is mandatory for consistency to standard python)


 

Pycom on Twitter