| 1 |
from ..helpers import nativestr |
| 2 |
|
| 3 |
|
| 4 |
def list_to_dict(aList): |
| 5 |
return {nativestr(aList[i][0]): nativestr(aList[i][1]) for i in range(len(aList))} |
| 6 |
|
| 7 |
|
| 8 |
def parse_range(response, **kwargs): |
| 9 |
"""Parse range response. Used by TS.RANGE and TS.REVRANGE.""" |
| 10 |
return [tuple((r[0], float(r[1]))) for r in response] |
| 11 |
|
| 12 |
|
| 13 |
def parse_m_range(response): |
| 14 |
"""Parse multi range response. Used by TS.MRANGE and TS.MREVRANGE.""" |
| 15 |
res = [] |
| 16 |
for item in response: |
| 17 |
res.append({nativestr(item[0]): [list_to_dict(item[1]), parse_range(item[2])]}) |
| 18 |
return sorted(res, key=lambda d: list(d.keys())) |
| 19 |
|
| 20 |
|
| 21 |
def parse_get(response): |
| 22 |
"""Parse get response. Used by TS.GET.""" |
| 23 |
if not response: |
| 24 |
return None |
| 25 |
return int(response[0]), float(response[1]) |
| 26 |
|
| 27 |
|
| 28 |
def parse_m_get(response): |
| 29 |
"""Parse multi get response. Used by TS.MGET.""" |
| 30 |
res = [] |
| 31 |
for item in response: |
| 32 |
if not item[2]: |
| 33 |
res.append({nativestr(item[0]): [list_to_dict(item[1]), None, None]}) |
| 34 |
else: |
| 35 |
res.append( |
| 36 |
{ |
| 37 |
nativestr(item[0]): [ |
| 38 |
list_to_dict(item[1]), |
| 39 |
int(item[2][0]), |
| 40 |
float(item[2][1]), |
| 41 |
] |
| 42 |
} |
| 43 |
) |
| 44 |
return sorted(res, key=lambda d: list(d.keys())) |