expect key as lambda in tsstats.utils.sort_clients

* kwarg renamed to key_l (from key)
* add possibility to modify attribute
This commit is contained in:
Thor77 2016-06-12 17:36:12 +02:00
parent 9d5197d813
commit 89906d04c7
4 changed files with 16 additions and 12 deletions

View File

@ -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.

View File

@ -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',

View File

@ -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)]

View File

@ -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)