Skip to content

Instantly share code, notes, and snippets.

@MikiDi
Created March 13, 2016 18:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MikiDi/8016ecfd0e669c6d7315 to your computer and use it in GitHub Desktop.
Save MikiDi/8016ecfd0e669c6d7315 to your computer and use it in GitHub Desktop.
Script for sorting grab results produced by [ebusd](https://github.com/john30/ebusd)
#!/usr/bin/python3
import string
import sys
def read_commentfile(fn):
f = open(fn, 'r')
return [l.strip('\n') for l in f.readlines()]
def parse_commentlines(l_stream):#takes a stream of lines
grabs = []
for l in l_stream:
try:
if " / " in l:
grabs.append(parse_grabline(l))
elif ishex(l):
grabs.append(parse_grabline(l))
elif ";Vaillant;" in l:
pass
else:
pass
except Exception as e:
#print("Unparsable line:\n\"{}\"".format(l))
pass
return grabs
def parse_grabline(l):
try:
[m, s] = l.split(' / ')
m_b = bytes.fromhex(m)
s_b = bytes.fromhex(s)
except ValueError:#No slave response
m_b = bytes.fromhex(l)
s_b = None
return {
"qq": m_b[0],
"zz": m_b[1],
"pbsb": m_b[2:4],
"nn": m_b[4],
"dbn": m_b[5:6+m_b[4]],
"sl": s_b,
"raw": l
}
def sort_lines(ln, crit):
"""
crit takes a tuple/list of dict keys, for multilevel sorting
see https://docs.python.org/3/howto/sorting.html#operator-module-functions for more info
"""
return sorted(ln, key=lambda l: tuple(l[c] for c in crit))
#returns true if string s only contains hexadecimal characters
def ishex(s):
return all(c in string.hexdigits for c in s)
#List uniqifier function by Peter Bengtsson
#http://www.peterbe.com/plog/uniqifiers-benchmark
def f5(seq, idfun=None):
# order preserving
if idfun is None:
def idfun(x): return x
seen = {}
result = []
for item in seq:
marker = idfun(item)
# in old Python versions:
# if seen.has_key(marker)
# but in new ones:
if marker in seen: continue
seen[marker] = 1
result.append(item)
return result
if __name__ == "__main__":
ln = read_commentfile(sys.argv[1])
grabs_parsed = parse_commentlines(ln)
grabs_sorted = sort_lines(grabs_parsed, ("pbsb", "dbn", "qq", "zz"))
grabs_unique = f5(grabs_sorted, lambda g: g["raw"])
for l in grabs_unique:
print(l["raw"])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment