Home > Enterprise >  How can I write this code in a more efficient way?
How can I write this code in a more efficient way?

Time:05-17

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)

CodePudding user response:

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.

  • Related