如何以更有效的方式编写此代码?
How can I write this code in a more efficient way?
此代码重复较多,尝试优化失败。有什么建议吗?
我正在使用 python/flask 和 SQLite。
它是关于从数据库中获取数据并将其显示在图表中,每个塑料表格都有许多字段,我将它们的值保存在列表中以在图表上显示它们。
@tracker.route('/insight')
@login_required
def getInsight():
plastic = Plastic.query.filter_by(user_id=current_user.id)
dates = []
plasticBottle = []
plasticBag = []
plasticWrap = []
yoghurtBox = []
cottonSwab = []
detergentBottle = []
shampooBottle = []
toothbrush = []
toothpaste = []
takeawayBox = []
takeawayWrap = []
straw = []
disposableCutlery = []
plasticPlate = []
for row in plastic:
dates.append(row.creation_datetime)
plasticBottle.append(round(row.plasticBottle, 3)/1000)
plasticBag.append(round(row.plasticBag, 3)/1000)
plasticWrap.append(round(row.plasticWrap, 3)/1000)
yoghurtBox.append(round(row.yoghurtBox, 3)/1000)
cottonSwab.append(round(row.cottonSwab, 3)/1000)
detergentBottle.append(round(row.detergentBottle, 3)/1000)
shampooBottle.append(round(row.shampooBottle, 3)/1000)
toothbrush.append(round(row.toothbrush, 3)/1000)
toothpaste.append(round(row.toothpaste, 3)/1000)
takeawayBox.append(round(row.takeawayBox, 3)/1000)
takeawayWrap.append(round(row.takeawayWrap, 3)/1000)
straw.append(round(row.straw, 3)/1000)
disposableCutlery.append(round(row.disposableCutlery, 3)/1000)
plasticPlate.append(round(row.plasticPlate, 3)/1000)
line_chart = pygal.Line()
# naming the title
line_chart.title = 'The variation of your plastic consumption over time'
# set the range of plot
line_chart.x_labels = map(lambda d: d.strftime('%Y-%m-%d'), dates)
# adding lines
line_chart.add('plasticBottle', plasticBottle)
line_chart.add('plasticBag', plasticBag)
line_chart.add('plasticWrap', plasticWrap)
line_chart.add('yoghurtBox', yoghurtBox)
line_chart.add('cottonSwab', cottonSwab)
line_chart.add('detergentBottle', detergentBottle)
line_chart.add('shampooBottle', shampooBottle)
line_chart.add('toothbrush', toothbrush)
line_chart.add('toothpaste', toothpaste)
line_chart.add('takeawayBox', takeawayBox)
line_chart.add('takeawayWrap', takeawayWrap)
line_chart.add('straw', straw)
line_chart.add('disposableCutlery', disposableCutlery)
line_chart.add('plasticPlate', plasticPlate)
graph_data = line_chart.render_data_uri()
return render_template("insight.html", title='Insight', graph_data=graph_data)
你可以有一个 str
键的列表和一个从这个键到相应数组的字典映射。这样你将只有几个 for
循环和一个长的部分来解析行。
此代码重复较多,尝试优化失败。有什么建议吗?
我正在使用 python/flask 和 SQLite。
它是关于从数据库中获取数据并将其显示在图表中,每个塑料表格都有许多字段,我将它们的值保存在列表中以在图表上显示它们。
@tracker.route('/insight')
@login_required
def getInsight():
plastic = Plastic.query.filter_by(user_id=current_user.id)
dates = []
plasticBottle = []
plasticBag = []
plasticWrap = []
yoghurtBox = []
cottonSwab = []
detergentBottle = []
shampooBottle = []
toothbrush = []
toothpaste = []
takeawayBox = []
takeawayWrap = []
straw = []
disposableCutlery = []
plasticPlate = []
for row in plastic:
dates.append(row.creation_datetime)
plasticBottle.append(round(row.plasticBottle, 3)/1000)
plasticBag.append(round(row.plasticBag, 3)/1000)
plasticWrap.append(round(row.plasticWrap, 3)/1000)
yoghurtBox.append(round(row.yoghurtBox, 3)/1000)
cottonSwab.append(round(row.cottonSwab, 3)/1000)
detergentBottle.append(round(row.detergentBottle, 3)/1000)
shampooBottle.append(round(row.shampooBottle, 3)/1000)
toothbrush.append(round(row.toothbrush, 3)/1000)
toothpaste.append(round(row.toothpaste, 3)/1000)
takeawayBox.append(round(row.takeawayBox, 3)/1000)
takeawayWrap.append(round(row.takeawayWrap, 3)/1000)
straw.append(round(row.straw, 3)/1000)
disposableCutlery.append(round(row.disposableCutlery, 3)/1000)
plasticPlate.append(round(row.plasticPlate, 3)/1000)
line_chart = pygal.Line()
# naming the title
line_chart.title = 'The variation of your plastic consumption over time'
# set the range of plot
line_chart.x_labels = map(lambda d: d.strftime('%Y-%m-%d'), dates)
# adding lines
line_chart.add('plasticBottle', plasticBottle)
line_chart.add('plasticBag', plasticBag)
line_chart.add('plasticWrap', plasticWrap)
line_chart.add('yoghurtBox', yoghurtBox)
line_chart.add('cottonSwab', cottonSwab)
line_chart.add('detergentBottle', detergentBottle)
line_chart.add('shampooBottle', shampooBottle)
line_chart.add('toothbrush', toothbrush)
line_chart.add('toothpaste', toothpaste)
line_chart.add('takeawayBox', takeawayBox)
line_chart.add('takeawayWrap', takeawayWrap)
line_chart.add('straw', straw)
line_chart.add('disposableCutlery', disposableCutlery)
line_chart.add('plasticPlate', plasticPlate)
graph_data = line_chart.render_data_uri()
return render_template("insight.html", title='Insight', graph_data=graph_data)
你可以有一个 str
键的列表和一个从这个键到相应数组的字典映射。这样你将只有几个 for
循环和一个长的部分来解析行。