Use defaultdict to simplify code.
authorPhilipp Spitzer <philipp@spitzer.priv.at>
Wed, 4 Jul 2018 19:18:11 +0000 (21:18 +0200)
committerPhilipp Spitzer <philipp@spitzer.priv.at>
Wed, 4 Jul 2018 19:18:11 +0000 (21:18 +0200)
web/seepark_web.py

index 82b3323c4c558bb0b3280c883fa34d7ae46f7ea3..95c3f9dea734df366631f66bc8d3f1b12eb2aa01 100644 (file)
@@ -4,6 +4,7 @@ import time
 import configparser
 import os
 import sys
 import configparser
 import os
 import sys
+from collections import defaultdict
 from flask import Flask, render_template, jsonify, request
 import flask.json
 from sqlalchemy import create_engine
 from flask import Flask, render_template, jsonify, request
 import flask.json
 from sqlalchemy import create_engine
@@ -87,13 +88,9 @@ def select_sensordata(initial_where, initial_sql_args):
 
     format = request.args.get('format', 'default')
     if format == 'c3':
 
     format = request.args.get('format', 'default')
     if format == 'c3':
-        c3result = dict()
+        c3result = defaultdict(list)
         for row in result:
         for row in result:
-            if not row['sensor_id'] in c3result:
-                c3result[row['sensor_id']] = list()
             c3result[row['sensor_id']].append(row['value'])
             c3result[row['sensor_id']].append(row['value'])
-            if not row['sensor_id'] + '_x' in c3result:
-                c3result[row['sensor_id'] + '_x'] = list()
             dt = row['timestamp'].strftime('%Y-%m-%d %H:%M:%S')
             c3result[row['sensor_id'] + '_x'].append(dt)
         result = c3result
             dt = row['timestamp'].strftime('%Y-%m-%d %H:%M:%S')
             c3result[row['sensor_id'] + '_x'].append(dt)
         result = c3result