From 37c30f645d6492109a58f803ab024f52d24d8e8f Mon Sep 17 00:00:00 2001 From: Raphael Scholer Date: Wed, 19 Aug 2015 06:15:44 +0200 Subject: [PATCH] cmus - Make Cmus._query_cmus more pythonic - Use string.partition instead of excessive slicing - Use splitlines instead of split('\n'). This also reduces the times the for-loop is run. --- i3pystatus/cmus.py | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/i3pystatus/cmus.py b/i3pystatus/cmus.py index 57f10b4..dbd975a 100644 --- a/i3pystatus/cmus.py +++ b/i3pystatus/cmus.py @@ -52,19 +52,20 @@ class Cmus(IntervalModule): return run_through_shell(cmdline, enable_shell=True) def _query_cmus(self): - status_dict = {} + response = {} cmd = self._cmus_command('query') + if not cmd.rc: - status = cmd.out.split('\n') - for item in status: - split_item = item.split(' ') - if split_item[0] in ['tag', 'set']: - key = '_'.join(split_item[:2]) - val = ' '.join([x for x in split_item[2:]]) - status_dict[key] = val + for line in cmd.out.splitlines(): + category, _, category_value = line.partition(' ') + if category in ('set', 'tag'): + key, _, value = category_value.partition(' ') + key = '_'.join((category, key)) + response[key] = value else: - status_dict[split_item[0]] = ' '.join(split_item[1:]) - return status_dict + response[category] = category_value + + return response def run(self): status = self._query_cmus()