patternpythonMinor
Trello list scraper that extracts all numbers from boards and logs the sum
Viewed 0 times
thelogstrelloallnumbersboardsextractsthatsumand
Problem
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.
```
#!/usr/bin/python
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,
```
#!/usr/bin/python
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,
Solution
It is good to see that you are using the
This way it will be easier to incorporate for example something like:
which is then easy to plot e.g. with matplotlib:
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
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)
plt.show()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).Code Snippets
...
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)totals_per month = [get_total_month(month, boards.lists) for month in months]import matplotlib.pyplot as plt
...
totals = [get_total_month(month, boards.lists) for month in months]
months = range(len(months))
plt.plot(months, totals)
plt.show()Context
StackExchange Code Review Q#139286, answer score: 2
Revisions (0)
No revisions yet.