diff options
author | Keith Bostic <keith@wiredtiger.com> | 2014-12-03 20:43:47 -0500 |
---|---|---|
committer | Keith Bostic <keith@wiredtiger.com> | 2014-12-03 20:43:47 -0500 |
commit | 411a97bca2abfdf7ef4a1ba3405bf9a07181885e (patch) | |
tree | f1e74aa99c461f329a965d0026a25a95becb90de /lang | |
parent | 4ad452d526163080bfc85b0158b4fb7cf9e9a3ec (diff) | |
download | mongo-411a97bca2abfdf7ef4a1ba3405bf9a07181885e.tar.gz |
Change the Python scripts to follow Python indenting standards (4 space
indents, spaces only).
Diffstat (limited to 'lang')
-rw-r--r-- | lang/python/wiredtiger/intpack-test.py | 6 | ||||
-rw-r--r-- | lang/python/wiredtiger/intpacking.py | 119 | ||||
-rw-r--r-- | lang/python/wiredtiger/packing-test.py | 2 | ||||
-rw-r--r-- | lang/python/wiredtiger/packing.py | 234 |
4 files changed, 181 insertions, 180 deletions
diff --git a/lang/python/wiredtiger/intpack-test.py b/lang/python/wiredtiger/intpack-test.py index b731a5e5adc..6c1c991ddfd 100644 --- a/lang/python/wiredtiger/intpack-test.py +++ b/lang/python/wiredtiger/intpack-test.py @@ -30,6 +30,6 @@ from intpacking import compress_int i = 1 while i < 1 << 60: - print -i, ''.join('%02x' % ord(c) for c in compress_int(-i)) - print i, ''.join('%02x' % ord(c) for c in compress_int(i)) - i <<= 1 + print -i, ''.join('%02x' % ord(c) for c in compress_int(-i)) + print i, ''.join('%02x' % ord(c) for c in compress_int(i)) + i <<= 1 diff --git a/lang/python/wiredtiger/intpacking.py b/lang/python/wiredtiger/intpacking.py index d9e48bb91f6..fefdb0dca1f 100644 --- a/lang/python/wiredtiger/intpacking.py +++ b/lang/python/wiredtiger/intpacking.py @@ -62,75 +62,76 @@ MINUS_BIT = -1 << 64 UINT64_MASK = 0xffffffffffffffff def getbits(x, start, end=0): - '''return the least significant bits of x, from start to end''' - return (x & ((1 << start) - 1)) >> (end) + '''return the least significant bits of x, from start to end''' + return (x & ((1 << start) - 1)) >> (end) def get_int(b, size): - r = 0; - for i in xrange(size): - r = (r << 8) | ord(b[i]) - return r + r = 0 + for i in xrange(size): + r = (r << 8) | ord(b[i]) + return r def pack_int(x): - if x < NEG_2BYTE_MIN: - packed = struct.pack('>Q', x & UINT64_MASK) - while packed and packed[0] == '\xff': - packed = packed[1:] - return chr(NEG_MULTI_MARKER | getbits(8 - len(packed), 4)) + packed - elif x < NEG_1BYTE_MIN: - x -= NEG_2BYTE_MIN - return chr(NEG_2BYTE_MARKER | getbits(x, 13, 8)) + chr(getbits(x, 8)) - elif x < 0: - x -= NEG_1BYTE_MIN - return chr(NEG_1BYTE_MARKER | getbits(x, 6)) - elif x <= POS_1BYTE_MAX: - return chr(POS_1BYTE_MARKER | getbits(x, 6)) - elif x <= POS_2BYTE_MAX: - x -= (POS_1BYTE_MAX + 1) - return chr(POS_2BYTE_MARKER | getbits(x, 13, 8)) + chr(getbits(x, 8)) - else: - packed = struct.pack('>Q', x - (POS_2BYTE_MAX + 1)) - while packed and packed[0] == '\x00': - packed = packed[1:] - return chr(POS_MULTI_MARKER | getbits(len(packed), 4)) + packed + if x < NEG_2BYTE_MIN: + packed = struct.pack('>Q', x & UINT64_MASK) + while packed and packed[0] == '\xff': + packed = packed[1:] + return chr(NEG_MULTI_MARKER | getbits(8 - len(packed), 4)) + packed + elif x < NEG_1BYTE_MIN: + x -= NEG_2BYTE_MIN + return chr(NEG_2BYTE_MARKER | getbits(x, 13, 8)) + chr(getbits(x, 8)) + elif x < 0: + x -= NEG_1BYTE_MIN + return chr(NEG_1BYTE_MARKER | getbits(x, 6)) + elif x <= POS_1BYTE_MAX: + return chr(POS_1BYTE_MARKER | getbits(x, 6)) + elif x <= POS_2BYTE_MAX: + x -= (POS_1BYTE_MAX + 1) + return chr(POS_2BYTE_MARKER | getbits(x, 13, 8)) + chr(getbits(x, 8)) + else: + packed = struct.pack('>Q', x - (POS_2BYTE_MAX + 1)) + while packed and packed[0] == '\x00': + packed = packed[1:] + return chr(POS_MULTI_MARKER | getbits(len(packed), 4)) + packed def unpack_int(b): - marker = ord(b[0]) - if marker < NEG_2BYTE_MARKER: - sz = 8 - getbits(marker, 4) - return ((-1 << (sz << 3)) | get_int(b[1:], sz), b[sz+1:]) - elif marker < NEG_1BYTE_MARKER: - return (NEG_2BYTE_MIN + ((getbits(marker, 5) << 8) | ord(b[1])), b[2:]) - elif marker < POS_1BYTE_MARKER: - return (NEG_1BYTE_MIN + getbits(marker, 6), b[1:]) - elif marker < POS_2BYTE_MARKER: - return (getbits(marker, 6), b[1:]) - elif marker < POS_MULTI_MARKER: - return (POS_1BYTE_MAX + 1 + ((getbits(marker, 5) << 8) | ord(b[1])), b[2:]) - else: - sz = getbits(marker, 4) - return (POS_2BYTE_MAX + 1 + get_int(b[1:], sz), b[sz+1:]) + marker = ord(b[0]) + if marker < NEG_2BYTE_MARKER: + sz = 8 - getbits(marker, 4) + return ((-1 << (sz << 3)) | get_int(b[1:], sz), b[sz+1:]) + elif marker < NEG_1BYTE_MARKER: + return (NEG_2BYTE_MIN + ((getbits(marker, 5) << 8) | ord(b[1])), b[2:]) + elif marker < POS_1BYTE_MARKER: + return (NEG_1BYTE_MIN + getbits(marker, 6), b[1:]) + elif marker < POS_2BYTE_MARKER: + return (getbits(marker, 6), b[1:]) + elif marker < POS_MULTI_MARKER: + return (POS_1BYTE_MAX + 1 + + ((getbits(marker, 5) << 8) | ord(b[1])), b[2:]) + else: + sz = getbits(marker, 4) + return (POS_2BYTE_MAX + 1 + get_int(b[1:], sz), b[sz+1:]) # Sanity testing if __name__ == '__main__': - import random + import random - for big in (100, 10000, 1 << 40, 1 << 64): - for i in xrange(1000): - r = random.randint(-big, big) - print "\rChecking %d" % r, - if unpack_int(pack_int(r))[0] != r: - print "\nFound a problem with %d" % r - break + for big in (100, 10000, 1 << 40, 1 << 64): + for i in xrange(1000): + r = random.randint(-big, big) + print "\rChecking %d" % r, + if unpack_int(pack_int(r))[0] != r: + print "\nFound a problem with %d" % r + break - print + print - for i in xrange(1000): - r1 = random.randint(-big, big) - r2 = random.randint(-big, big) - print "\rChecking %d, %d" % (r1, r2), - if cmp(r1, r2) != cmp(pack_int(r1), pack_int(r2)): - print "\nFound a problem with %d, %d" % (r1, r2) - break + for i in xrange(1000): + r1 = random.randint(-big, big) + r2 = random.randint(-big, big) + print "\rChecking %d, %d" % (r1, r2), + if cmp(r1, r2) != cmp(pack_int(r1), pack_int(r2)): + print "\nFound a problem with %d, %d" % (r1, r2) + break - print + print diff --git a/lang/python/wiredtiger/packing-test.py b/lang/python/wiredtiger/packing-test.py index 3a4e34f3fc1..eaae4d51ae7 100644 --- a/lang/python/wiredtiger/packing-test.py +++ b/lang/python/wiredtiger/packing-test.py @@ -29,7 +29,7 @@ from packing import pack, unpack def check(fmt, *v): - print fmt, repr(v), ''.join('%02x' % ord(c) for c in pack(fmt, *v)) + print fmt, repr(v), ''.join('%02x' % ord(c) for c in pack(fmt, *v)) check('iii', 0, 101, -99) check('3i', 0, 101, -99) diff --git a/lang/python/wiredtiger/packing.py b/lang/python/wiredtiger/packing.py index 3ad4623e2f1..6bcb82e76d5 100644 --- a/lang/python/wiredtiger/packing.py +++ b/lang/python/wiredtiger/packing.py @@ -30,124 +30,124 @@ from intpacking import pack_int, unpack_int def __get_type(fmt): - if not fmt: - return None, fmt - # Variable-sized encoding is the default (and only supported format in v1) - if fmt[0] in '.@<>': - tfmt = fmt[0] - fmt = fmt[1:] - else: - tfmt = '.' - return tfmt, fmt + if not fmt: + return None, fmt + # Variable-sized encoding is the default (and only supported format in v1) + if fmt[0] in '.@<>': + tfmt = fmt[0] + fmt = fmt[1:] + else: + tfmt = '.' + return tfmt, fmt def unpack(fmt, s): - tfmt, fmt = __get_type(fmt) - if not fmt: - return () - if tfmt != '.': - raise ValueError('Only variable-length encoding is currently supported') - result = [] - havesize = size = 0 - for offset, f in enumerate(fmt): - if f.isdigit(): - size = (size * 10) + int(f) - havesize = 1 - continue - elif f == 'x': - if not havesize: - size = 1 - s = s[size:] - # Note: no value, don't increment i - elif f in 'Ssu': - if not havesize: - if f == 's': - size = 1 - elif f == 'S': - size = s.find('\0') - elif f == 'u': - if offset == len(fmt) - 1: - size = len(s) - else: - size, s = unpack_int(s) - result.append(s[:size]) - if f == 'S' and not havesize: - size += 1 - s = s[size:] - elif f in 't': - # bit type, size is number of bits - if not havesize: - size = 1 - result.append(ord(s[0:1])) - s = s[1:] - else: - # integral type - if not havesize: - size = 1 - for j in xrange(size): - v, s = unpack_int(s) - result.append(v) - havesize = size = 0 - return result + tfmt, fmt = __get_type(fmt) + if not fmt: + return () + if tfmt != '.': + raise ValueError('Only variable-length encoding is currently supported') + result = [] + havesize = size = 0 + for offset, f in enumerate(fmt): + if f.isdigit(): + size = (size * 10) + int(f) + havesize = 1 + continue + elif f == 'x': + if not havesize: + size = 1 + s = s[size:] + # Note: no value, don't increment i + elif f in 'Ssu': + if not havesize: + if f == 's': + size = 1 + elif f == 'S': + size = s.find('\0') + elif f == 'u': + if offset == len(fmt) - 1: + size = len(s) + else: + size, s = unpack_int(s) + result.append(s[:size]) + if f == 'S' and not havesize: + size += 1 + s = s[size:] + elif f in 't': + # bit type, size is number of bits + if not havesize: + size = 1 + result.append(ord(s[0:1])) + s = s[1:] + else: + # integral type + if not havesize: + size = 1 + for j in xrange(size): + v, s = unpack_int(s) + result.append(v) + havesize = size = 0 + return result def pack(fmt, *values): - tfmt, fmt = __get_type(fmt) - if not fmt: - return () - if tfmt != '.': - raise ValueError('Only variable-length encoding is currently supported') - result = '' - havesize = i = size = 0 - for offset, f in enumerate(fmt): - if f.isdigit(): - size = (size * 10) + int(f) - havesize = 1 - continue - elif f == 'x': - if not havesize: - result += '\0' - else: - result += '\0' * size - # Note: no value, don't increment i - elif f in 'Ssu': - val = values[i] - if f == 'S' and '\0' in val: - l = val.find('\0') - else: - l = len(val) - if havesize: - if l > size: - l = size - elif f == 's': - havesize = size = 1 - elif f == 'u' and offset != len(fmt) - 1: - result += pack_int(l) - if type(val) is unicode and f in 'Ss': - result += str(val[:l]) - else: - result += val[:l] - if f == 'S' and not havesize: - result += '\0' - elif size > l: - result += '\0' * (size - l) - i += 1 - elif f in 't': - # bit type, size is number of bits - if not havesize: - size = 1 - if size > 8: - raise ValueError("bit count cannot be greater than 8 for 't' encoding") - mask = (1 << size) - 1 - val = values[i] - if (mask & val) != val: - raise ValueError("value out of range for 't' encoding") - result += chr(val) - i += 1 - else: - # integral type - if not havesize: - size = 1 - for j in xrange(size): - result += pack_int(values[i]) - i += 1 - havesize = size = 0 - return result + tfmt, fmt = __get_type(fmt) + if not fmt: + return () + if tfmt != '.': + raise ValueError('Only variable-length encoding is currently supported') + result = '' + havesize = i = size = 0 + for offset, f in enumerate(fmt): + if f.isdigit(): + size = (size * 10) + int(f) + havesize = 1 + continue + elif f == 'x': + if not havesize: + result += '\0' + else: + result += '\0' * size + # Note: no value, don't increment i + elif f in 'Ssu': + val = values[i] + if f == 'S' and '\0' in val: + l = val.find('\0') + else: + l = len(val) + if havesize: + if l > size: + l = size + elif f == 's': + havesize = size = 1 + elif f == 'u' and offset != len(fmt) - 1: + result += pack_int(l) + if type(val) is unicode and f in 'Ss': + result += str(val[:l]) + else: + result += val[:l] + if f == 'S' and not havesize: + result += '\0' + elif size > l: + result += '\0' * (size - l) + i += 1 + elif f in 't': + # bit type, size is number of bits + if not havesize: + size = 1 + if size > 8: + raise ValueError("bit count cannot be greater than 8 for 't' encoding") + mask = (1 << size) - 1 + val = values[i] + if (mask & val) != val: + raise ValueError("value out of range for 't' encoding") + result += chr(val) + i += 1 + else: + # integral type + if not havesize: + size = 1 + for j in xrange(size): + result += pack_int(values[i]) + i += 1 + havesize = size = 0 + return result |