From 065276805f0f2dd23a6382f0120ca07e9eae116f Mon Sep 17 00:00:00 2001 From: philippeitis <33013301+philippeitis@users.noreply.github.com> Date: Wed, 4 Mar 2020 13:35:31 -0800 Subject: [PATCH] Simplify process() in history.py, avoid list allocation in parse() --- jc/parsers/history.py | 37 ++++++++++++------------------------- 1 file changed, 12 insertions(+), 25 deletions(-) diff --git a/jc/parsers/history.py b/jc/parsers/history.py index 195dc603..41c5fab4 100644 --- a/jc/parsers/history.py +++ b/jc/parsers/history.py @@ -79,21 +79,11 @@ def process(proc_data): # rebuild output for added semantic information processed = [] for k, v in proc_data.items(): - proc_line = {} - proc_line['line'] = k - proc_line['command'] = v + proc_line = { + 'line': int(k) if k.isdigit() else None, + 'command': v, + } processed.append(proc_line) - - for entry in processed: - int_list = ['line'] - for key in int_list: - if key in entry: - try: - key_int = int(entry[key]) - entry[key] = key_int - except (ValueError): - entry[key] = None - return processed @@ -120,17 +110,14 @@ def parse(data, raw=False, quiet=False): # split lines and clear out any non-ascii chars linedata = data.encode('ascii', errors='ignore').decode().splitlines() - # Clear any blank lines - cleandata = list(filter(None, linedata)) - - if cleandata: - for entry in cleandata: - try: - parsed_line = entry.split(maxsplit=1) - raw_output[parsed_line[0]] = parsed_line[1] - except IndexError: - # need to catch indexerror in case there is weird input from prior commands - pass + # Skip any blank lines + for entry in filter(None, linedata): + try: + parsed_line = entry.split(maxsplit=1) + raw_output[parsed_line[0]] = parsed_line[1] + except IndexError: + # need to catch indexerror in case there is weird input from prior commands + pass if raw: return raw_output