From 28ec2791d119597ae2ad7dd9659498512e1fc4a2 Mon Sep 17 00:00:00 2001 From: Israel Blancas Date: Mon, 16 Oct 2017 00:15:17 +0200 Subject: [PATCH] Do some things in a more pythonic way --- src/aiy/_drivers/_button.py | 2 +- src/aiy/_drivers/_led.py | 2 +- src/aiy/assistant/auth_helpers.py | 2 +- src/aiy/assistant/grpc.py | 2 +- src/aiy/audio.py | 6 +++--- src/aiy/cloudspeech.py | 2 +- src/aiy/voicehat.py | 6 +++--- src/assistant_grpc_demo.py | 4 ++-- src/cloudspeech_demo.py | 2 +- 9 files changed, 14 insertions(+), 14 deletions(-) diff --git a/src/aiy/_drivers/_button.py b/src/aiy/_drivers/_button.py index 1d556992..7c133c69 100644 --- a/src/aiy/_drivers/_button.py +++ b/src/aiy/_drivers/_button.py @@ -82,7 +82,7 @@ def MyButtonPressHandler(channel): my_button.on_press(MyButtonPressHandler) """ GPIO.remove_event_detect(self.channel) - if callback is not None: + if callback: self.callback = callback GPIO.add_event_detect( self.channel, self.polarity, callback=self._debounce_and_callback) diff --git a/src/aiy/_drivers/_led.py b/src/aiy/_drivers/_led.py index 44951723..9578a1c2 100644 --- a/src/aiy/_drivers/_led.py +++ b/src/aiy/_drivers/_led.py @@ -90,7 +90,7 @@ def _animate(self): running = self.running if not running: return - if state is not None: + if state: if not self._parse_state(state): raise ValueError('unsupported state: %d' % state) if self.iterator: diff --git a/src/aiy/assistant/auth_helpers.py b/src/aiy/assistant/auth_helpers.py index d09dc644..3fd39ac5 100644 --- a/src/aiy/assistant/auth_helpers.py +++ b/src/aiy/assistant/auth_helpers.py @@ -122,6 +122,6 @@ def _try_to_get_credentials(client_secrets): def get_assistant_credentials(credentials_file=None): - if credentials_file is None: + if not credentials_file: credentials_file = _ASSISTANT_CREDENTIALS_FILE return _try_to_get_credentials(credentials_file) diff --git a/src/aiy/assistant/grpc.py b/src/aiy/assistant/grpc.py index 6ac11f70..b66024d4 100644 --- a/src/aiy/assistant/grpc.py +++ b/src/aiy/assistant/grpc.py @@ -71,7 +71,7 @@ def get_assistant(): aiy.audio.play_audio(audio) """ global _assistant_recognizer - if _assistant_recognizer is None: + if not _assistant_recognizer: credentials = aiy.assistant.auth_helpers.get_assistant_credentials() _assistant_recognizer = _AssistantRecognizer(credentials) return _assistant_recognizer diff --git a/src/aiy/audio.py b/src/aiy/audio.py index dd3b0c42..7711acc7 100644 --- a/src/aiy/audio.py +++ b/src/aiy/audio.py @@ -66,7 +66,7 @@ def get_player(): audio. """ global _voicehat_player - if _voicehat_player is None: + if not _voicehat_player: _voicehat_player = aiy._drivers._player.Player() return _voicehat_player @@ -78,7 +78,7 @@ def get_recorder(): use this. """ global _voicehat_recorder - if _voicehat_recorder is None: + if not _voicehat_recorder: _voicehat_recorder = aiy._drivers._recorder.Recorder() return _voicehat_recorder @@ -126,6 +126,6 @@ def get_status_ui(): of statuses it is able to communicate with the LED on the Voicehat. """ global _status_ui - if _status_ui is None: + if not _status_ui: _status_ui = aiy._drivers._StatusUi() return _status_ui diff --git a/src/aiy/cloudspeech.py b/src/aiy/cloudspeech.py index 56f7a981..c38ea54d 100644 --- a/src/aiy/cloudspeech.py +++ b/src/aiy/cloudspeech.py @@ -79,6 +79,6 @@ def get_recognizer(): turn_off_light() """ global _cloudspeech_recognizer - if _cloudspeech_recognizer is None: + if not _cloudspeech_recognizer: _cloudspeech_recognizer = _CloudSpeechRecognizer(CLOUDSPEECH_CREDENTIALS_FILE) return _cloudspeech_recognizer diff --git a/src/aiy/voicehat.py b/src/aiy/voicehat.py index 3ee8d4d0..7953341b 100644 --- a/src/aiy/voicehat.py +++ b/src/aiy/voicehat.py @@ -58,7 +58,7 @@ def on_button_press(_): # Calling wait_for_press() also cancels any callback. """ global _voicehat_button - if _voicehat_button is None: + if not _voicehat_button: _voicehat_button = aiy._drivers._button.Button(channel=_GPIO_BUTTON) return _voicehat_button @@ -76,7 +76,7 @@ def get_led(): led.set_state(aiy.voicehat.LED_OFF) """ global _voicehat_led - if _voicehat_led is None: + if not _voicehat_led: _voicehat_led = aiy._drivers._led.LED(channel=_GPIO_LED) _voicehat_led.start() return _voicehat_led @@ -104,6 +104,6 @@ def get_status_ui(): aiy.voicehat.get_status_ui().set_state('thinking') """ global _status_ui - if _status_ui is None: + if not _status_ui: _status_ui = aiy._drivers._status_ui._StatusUi() return _status_ui diff --git a/src/assistant_grpc_demo.py b/src/assistant_grpc_demo.py index f610871a..23456e19 100755 --- a/src/assistant_grpc_demo.py +++ b/src/assistant_grpc_demo.py @@ -40,13 +40,13 @@ def main(): status_ui.status('listening') print('Listening...') text, audio = assistant.recognize() - if text is not None: + if text: if text == 'goodbye': status_ui.status('stopping') print('Bye!') break print('You said "', text, '"') - if audio is not None: + if audio: aiy.audio.play_audio(audio) diff --git a/src/cloudspeech_demo.py b/src/cloudspeech_demo.py index e3de3c10..13eedfbf 100755 --- a/src/cloudspeech_demo.py +++ b/src/cloudspeech_demo.py @@ -35,7 +35,7 @@ def main(): button.wait_for_press() print('Listening...') text = recognizer.recognize() - if text is None: + if not text: print('Sorry, I did not hear you.') else: print('You said "', text, '"')