Re: Question on difference between LambdaType and FunctionType
Ian Kelly wrote: > What about: > > __init__ = lambda self: setattr(self, 'foo', 'bar') That's an edge-case alright. Fortunately, I've decided to not skip lambdas. That's too problematic, it's easier to parse them as a special-case. Thanks for pointing this out. Sincerely, Iwo Herka -- https://mail.python.org/mailman/listinfo/python-list
Error Python version 3.6 does not support this syntax.
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.", 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* Full 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") def start_scan(self): """Start bluetooth scanning process.""" try: out = self.get_output("scan on") *except BluetoothctlError, e:print(e)return None* def make_discoverable(self): """Make device discoverable.""" try: out = self.get_output("discoverable on") * except BluetoothctlError, e:print(e)return None* 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) 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_available_devices(self): """Return a list of tuples of paired and discoverable devices.""" try: out = self.get_output("devices") *except BluetoothctlError, e:print(e)return None* else: available_devices = [] for line in out: device = self.parse_device_info(line) if device: available_devices.append(device) return available_devices def get_paired_devices(self): """Return a list of tuples of paired devices.""" try: out = self.get_output("paired-devices") *except BluetoothctlError, 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 def get_discoverable_devices(self): """Filter paired devices out of available.""" available = self.get_available_devices() paired = self.get_paired_devices() return [d for d in available if d not in paired] def get_device_info(self, mac_address): """Get device info by mac address.""" try: out = self.get_output("info " + mac_address) *except BluetoothctlError, e:print(e)return None* else: return out def pair(self, mac_address): """Try to pair with a device by mac address.""" try: out = self.get_output("pair " + mac_address, 4) *except BluetoothctlError, e:print(e)return None* else: res = self.child.expect(["Failed to pair", "Pairing successful", pexpect.EOF]) success = True if res == 1 else False return success def remove(self, mac_address): """Remove paired device by mac address, return success of the operation.""" try: out = self.get_output("remove " + mac_address, 3) *except BluetoothctlError, e:print(e)return None* else: res = self.child.expect(["not available", "Device has been removed", pexpect.EOF]) success = True if res == 1 else False return succes
Re: Error Python version 3.6 does not support this syntax.
On Tue, 2018-11-27 at 13:50 +0100, srinivasan wrote: > > *except BluetoothctlError, e:* > I don't have python3.6 available, but I believe the proper syntax is: except BluetoothctlError as e: print(e) See: https://docs.python.org/3/tutorial/errors.html?highlight=exception HTW -- https://mail.python.org/mailman/listinfo/python-list
Re: Error Python version 3.6 does not support this syntax.
"srinivasan" wrote in message news:cafstbwec-ww7agrwtotk5z8fhlrfa1hocoobkv7bcyds1be...@mail.gmail.com... 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.", 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* Here is a quick, untested, reply. If it does not work for you, please post again with the output. The correct syntax for Python 3.x is except BluetoothctlError as e: The line 'print(e)' should then work. In the line 'return None', it may be complaining that None is superfluous - a plain 'return' does the same thing. HTH Frank Millman -- https://mail.python.org/mailman/listinfo/python-list
Re: Error Python version 3.6 does not support this syntax.
On 11/27/2018 7: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 Created in 2015 ) 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.", No version of Python 3 supports the the old exception statement syntax. Python 2.7, released in 2010, supports both the old and the new syntax. I am not sure about 2.6 and before. It is odd to me that someone would use both the new print syntax and old except syntax together in 2015. 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:* If you run the code through the lib2to3 converter it will change ', ' to ' as '. I have not Full Code snippet: Not needed, however start_scan and make_discoverable are a bit flakey. As written, out is discarded making the assignment to out useless and the 'return None' redundant (since the function always returns None). I suspect that this is a bug. def start_scan(self): """Start bluetooth scanning process.""" try: out = self.get_output("scan on") except BluetoothctlError, e: print(e)return None -- Terry Jan Reedy -- https://mail.python.org/mailman/listinfo/python-list
Re: Error Python version 3.6 does not support this syntax.
On 11/27/2018 8:24 AM, Frank Millman wrote: In the line 'return None', it may be complaining that None is superfluous - a plain 'return' does the same thing. PEP 8 recommends explicit 'return None' if elsewhere in the function there is an explicit 'return something', as in if condition: return makelist(args) else: return None (though in the above, 'return []' might be better). However, in the quoted code, there is no other return (this might be a bug). -- Terry Jan Reedy -- https://mail.python.org/mailman/listinfo/python-list
PEP 572 -- Assignment Expressions
Hello. Maybe it's too late for a discussion, but I just couldn't resist. I just found out about this new ":=" operator. I need to ask: What is the need for this additional ":" to the "="? Why: if (match := pattern.search(data)) is not None: # Do something with match What is wrong with: if match = pattern.search(data) is not None: # Do something with match Assignment expression or assignment statement, it's an assignment, right? It is very clear to everyone that it's an assignment! Can't it all just be a "="? Thank you very much! Kind Regards Ivo Shipkaliev -- https://mail.python.org/mailman/listinfo/python-list
Re: PEP 572 -- Assignment Expressions
On Wed, Nov 28, 2018 at 4:38 AM Ivo Shipkaliev wrote: > >Hello. >Maybe it's too late for a discussion, but I just couldn't resist. >I just found out about this new ":=" operator. I need to ask: > >What is the need for this additional ":" to the "="? >Why: > if (match := pattern.search(data)) is not None: ># Do something with match > >What is wrong with: > if match = pattern.search(data) is not None: ># Do something with match > >Assignment expression or assignment statement, it's an assignment, > right? It is very clear to everyone that it's an assignment! Can't it all > just be a "="? >Thank you very much! It's a bug magnet. https://www.python.org/dev/peps/pep-0572/#why-not-just-turn-existing-assignment-into-an-expression ChrisA -- https://mail.python.org/mailman/listinfo/python-list
Re: [Tutor] Error Python version 3.6 does not support this syntax.
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* > -- https://mail.python.org/mailman/listinfo/python-list
Re: PEP 572 -- Assignment Expressions
On 11/27/2018 5:48 AM, Ivo Shipkaliev wrote: Hello. Maybe it's too late for a discussion, but I just couldn't resist. I just found out about this new ":=" operator. I need to ask: What is the need for this additional ":" to the "="? Did you read the PEP? It answers the question. Why: if (match := pattern.search(data)) is not None: # Do something with match What is wrong with: if match = pattern.search(data) is not None: # Do something with match match = pattern.search(data) is not None returns True or False, which is then assigned to match. On the other hand, in (match := pattern.search(data)) is not None match is assigned the result of the search; then that is compared to None. I am glad to see this added to python. I first encountered embedded assignment in 1969 in the fortran iv compiler for the GE 415 computer. Love at first sight. -- Bob Gailer -- https://mail.python.org/mailman/listinfo/python-list