@@ -0,0 +1,1502 @@
+import enum%0Aimport struct%0A%0A%0Aclass TypeID(enum.Enum):%0A    BOOL = 1%0A    INT = 2%0A    UINT = 3%0A    FLOAT = 4%0A    BYTE_SLICE = 5%0A    STRING = 6%0A    COMPLEX = 7%0A    INTERFACE = 8%0A    # gap for reserved ids.%0A    WIRE_TYPE = 16%0A    ARRAY_TYPE = 17%0A    COMMON_TYPE = 18%0A    SLICE_TYPE = 19%0A    STRUCT_TYPE = 20%0A    FIELD_TYPE = 21%0A    # 22 is slice of fieldType.%0A    MAP_TYPE = 23%0A%0A%0Adef decode_uint(buf):%0A    (length, ) = struct.unpack('b', buf%5B:1%5D)%0A    if length %3E= 0:  # small uint in a single byte%0A        return length, buf%5B1:%5D%0A%0A    # larger uint split over multiple bytes%0A    length = -length%0A    n = 0%0A    for b in buf%5B1:length%5D:%0A        n = (n + b) %3C%3C 8%0A    n += buf%5Blength%5D%0A    return n, buf%5Blength + 1:%5D%0A%0A%0Adef decode_int(buf):%0A    uint, buf = decode_uint(buf)%0A    if uint & 1:%0A        return ~(uint %3E%3E 1), buf%0A    else:%0A        return (uint %3E%3E 1), buf%0A%0A%0Adef decode_value(typeid, buf):%0A    if typeid == TypeID.INT:%0A        return decode_int(buf)%0A    if typeid == TypeID.UINT:%0A        return decode_uint(buf)%0A    raise NotImplementedError(%22cannot decode %25s%22 %25 typeid)%0A%0A%0Adef load(buf):%0A    %22%22%22Load and decode a bytes object.%22%22%22%0A    length, buf = decode_uint(buf)%0A    assert len(buf) == length%0A%0A    typeid, buf = decode_int(buf)%0A    if typeid %3C 0:%0A        raise NotImplementedError(%22cannot decode non-standard type ID %25d%22 %25%0A                                  -typeid)%0A    typeid = TypeID(typeid)%0A    # TODO: why must we skip a zero byte here?%0A    value, buf = decode_value(typeid, buf%5B1:%5D)%0A    return value%0A

add basic load function with support for int and uint