From 89906d04c7eec72ea5f290ec82e4fd24017661e3 Mon Sep 17 00:00:00 2001 From: Thor77 Date: Sun, 12 Jun 2016 17:36:12 +0200 Subject: [PATCH] expect key as lambda in tsstats.utils.sort_clients * kwarg renamed to key_l (from key) * add possibility to modify attribute --- docs/source/conf.py | 4 ++-- setup.py | 2 +- tsstats/template.py | 12 +++++++----- tsstats/utils.py | 10 ++++++---- 4 files changed, 16 insertions(+), 12 deletions(-) diff --git a/docs/source/conf.py b/docs/source/conf.py index 6871da8..454098a 100644 --- a/docs/source/conf.py +++ b/docs/source/conf.py @@ -54,9 +54,9 @@ author = 'Thor77' # built documents. # # The short X.Y version. -version = '0.6' +version = '0.7' # The full version, including alpha/beta/rc tags. -release = '0.6.8' +release = '0.7.0' # The language for content autogenerated by Sphinx. Refer to documentation # for a list of supported languages. diff --git a/setup.py b/setup.py index 5eb3060..0cbea8e 100644 --- a/setup.py +++ b/setup.py @@ -2,7 +2,7 @@ from setuptools import setup setup( name='tsstats', - version='0.6.8', + version='0.7.0', author='Thor77', author_email='thor77@thor77.org', description='A simple Teamspeak stats-generator', diff --git a/tsstats/template.py b/tsstats/template.py index 34b0bdc..598779d 100644 --- a/tsstats/template.py +++ b/tsstats/template.py @@ -26,16 +26,18 @@ def render_template(clients, output, title='TeamspeakStats'): :type title: str ''' # prepare clients - clients_onlinetime_ = sort_clients(clients, 'onlinetime') + clients_onlinetime_ = sort_clients( + clients, lambda c: c.onlinetime + ) clients_onlinetime = [ (client, seconds_to_text(onlinetime)) for client, onlinetime in clients_onlinetime_ ] - clients_kicks = sort_clients(clients, 'kicks') - clients_pkicks = sort_clients(clients, 'pkicks') - clients_bans = sort_clients(clients, 'bans') - clients_pbans = sort_clients(clients, 'pbans') + clients_kicks = sort_clients(clients, lambda c: c.kicks) + clients_pkicks = sort_clients(clients, lambda c: c.pkicks) + clients_bans = sort_clients(clients, lambda c: c.bans) + clients_pbans = sort_clients(clients, lambda c: c.pbans) objs = [('Onlinetime', clients_onlinetime), ('Kicks', clients_kicks), ('passive Kicks', clients_pkicks), ('Bans', clients_bans), ('passive Bans', clients_pbans)] diff --git a/tsstats/utils.py b/tsstats/utils.py index 98540d6..c2bde20 100644 --- a/tsstats/utils.py +++ b/tsstats/utils.py @@ -1,20 +1,22 @@ # -*- coding: utf-8 -*- -def sort_clients(clients, key): +def sort_clients(clients, key_l): ''' sort `clients` by `key` :param clients: clients to sort - :param key: key to sort clients with + :param key_l: lambda/function returning the value of `key` for a client :type clients: tsstats.client.Clients - :type key: str + :type key_l: function :return: sorted `clients` :rtype: list ''' - cl_data = [(client, client[key]) for client in clients if client[key] > 0] + cl_data = [ + (client, key_l(client)) for client in clients if key_l(client) > 0 + ] return sorted(cl_data, key=lambda data: data[1], reverse=True)