From 907a2dd561de4296d227ba9f379754a625183139 Mon Sep 17 00:00:00 2001 From: Andrey Pohilko Date: Fri, 27 Dec 2019 10:34:04 +0300 Subject: [PATCH] Cosmetics --- examples/automata/bot.py | 1 - examples/bb8joystick/bb8.py | 9 ++++-- examples/demo.py | 59 ++++++++++++++++++++----------------- examples/tracker/color.json | 12 ++++++-- pylgbst/comms/cpygatt.py | 1 - pylgbst/messages.py | 2 +- pylgbst/utilities.py | 2 +- tests/descr.json | 4 --- 8 files changed, 50 insertions(+), 40 deletions(-) diff --git a/examples/automata/bot.py b/examples/automata/bot.py index e336ee2..9345dd2 100644 --- a/examples/automata/bot.py +++ b/examples/automata/bot.py @@ -32,4 +32,3 @@ def read_color(): action_by_color(color) print(number) - diff --git a/examples/bb8joystick/bb8.py b/examples/bb8joystick/bb8.py index b9b4e2c..b88b78f 100644 --- a/examples/bb8joystick/bb8.py +++ b/examples/bb8joystick/bb8.py @@ -22,7 +22,8 @@ class _SpheroImproved(spheropy.Sphero): async def connect(self, search_name=None, address=None, port=None, bluetooth_interface=None, use_ble=False, num_retry_attempts=1): gattool = BLEInterfaceGattool(search_name) - return await super().connect(search_name, address, port, gattool, use_ble, num_retry_attempts) + return await + super().connect(search_name, address, port, gattool, use_ble, num_retry_attempts) async def sleep(self, sleeptime, reset_inactivity_timeout=True, response_timeout_in_seconds=None): # port from https://github.com/jchadwhite/SpheroBB8-python/blob/master/BB8_driver.py#L394 @@ -33,7 +34,8 @@ async def sleep(self, sleeptime, reset_inactivity_timeout=True, response_timeout wait_for_response=False, reset_inactivity_timeout=reset_inactivity_timeout) - return await self._send_command(command, response_timeout_in_seconds) + return await + self._send_command(command, response_timeout_in_seconds) async def set_rotation_rate(self, rate, reset_inactivity_timeout=True, response_timeout_in_seconds=None): # port from https://github.com/jchadwhite/SpheroBB8-python/blob/master/BB8_driver.py @@ -44,7 +46,8 @@ async def set_rotation_rate(self, rate, reset_inactivity_timeout=True, response_ wait_for_response=False, reset_inactivity_timeout=reset_inactivity_timeout) - return await self._send_command(command, response_timeout_in_seconds) + return await + self._send_command(command, response_timeout_in_seconds) class BB8(object): diff --git a/examples/demo.py b/examples/demo.py index b4e0ff3..dc3dd62 100644 --- a/examples/demo.py +++ b/examples/demo.py @@ -3,8 +3,7 @@ from time import sleep from pylgbst import * -from pylgbst.comms import DebugServerConnection -from pylgbst.hub import MoveHub, math +from pylgbst.hub import MoveHub from pylgbst.peripherals import EncodedMotor, TiltSensor, Current, Voltage, COLORS, COLOR_BLACK log = logging.getLogger("demo") @@ -13,9 +12,11 @@ def demo_led_colors(movehub): # LED colors demo log.info("LED colors demo") + # We get a response with payload and port, not x and y here... def colour_callback(**named): log.info("LED Color callback: %s", named) + movehub.led.subscribe(colour_callback) for color in list(COLORS.keys())[1:] + [COLOR_BLACK]: log.info("Setting LED color to: %s", COLORS[color]) @@ -183,55 +184,59 @@ def demo_all(movehub): demo_color_sensor(movehub) demo_motor_sensors(movehub) + DEMO_CHOICES = { - 'all':demo_all, - 'voltage':demo_voltage, - 'led_colors':demo_led_colors, - 'motors_timed':demo_motors_timed, - 'motors_angled':demo_motors_angled, - 'port_cd_motor':demo_port_cd_motor, - 'tilt_sensor':demo_tilt_sensor_simple, - 'tilt_sensor_precise':demo_tilt_sensor_precise, - 'color_sensor':demo_color_sensor, - 'motor_sensors':demo_motor_sensors, + 'all': demo_all, + 'voltage': demo_voltage, + 'led_colors': demo_led_colors, + 'motors_timed': demo_motors_timed, + 'motors_angled': demo_motors_angled, + 'port_cd_motor': demo_port_cd_motor, + 'tilt_sensor': demo_tilt_sensor_simple, + 'tilt_sensor_precise': demo_tilt_sensor_precise, + 'color_sensor': demo_color_sensor, + 'motor_sensors': demo_motor_sensors, } + def get_options(): - import argparse - parser = argparse.ArgumentParser( + import argparse + arg_parser = argparse.ArgumentParser( description='Demonstrate move-hub communications', ) - parser.add_argument( - '-c','--connection', + arg_parser.add_argument( + '-c', '--connection', default='auto://', help='''Specify connection URL to use, `protocol://mac?param=X` with protocol in: "gatt","pygatt","gattlib","gattool", "bluepy","bluegiga"''' ) - parser.add_argument( - '-d','--demo', + arg_parser.add_argument( + '-d', '--demo', default='all', - choices = sorted(DEMO_CHOICES.keys()), + choices=sorted(DEMO_CHOICES.keys()), help="Run a particular demo, default all" ) - return parser + return arg_parser + def connection_from_url(url): import pylgbst if url == 'auto://': - return None + return None try: from urllib.parse import urlparse, parse_qs except ImportError: from urlparse import urlparse, parse_qs parsed = urlparse(url) - name = 'get_connection_%s'%(parsed.scheme) - factory = getattr( pylgbst, name, None) + name = 'get_connection_%s' % parsed.scheme + factory = getattr(pylgbst, name, None) if not factory: - raise ValueError("Unrecognised URL scheme/protocol, expect a get_connection_ in pylgbst: %s"%(parsed.protocol)) + msg = "Unrecognised URL scheme/protocol, expect a get_connection_ in pylgbst: %s" + raise ValueError(msg % parsed.protocol) params = {} if parsed.netloc.strip(): params['hub_mac'] = parsed.netloc - for key,value in parse_qs(parsed.query).items(): + for key, value in parse_qs(parsed.query).items(): if len(value) == 1: params[key] = value[0] else: @@ -240,6 +245,7 @@ def connection_from_url(url): **params ) + if __name__ == '__main__': logging.basicConfig(level=logging.INFO) parser = get_options() @@ -249,8 +255,7 @@ def connection_from_url(url): connection = connection_from_url(options.connection) parameters['connection'] = connection except ValueError as err: - parser.error(error.args[0]) - connection + parser.error(err.args[0]) hub = MoveHub(**parameters) try: diff --git a/examples/tracker/color.json b/examples/tracker/color.json index 969728f..d43d9e0 100644 --- a/examples/tracker/color.json +++ b/examples/tracker/color.json @@ -1,4 +1,12 @@ [ - [125, 64, 64], - [145, 255, 250] + [ + 125, + 64, + 64 + ], + [ + 145, + 255, + 250 + ] ] \ No newline at end of file diff --git a/pylgbst/comms/cpygatt.py b/pylgbst/comms/cpygatt.py index 55e7021..d62afc9 100644 --- a/pylgbst/comms/cpygatt.py +++ b/pylgbst/comms/cpygatt.py @@ -56,7 +56,6 @@ def is_alive(self): return True - class BlueGigaConnection(GattoolConnection): def __init__(self): super(BlueGigaConnection, self).__init__() diff --git a/pylgbst/messages.py b/pylgbst/messages.py index 46792ba..733db90 100644 --- a/pylgbst/messages.py +++ b/pylgbst/messages.py @@ -174,7 +174,7 @@ def bytes(self): def is_reply(self, msg): if not isinstance(msg, MsgHubAction): - raise TypeError("Unexpected message type: %s"%(msg.__class__,)) + raise TypeError("Unexpected message type: %s" % (msg.__class__,)) if self.action == self.DISCONNECT and msg.action == self.UPSTREAM_DISCONNECT: return True diff --git a/pylgbst/utilities.py b/pylgbst/utilities.py index 7ee2c4d..4031576 100644 --- a/pylgbst/utilities.py +++ b/pylgbst/utilities.py @@ -19,7 +19,7 @@ def check_unpack(seq, index, pattern, size): """Check that we got size bytes, if so, unpack using pattern""" - data = seq[index : index + size] + data = seq[index: index + size] if len(data) == size: return unpack(pattern, data)[0] else: diff --git a/tests/descr.json b/tests/descr.json index cf270b0..c1001e5 100644 --- a/tests/descr.json +++ b/tests/descr.json @@ -622,7 +622,6 @@ ] ] }, - "LEDRGB on port 0x32": { "mode_count": 2, "input_modes": [], @@ -694,7 +693,6 @@ "can_input": false } }, - "ColorDistanceSensor on port 0x1": { "mode_count": 11, "input_modes": [ @@ -1010,7 +1008,6 @@ ] ] }, - "TiltSensor on port 0x3a": { "mode_count": 8, "input_modes": [ @@ -1266,7 +1263,6 @@ ] ] }, - "Current on port 0x3b": { "mode_count": 2, "input_modes": [