python trello_performance.py

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python trello_performance.py相关的知识,希望对你有一定的参考价值。

import re
from collections import OrderedDict

import requests
import numpy
import matplotlib.pyplot as plot


def compute_list_score(list_id, members_ids):
    cards_url = 'https://api.trello.com/1/lists/{}/cards?fields=all&token=[TOKEN]&key=[KEY]'.format(list_id)
    cards_response = requests.get(cards_url)
    cards = cards_response.json()

    members_score = {member_id: 0 for member_id in members_ids}

    if not cards:
        return members_score

    for card in cards:
        card_members = []
        card_full_name = card.get('name')

        for member_id in card.get('idMembers'):
            if not member_id in members_score:
                members_score[member_id] = 0
            card_members.append(members_ids[member_id])

        points_regex = re.search('\((?P<points>([0-9]+(\.\d+)?))\) (?P<name>[\w\ \:\-]*)', card_full_name)
        try:
            card_points = float(points_regex.group('points'))
            # card_name = points_regex.group('name')
            member_points = card_points / len(card_members)
        except:
            card_points = 0
            # card_name = card_full_name
            member_points = 0

        for member_id in card.get('idMembers'):
            members_score[member_id] += member_points

    return members_score


def fetch_members(board_id):
    members_url = 'https://api.trello.com/1/boards/{}/members?&token=[TOKEN]&key=[KEY]'.format(board_id)
    members_response = requests.get(members_url)
    return members_response.json()


def compute_score(board_id):
    lists_url = 'https://api.trello.com/1/boards/{}/lists?token=&token=[TOKEN]&key=[KEY]'.format(board_id)
    lists_reponse = requests.get(lists_url)
    lists = lists_reponse.json()

    ignored_lists = [
        
    ]

    ignored_members = [
        
    ]

    cleaned_lists = [
        _list for _list in lists if _list.get('id') not in ignored_lists
    ]

    members = fetch_members(board_id)
    members_ids = {
        member.get('id'): member.get('fullName')
        for member in members
        if member.get('id') not in ignored_members
    }
    members_score = {member_id: [] for member_id in members_ids}
    list_names = []
    for _list in cleaned_lists:
        list_id = _list.get('id')
        list_names.append(_list.get('name'))
        scores = compute_list_score(list_id, members_ids)
        for member_id in scores:
            members_score[member_id].append(scores[member_id])

    return members_score, list_names


def plot_scores(scores, labels, member_names):
    _scores = OrderedDict(scores.items())

    labeled_scores = [None] * len(labels)
    for index in range(len(labels)):
        labeled_scores[index] = [
            float(member_score[index])
            for member_id, member_score in _scores.items()
        ]

    x_axis = numpy.arange(len(labeled_scores[0]))
    bottom = numpy.zeros(len(x_axis))
    stacked_bars = []
    for label_score in labeled_scores:
        stacked_bars.append(plot.bar(x_axis, label_score, 0.8, bottom=bottom))
        bottom += numpy.array(label_score)

    plot.ylabel('Scores')
    plot.title('Developers Performance')
    x_labels = [
        '{}'.format(member_id[1:5])
        for member_id, _ in _scores.items()
    ]
    plot.xticks(x_axis, x_labels)
    plot.yticks(numpy.arange(0, 110, 10))
    plot.legend(stacked_bars, labels)

    plot.show()


if __name__ == '__main__':
    board_id = '[BOARD_ID]'
    scores, names = compute_score(board_id)

    members = fetch_members(board_id)
    member_names = {
        member.get('id'): member.get('username')
        for member in members
    }

    plot_scores(scores, names, member_names)

以上是关于python trello_performance.py的主要内容,如果未能解决你的问题,请参考以下文章

001--python全栈--基础知识--python安装

Python代写,Python作业代写,代写Python,代做Python

Python开发

Python,python,python

Python 介绍

Python学习之认识python