如何以更有效的方式编写此代码?
该代码重复很多,我试图优化它,但失败了。有建议吗?
我正在使用python/烧瓶和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)
This code has a lot of repetition, I tried to optimise it but failed. Any advice?
I'm using python/flask and SQLite.
It's about getting data from the database and showing it in a graph, every plastic form has many fields where I'm saving their values in a list to show them on the graph.
@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)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以拥有
str
键的列表,以及从此键到相应数组的dict映射。这样,您只会有几个循环的,而一个长则可以分析行。
You can have a list of
str
keys and a dict mapping from this keys to corresponding arrays. This way you will only have severalfor
loops and a single long section to parse rows.