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 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?

Code Snippet:
-------------------
import time
import pexpect
import subprocess
import sys

class BluetoothctlError(Exception):
    """This exception is raised, when bluetoothctl fails to start."""
    pass


class Bluetoothctl:
    """A wrapper for bluetoothctl utility."""

    def __init__(self):
        out = subprocess.check_output("rfkill unblock bluetooth", shell = True)
        self.child = pexpect.spawn("bluetoothctl", echo = False)

    def get_output(self, command, pause = 0):
        """Run a command in bluetoothctl prompt, return output as a
list of lines."""
        self.child.send(command + "\n")
        time.sleep(pause)
        start_failed = self.child.expect(["bluetooth", pexpect.EOF])

        if start_failed:
            raise BluetoothctlError("Bluetoothctl failed after running
" + command)

        return self.child.before.split("\r\n")
------------------------------------------------------------------------------->
the issue seems to be here

    def start_scan(self):
        """Start bluetooth scanning process."""
        try:
            out = self.get_output("scan on")
        except BluetoothctlError as e:
            print(e)
            return None

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)

Error Logs:
-------------
/home/srinivasan/Downloads/bt_tests/qa/venv/bin/python
/home/srinivasan/Downloads/bt_tests/qa/test_library/bt_tests.py
Init bluetooth...
Ready!
Traceback (most recent call last):
  File "/home/srinivasan/Downloads/bt_tests/qa/test_library/bt_tests.py",
line 169, in <module>
    bl.start_scan()
  File "/home/srinivasan/Downloads/bt_tests/qa/test_library/bt_tests.py",
line 32, in start_scan
    out = self.get_output("scan on")
  File "/home/srinivasan/Downloads/bt_tests/qa/test_library/bt_tests.py",
line 27, in get_output
    return self.child.before.split("\r\n")
TypeError: a bytes-like object is required, not 'str'

Process finished with exit code 1

On Wed, Nov 28, 2018 at 12:17 AM Mats Wichmann <mats at wichmann.us> wrote:
>
> On 11/27/18 5:50 AM, srinivasan wrote:
> > Dear Python Experts,
> >
> > As still I am newbie and learning python, I am trying to reuse the
> > Bluetoothctl wrapper in Python from the link (
> > https://gist.github.com/egorf/66d88056a9d703928f93) I am using python3.6
> > version, In pycharm editor on the bold highlighted code snippets I see the
> > error message "Python version 3.6 does not support this syntax.",
>
> once again you've posted in a way that inserts lots of extra crud, you
> avoided that last time.
>
> The syntax change is simple (and works on most older Pythons too):
>
> except ErrorType, e:
>
> becomes
>
> except ErrorType as e:
>
>
> >
> > Could you please how help me how the below highlighted lines of code can be
> > can be ported to python3.6 version?
> >
> > *        except BluetoothctlError, e:*
> >
> > *            print(e)*
> > *            return None*
> >