Exemple #1
0
def decode(tree_root, data_to_decode):
    """decote data according to its code tree"""
    result = ""
    import kbp._bits as _bits
    decomp = _bits.decompress(data_to_decode, 1)
    while not decomp.is_end():
        node = tree_root
        while "symbol" not in node:
            bit = decomp.read_bit()
            if bit == 0:
                node = node["left0"]
            elif bit == 1:
                node = node["right1"]
        result += node["symbol"]
    return result
Exemple #2
0
def decode(tree_root, data_to_decode):
    """decote data according to its code tree"""
    result = ""
    import kbp._bits as _bits

    decomp = _bits.decompress(data_to_decode, 1)
    while not decomp.is_end():
        node = tree_root
        while "symbol" not in node:
            bit = decomp.read_bit()
            if bit == 0:
                node = node["left0"]
            elif bit == 1:
                node = node["right1"]
        result += node["symbol"]
    return result
Exemple #3
0
assert "".join(str(int(i)) for i in bits("AE")) == "01000001" + "01000101"
assert "".join(
    str(int(i))
    for i in bits("AE", bit7_first=False)) == "10000010" + "10100010"

assert reverse(10341313, 32) == 2211690752
assert reverse(reverse(10341313, 32), 32) == 10341313

c = compress(1)
c.write_bit(1)
assert c.getdata() == "\x80"

c = compress(2)
c.write_bit(1)
assert c.getdata() == "\x00\x80"

c = compress(2)
c.write_variablenumber(109)
assert c.getdata() == "`\xdf"

c = compress(2)
c.write_fixednumber(109, 9)
assert c.getdata() == '\x806'

#c = compress(2);c.write_bit(1);c.write_fixednumber(15, 5);c.write_variablenumber(2049);
#test = c.getdata()
test = 'U\xbd`U'
d = decompress(test, 2)
assert d.read_bit() == 1
assert d.read_fixednumber(5) == 15
assert d.read_variablenumber() == 2049
Exemple #4
0
from kbp._bits import bits, reverse, compress, decompress

assert "".join(str(int(i)) for i in bits("AE")) == "01000001" + "01000101"
assert "".join(str(int(i)) for i in bits("AE", bit7_first=False)) == "10000010" + "10100010"

assert reverse(10341313, 32) == 2211690752
assert reverse(reverse(10341313, 32), 32) == 10341313

c = compress(1)
c.write_bit(1)
assert c.getdata() == "\x80"

c = compress(2)
c.write_bit(1)
assert c.getdata() == "\x00\x80"

c = compress(2)
c.write_variablenumber(109)
assert c.getdata() == "`\xdf"

c = compress(2)
c.write_fixednumber(109, 9)
assert c.getdata() == '\x806'
 
#c = compress(2);c.write_bit(1);c.write_fixednumber(15, 5);c.write_variablenumber(2049);
#test = c.getdata()
test = 'U\xbd`U'
d = decompress(test, 2)
assert d.read_bit() == 1
assert d.read_fixednumber(5) == 15
assert d.read_variablenumber() == 2049