diff options
author | David Arthur <mumrah@gmail.com> | 2013-02-20 10:34:34 -0500 |
---|---|---|
committer | David Arthur <mumrah@gmail.com> | 2013-04-02 20:19:30 -0400 |
commit | 8b70b9cf6ab28a662bff0b00ece6e7a2924a9e8f (patch) | |
tree | 57b64f1a6715548bbbc4c097174a1cdfcaafb290 /kafka/util.py | |
parent | 71fef1b1555c2fb15a89411a5a6f79baebe4d3ae (diff) | |
download | kafka-python-8b70b9cf6ab28a662bff0b00ece6e7a2924a9e8f.tar.gz |
First pass of cleanup/refactoring
Also added a bunch of docstrings
Diffstat (limited to 'kafka/util.py')
-rw-r--r-- | kafka/util.py | 33 |
1 files changed, 25 insertions, 8 deletions
diff --git a/kafka/util.py b/kafka/util.py index 6f27637..0623f35 100644 --- a/kafka/util.py +++ b/kafka/util.py @@ -1,38 +1,55 @@ +from itertools import groupby import struct def write_int_string(s): - return struct.pack('>i%ds' % len(s), len(s), s) + if s is None: + return struct.pack('>i', -1) + else: + return struct.pack('>i%ds' % len(s), len(s), s) def write_short_string(s): - return struct.pack('>H%ds' % len(s), len(s), s) + if s is None: + return struct.pack('>h', -1) + else: + return struct.pack('>h%ds' % len(s), len(s), s) def read_short_string(data, cur): if len(data) < cur+2: - raise IOError("Not enough data left") - (strLen,) = struct.unpack('>H', data[cur:cur+2]) + raise BufferUnderflowError("Not enough data left") + (strLen,) = struct.unpack('>h', data[cur:cur+2]) if strLen == -1: return (None, cur+2) cur += 2 if len(data) < cur+strLen: - raise IOError("Not enough data left") + raise BufferUnderflowError("Not enough data left") out = data[cur:cur+strLen] return (out, cur+strLen) def read_int_string(data, cur): if len(data) < cur+4: - raise IOError("Not enough data left") + raise BufferUnderflowError("Not enough data left") (strLen,) = struct.unpack('>i', data[cur:cur+4]) if strLen == -1: return (None, cur+4) cur += 4 if len(data) < cur+strLen: - raise IOError("Not enough data left") + raise BufferUnderflowError("Not enough data left") out = data[cur:cur+strLen] return (out, cur+strLen) def relative_unpack(fmt, data, cur): size = struct.calcsize(fmt) if len(data) < cur+size: - raise IOError("Not enough data left") + raise BufferUnderflowError("Not enough data left") out = struct.unpack(fmt, data[cur:cur+size]) return (out, cur+size) + +def group_list_by_key(l, key): + sorted_l = sorted(l, key=key) + return list(groupby(sorted_l, key=key)) + +class BufferUnderflowError(Exception): + pass + +class ChecksumError(Exception): + pass |