TeamspeakStats/tsstats/utils.py
Thor77 89906d04c7 expect key as lambda in tsstats.utils.sort_clients
* kwarg renamed to key_l (from key)
* add possibility to modify attribute
2016-06-12 17:36:12 +02:00

38 lines
1,004 B
Python

# -*- coding: utf-8 -*-
def sort_clients(clients, key_l):
'''
sort `clients` by `key`
:param clients: clients to sort
:param key_l: lambda/function returning the value of `key` for a client
:type clients: tsstats.client.Clients
:type key_l: function
:return: sorted `clients`
:rtype: list
'''
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)
def seconds_to_text(seconds):
'''
convert `seconds` to a text-representation
:param seconds: seconds to convert
:type seconds: int
:return: `seconds` as text-representation
:rtype: str
'''
minutes, seconds = divmod(seconds, 60)
hours, minutes = divmod(minutes, 60)
hours = str(hours) + 'h ' if hours > 0 else ''
minutes = str(minutes) + 'm ' if minutes > 0 else ''
seconds = str(seconds) + 's' if seconds > 0 else ''
return hours + minutes + seconds