10000 iot: fix print formatting for gateway codelab (#2058) · dalequark/python-docs-samples@49125ba · GitHub
[go: up one dir, main page]

Skip to content

Commit 49125ba

Browse files
authored
iot: fix print formatting for gateway codelab (GoogleCloudPlatform#2058)
* iot: fix print formatting for gateway codelab * iot: fix lint errors
1 parent 4cd36ef commit 49125ba

File tree

2 files changed

+17
-10
lines changed

2 files changed

+17
-10
lines changed

iot/api-client/codelabs/lightsensor.py

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ def detect_light(device_id, sock):
8484
lux = "{:.3f}".format(LightSensor.lux)
8585

8686
sys.stdout.write(
87-
'\r>> ' + bcolors.CGREEN + bcolors.CBLINK + 'Lux: {}'.format(lux) +
87+
'\r>> ' + bcolors.CGREEN + bcolors.CBOLD + 'Lux: {}'.format(lux) +
8888
bcolors.ENDC + ' <<')
8989
sys.stdout.flush()
9090

@@ -95,9 +95,14 @@ def detect_light(device_id, sock):
9595

9696

9797
def print_sensor_state():
98-
print(
99-
'\nSensor is {}, reporting lux every {} seconds.'.format(
100-
LightSensor.power, LightSensor.interval))
98+
if LightSensor.power == 'on':
99+
print(
100+
'\nSensor is {}, reporting lux every {} seconds.'.format(
101+
LightSensor.power, LightSensor.interval))
102+
else:
103+
print(
104+
'\nSensor is {}. Send a configuration update to turn on'.format(
105+
LightSensor.power))
101106

102107

103108
def process_message(message):
@@ -172,11 +177,13 @@ def main():
172177
sys.exit(1)
173178
else:
174179
# Received data from the socket, so process the message.
175-
message = json.loads(data.decode("utf-8"))
176-
if not message:
177-
print('invalid json: {}'.format(data.decode("utf-8")))
178-
continue
179-
process_message(message)
180+
decode = data.decode("utf-8")
181+
if decode != '':
182+
message = json.loads(decode)
183+
if not message:
184+
print('invalid json: {}'.format(data.decode("utf-8")))
185+
continue
186+
process_message(message)
180187
finally:
181188
print('Closing socket')
182189
sock.close()

iot/api-client/codelabs/thermostat.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ def main():
7777
humidity = "{:.3f}".format(h)
7878
temperature = "{:.3f}".format(temperature_f)
7979
sys.stdout.write(
80-
'\r >>' + bcolors.CGREEN + bcolors.CBOLD +
80+
'\r>> ' + bcolors.CGREEN + bcolors.CBOLD +
8181
'Temp: {} F, Hum: {}%'.format(temperature, humidity) +
8282
bcolors.ENDC + ' <<')
8383
sys.stdout.flush()

0 commit comments

Comments
 (0)
0