1
0
mirror of https://github.com/kellyjonbrazil/jc.git synced 2025-06-19 00:17:51 +02:00

Revert "Simplify process() in history.py, avoid list allocation in parse()"

This commit is contained in:
Kelly Brazil
2020-03-04 15:32:23 -08:00
committed by GitHub
parent d75c4068ca
commit c01bcd3734

View File

@ -79,11 +79,21 @@ def process(proc_data):
# rebuild output for added semantic information # rebuild output for added semantic information
processed = [] processed = []
for k, v in proc_data.items(): for k, v in proc_data.items():
proc_line = { proc_line = {}
'line': int(k) if k.isdigit() else None, proc_line['line'] = k
'command': v, proc_line['command'] = v
}
processed.append(proc_line) 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 return processed
@ -110,14 +120,17 @@ def parse(data, raw=False, quiet=False):
# split lines and clear out any non-ascii chars # split lines and clear out any non-ascii chars
linedata = data.encode('ascii', errors='ignore').decode().splitlines() linedata = data.encode('ascii', errors='ignore').decode().splitlines()
# Skip any blank lines # Clear any blank lines
for entry in filter(None, linedata): cleandata = list(filter(None, linedata))
try:
parsed_line = entry.split(maxsplit=1) if cleandata:
raw_output[parsed_line[0]] = parsed_line[1] for entry in cleandata:
except IndexError: try:
# need to catch indexerror in case there is weird input from prior commands parsed_line = entry.split(maxsplit=1)
pass 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: if raw:
return raw_output return raw_output