如何以更有效的方式编写此代码?

发布于 2025-01-29 09:00:57 字数 2554 浏览 1 评论 0原文

该代码重复很多,我试图优化它,但失败了。有建议吗?

我正在使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

缱绻入梦 2025-02-05 09:00:57

您可以拥有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 several for loops and a single long section to parse rows.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文