I wrote a script that pulls all the cards in my food expenses list that are separated by months. I then extract all the numbers and log the sum to a file. Later I plan on graphing my monthly expenses and the names.


from trollop import TrelloConnection
import logging 
# Lots of issues with Python3. Lots of unicode, string errors, Just switched to 
# py2. should try to use dicts for {name: cost} and to  practice using dicts 

# TODO: Data Visualization, Error/exception handling, appending to log for running total
# TODO: clean up code, get feedback on reddit, maybe use args for spec params
# TODO: set up cron job to run every month

api_key = '####' # TRELLO_API_KEY
token = '####'
# idBoard': '577b17583e5d17ee55b20e44',
# idList': '577b17583e5d17ee55b20e45',
# Set up basic logging
logging.basicConfig(format='%(levelname)s %(message)s',
    level=logging.INFO, filename='trello_expenses.log', filemode='w')

# Establish connection
conn = TrelloConnection(api_key, token)
months = ['January', 'February', 'July', 'August', \
          'September','October','November', 'December']
# Get expenses board via ID

logging.info("Iterating through boards...")

# for board in conn.me.boards:
#     # logging.info("id: {}".format(each._id))
#     # print u"board name: {0}, board id {1}" . format( board.name, board._id )  
#     logging.info (u"board name: {0}, board id {1}".\
#                     format( board.name, board._id ))  

# for each_card in conn.me.boards[2].cards:
#     print u"{0},{1},{2}".format(each_card._id, each_card.url,each_card.desc)  

def main():
    total = 0.0
    costs = list()
    names = list()

    # get first index i.e the list im currently working on
    board = conn.get_board('BE89pW61')
    board_lists = board.lists
    current = board_lists[0]
    # Get all lists  names and ids in board
    for entry in board_lists:

        if 'august' in entry.name.lower():
            # print u'List name: {0} list id: {1}'.format (entry.name, entry._id)

            for cards in entry.cards:
                name, cost =  cards.name.split('-')
                costs.append(float (cost) )
                # print cards.name
            # print costs
    total = sum (costs)
    print 'total is {}'.format( total )

if __name__=='__main__':
  • \$\begingroup\$ board_lists is a useless variable and since it looks very similar to board.lists, it is a potential source of confusion later. Just use board.lists unless you need to do something special to a copy of board.lists that you don't want to do to board.lists itself. In your code as is, you are just inspecting the contents, so it should be OK. \$\endgroup\$ Aug 21, 2016 at 17:13
  • \$\begingroup\$ current appears unused. It looks like you have commented out lines for debugging so it is a little uncertain what "correct" output for your code should be \$\endgroup\$ Aug 21, 2016 at 17:17
  • \$\begingroup\$ Welcome to Code Review! Hope you get some nice answers! \$\endgroup\$
    – Graipher
    Aug 21, 2016 at 18:18

2 Answers 2


It is good to see that you are using the if __name__=='__main__': guard. However, I think you are doing a bit too much in your main and I would make a get_total_month function like this:

def get_total_month(month, board_lists):
    costs = 0
    month = month.lower()
    for entry in board_lists:
        if month in entry.name.lower():
            for card in entry.cards:
                costs += float(card.name.split('-')[1])
    return costs

def main():
    board = conn.get_board('BE89pW61')
    total = get_total_month('august', board.lists)
    print 'total is {}'.format(total)

This way it will be easier to incorporate for example something like:

totals_per month = [get_total_month(month, boards.lists) for month in months]

which is then easy to plot e.g. with matplotlib:

import matplotlib.pyplot as plt

totals = [get_total_month(month, boards.lists) for month in months]
months = range(len(months))

plt.plot(months, totals)

Adding the actual names of the month (and maybe also their year) is a bit more involved, but you can have a look at the examples here and here.

Regarding whitespace, try to stick to one convention, preferentially the one set by pythons offical style-guie, PEP8. This means in particular not writing sum (total) or float( entry ), but sum(total) and float(entry).

  • \$\begingroup\$ I see you got rid of my costs list(). I'm assuming that just having costs as a float instead of a list I append to, makes the code more compact and faster? \$\endgroup\$ Aug 23, 2016 at 15:59
  • \$\begingroup\$ Should I now zip my months and totals ? \$\endgroup\$ Aug 23, 2016 at 16:04
  • \$\begingroup\$ @user3577138 Yes, int addition is faster than list.append and more memory efficient if you don't need the individual results. For the zip, do you mean for plotting? \$\endgroup\$
    – Graipher
    Aug 23, 2016 at 17:11

Can you take a look at this line:

logging.info("Iterating through boards...")

It will be executed even if you import this file. Maybe you should move it (and the related code) to the main section?


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.