Tweaks to sorting
This commit is contained in:
parent
ecc4e5ef9d
commit
01029230c9
|
@ -66,7 +66,7 @@ class Client(object):
|
|||
if extended:
|
||||
params["extended"] = 1
|
||||
streams = self.http.get("stream/list", params)
|
||||
return nilmdb.utils.sort.sort_streams_nicely(streams)
|
||||
return nilmdb.utils.sort.sort_human(streams, key = lambda s: s[0])
|
||||
|
||||
def stream_get_metadata(self, path, keys = None):
|
||||
params = { "path": path }
|
||||
|
|
|
@ -1,7 +1,18 @@
|
|||
import re
|
||||
|
||||
def sort_streams_nicely(x):
|
||||
def sort_human(items, key = None):
|
||||
"""Human-friendly sort (/stream/2 before /stream/10)"""
|
||||
num = lambda t: int(t) if t.isdigit() else t
|
||||
key = lambda k: [ num(c) for c in re.split('([0-9]+)', k[0]) ]
|
||||
return sorted(x, key = key)
|
||||
def to_num(val):
|
||||
try:
|
||||
return int(val)
|
||||
except Exception:
|
||||
return val
|
||||
|
||||
def human_key(text):
|
||||
if key:
|
||||
text = key(text)
|
||||
# Break into character and numeric chunks.
|
||||
chunks = re.split(r'([0-9]+)', text)
|
||||
return [ to_num(c) for c in chunks ]
|
||||
|
||||
return sorted(items, key = human_key)
|
||||
|
|
|
@ -105,16 +105,19 @@ class TestClient(object):
|
|||
client.http.post("/stream/list")
|
||||
client = nilmdb.client.Client(url = testurl)
|
||||
|
||||
# Create three streams
|
||||
# Create four streams
|
||||
client.stream_create("/newton/prep", "float32_8")
|
||||
client.stream_create("/newton/raw", "uint16_6")
|
||||
client.stream_create("/newton/zzz/rawnotch", "uint16_9")
|
||||
client.stream_create("/newton/zzz/rawnotch2", "uint16_9")
|
||||
client.stream_create("/newton/zzz/rawnotch11", "uint16_9")
|
||||
|
||||
# Verify we got 3 streams
|
||||
# Verify we got 4 streams in the right order
|
||||
eq_(client.stream_list(), [ ["/newton/prep", "float32_8"],
|
||||
["/newton/raw", "uint16_6"],
|
||||
["/newton/zzz/rawnotch", "uint16_9"]
|
||||
["/newton/zzz/rawnotch2", "uint16_9"],
|
||||
["/newton/zzz/rawnotch11", "uint16_9"]
|
||||
])
|
||||
|
||||
# Match just one type or one path
|
||||
eq_(client.stream_list(layout="uint16_6"),
|
||||
[ ["/newton/raw", "uint16_6"] ])
|
||||
|
|
Loading…
Reference in New Issue
Block a user