osdir.com

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Tutor] Error Python version 3.6 does not support this syntax.


Dear Mats,

Thanks a lot for your quick responses, again the below line seems to
be throwing the same error, is that should I again decode the line
where am facing the issue to str? or could you please let me if there
is any alternative solution for the same or workaround in python 3.6?

Code Snippet:

    def parse_device_info(self, info_string):
        """Parse a string corresponding to a device."""
        device = {}
        block_list = ["[\x1b[0;", "removed"]
        string_valid = not any(keyword in info_string for keyword in
block_list) ---------------------------> Again this line seems to be
the same issue

        if string_valid:
            try:
                device_position = info_string.index("Device")
            except ValueError:
                pass
            else:
                if device_position > -1:
                    attribute_list = info_string[device_position:].split(" ", 2)
                    device = {
                        "mac_address": attribute_list[1],
                        "name": attribute_list[2]
                    }

        return device

    def get_paired_devices(self):
        """Return a list of tuples of paired devices."""
        try:
            out = self.get_output("paired-devices")
        except BluetoothctlError as e:
            print(e)
            return None
        else:
            paired_devices = []
            for line in out:
                device = self.parse_device_info(line)
                if device:
                    paired_devices.append(device)

            return paired_devices

if __name__ == "__main__":

    print("Init bluetooth...")
    bl = Bluetoothctl()
    print("Ready!")
    bl.start_scan()
    print("Scanning for 10 seconds...")
    for i in range(0, 10):
        print(i)
        time.sleep(1)

    bl.pair("64:A2:F9:06:63:79")
    print("Pairing for 10 seconds...")
    for i in range(0, 10):
        print(i)
        time.sleep(1)

    # Seems to be an issue ----------
    bl.get_paired_devices()
    print("Getting Paired devices for 10 seconds...")
    for i in range(0, 10):
        print(i)
        time.sleep(1)

Error Logs:

Traceback (most recent call last):
, in parse_device_info
    string_valid = not any(keyword in info_string for keyword in block_list)
  File "/home/srinivasan/Downloads/bt_tests/qa/test_library/bt_tests.py",
line 52, in <genexpr>
    string_valid = not any(keyword in info_string for keyword in block_list)
TypeError: a bytes-like object is required, not 'str'

On Fri, Nov 30, 2018 at 1:18 AM Mats Wichmann <mats at wichmann.us> wrote:
>
> On 11/29/18 12:20 PM, srinivasan wrote:
> > Dear Python Experts,
> >
> > With the below code snippet, I am seeing the below error, I am using
> > python 3.6, could you please what could be the issue?
>
> >         self.child = pexpect.spawn("bluetoothctl", echo = False)
> ...
> >         self.child.send(command + "\n")
> >         time.sleep(pause)
> >         start_failed = self.child.expect(["bluetooth", pexpect.EOF])
> ...
> >         return self.child.before.split("\r\n")
> > ------------------------------------------------------------------------------->
> > the issue seems to be here
> > line 27, in get_output
> >     return self.child.before.split("\r\n")
> > TypeError: a bytes-like object is required, not 'str'
>
> your types don't match. it's Python 3 so what you get back from talking
> to an external process is a bytes object. you're calling the split
> method on that object, but passing it a string to split on - that's what
> the error is saying.  It shouldn't be any more complicated to fix that
> than to give it a byte object:
>
> return self.child.before.split(b"\r\n")
>
> or... decode the object to a str before splitting on a string.
>
> but since you're specifically splitting on lines, you may as well use
> the splitlines method instead of the split method, since that's what it
> is designed for.
>
>
>
>
>