Browse Source

Tweaks to sorting

tags/nilmdb-1.5.4
Jim Paris 11 years ago
parent
commit
01029230c9
3 changed files with 23 additions and 9 deletions
  1. +1
    -1
      nilmdb/client/client.py
  2. +15
    -4
      nilmdb/utils/sort.py
  3. +7
    -4
      tests/test_client.py

+ 1
- 1
nilmdb/client/client.py View File

@@ -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 }


+ 15
- 4
nilmdb/utils/sort.py View File

@@ -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)

+ 7
- 4
tests/test_client.py View File

@@ -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…
Cancel
Save