Ejemplo n.º 1
0
def write_gints(dbfile, nums):
    """Write the integers in the iterator nums to bytes stream dbfile.
    """

    buf = array("c")
    count = 0
    key = 0
    for v in nums:
        shift = count * 2
        if v < 256:
            buf.append(chr(v))
        elif v < 65536:
            key |= 1 << shift
            buf.extend(pack_ushort_le(v))
        elif v < 16777216:
            key |= 2 << shift
            buf.extend(pack_uint_le(v)[:3])
        else:
            key |= 3 << shift
            buf.extend(pack_uint_le(v))

        count += 1
        if count == 4:
            #print bin(key), repr(buf)
            dbfile.write(chr(key))
            dbfile.write(buf.tostring())
            count = 0
            key = 0
            del buf[0:len(buf)]

    if count:
        dbfile.write(chr(key))
        dbfile.write(buf.tostring())
    def write_nums(self, f, numbers):
        buf = emptybytes
        count = 0
        key = 0
        for v in numbers:
            shift = count * 2
            if v < 256:
                buf += pack_byte(v)
            elif v < 65536:
                key |= 1 << shift
                buf += pack_ushort_le(v)
            elif v < 16777216:
                key |= 2 << shift
                buf += pack_uint_le(v)[:3]
            else:
                key |= 3 << shift
                buf += pack_uint_le(v)

            count += 1
            if count == 4:
                f.write_byte(key)
                f.write(buf)
                count = 0
                key = 0
                buf = emptybytes  # Clear the buffer

        # Write out leftovers in the buffer
        if count:
            f.write_byte(key)
            f.write(buf)
Ejemplo n.º 3
0
    def write_nums(self, f, numbers):
        buf = emptybytes
        count = 0
        key = 0
        for v in numbers:
            shift = count * 2
            if v < 256:
                buf += pack_byte(v)
            elif v < 65536:
                key |= 1 << shift
                buf += pack_ushort_le(v)
            elif v < 16777216:
                key |= 2 << shift
                buf += pack_uint_le(v)[:3]
            else:
                key |= 3 << shift
                buf += pack_uint_le(v)

            count += 1
            if count == 4:
                f.write_byte(key)
                f.write(buf)
                count = 0
                key = 0
                buf = emptybytes  # Clear the buffer

        # Write out leftovers in the buffer
        if count:
            f.write_byte(key)
            f.write(buf)
Ejemplo n.º 4
0
def write_gints(dbfile, nums):
    """Write the integers in the iterator nums to bytes stream dbfile.
    """

    buf = array("c")
    count = 0
    key = 0
    for v in nums:
        shift = count * 2
        if v < 256:
            buf.append(chr(v))
        elif v < 65536:
            key |= 1 << shift
            buf.extend(pack_ushort_le(v))
        elif v < 16777216:
            key |= 2 << shift
            buf.extend(pack_uint_le(v)[:3])
        else:
            key |= 3 << shift
            buf.extend(pack_uint_le(v))

        count += 1
        if count == 4:
            # print bin(key), repr(buf)
            dbfile.write(chr(key))
            dbfile.write(buf.tostring())
            count = 0
            key = 0
            del buf[0 : len(buf)]

    if count:
        dbfile.write(chr(key))
        dbfile.write(buf.tostring())
Ejemplo n.º 5
0
    def write_nums(cls, f, numbers):
        buf = array("B")
        count = 0
        key = 0
        for v in numbers:
            shift = count * 2
            if v < 256:
                buf.append(v)
            elif v < 65536:
                key |= 1 << shift
                buf.extend(pack_ushort_le(v))
            elif v < 16777216:
                key |= 2 << shift
                buf.extend(pack_uint_le(v)[:3])
            else:
                key |= 3 << shift
                buf.extend(pack_uint_le(v))

            count += 1
            if count == 4:
                f.write_byte(key)
                f.write(buf)
                count = 0
                key = 0
                del buf[:]  # Clear the buffer

        # Write out leftovers in the buffer
        if count:
            f.write_byte(key)
            f.write(buf)
Ejemplo n.º 6
0
 def write_ushort_le(self, n):
     self.write(pack_ushort_le(n))
Ejemplo n.º 7
0
 def write_ushort_le(self, n):
     self.write(pack_ushort_le(n))