2016 Update
This commit is contained in:
parent
948b86eafa
commit
dcf5d15da3
7965 changed files with 139854 additions and 31002 deletions
|
|
@ -1,4 +1,4 @@
|
|||
class BitWriter:
|
||||
class BitWriter(object):
|
||||
def __init__(self, f):
|
||||
self.accumulator = 0
|
||||
self.bcount = 0
|
||||
|
|
@ -7,48 +7,71 @@ class BitWriter:
|
|||
def __del__(self):
|
||||
try:
|
||||
self.flush()
|
||||
except ValueError: # I/O operation on closed file
|
||||
except ValueError: # I/O operation on closed file
|
||||
pass
|
||||
|
||||
def writebit(self, bit):
|
||||
if self.bcount == 8 :
|
||||
def _writebit(self, bit):
|
||||
if self.bcount == 8:
|
||||
self.flush()
|
||||
if bit > 0:
|
||||
self.accumulator |= (1 << (7-self.bcount))
|
||||
self.accumulator |= 1 << 7-self.bcount
|
||||
self.bcount += 1
|
||||
|
||||
def writebits(self, bits, n):
|
||||
while n > 0:
|
||||
self.writebit( bits & (1 << (n-1)) )
|
||||
self._writebit(bits & 1 << n-1)
|
||||
n -= 1
|
||||
|
||||
def flush(self):
|
||||
self.out.write(chr(self.accumulator))
|
||||
self.out.write(bytearray([self.accumulator]))
|
||||
self.accumulator = 0
|
||||
self.bcount = 0
|
||||
|
||||
|
||||
class BitReader:
|
||||
class BitReader(object):
|
||||
def __init__(self, f):
|
||||
self.input = f
|
||||
self.accumulator = 0
|
||||
self.bcount = 0
|
||||
self.read = 0
|
||||
|
||||
def readbit(self):
|
||||
if self.bcount == 0 :
|
||||
def _readbit(self):
|
||||
if not self.bcount:
|
||||
a = self.input.read(1)
|
||||
if ( len(a) > 0 ):
|
||||
if a:
|
||||
self.accumulator = ord(a)
|
||||
self.bcount = 8
|
||||
self.read = len(a)
|
||||
rv = ( self.accumulator & ( 1 << (self.bcount-1) ) ) >> (self.bcount-1)
|
||||
rv = (self.accumulator & (1 << self.bcount-1)) >> self.bcount-1
|
||||
self.bcount -= 1
|
||||
return rv
|
||||
|
||||
def readbits(self, n):
|
||||
v = 0
|
||||
while n > 0:
|
||||
v = (v << 1) | self.readbit()
|
||||
v = (v << 1) | self._readbit()
|
||||
n -= 1
|
||||
return v
|
||||
|
||||
if __name__ == '__main__':
|
||||
import os
|
||||
import sys
|
||||
# determine module name from this file's name and import it
|
||||
module_name = os.path.splitext(os.path.basename(__file__))[0]
|
||||
bitio = __import__(module_name)
|
||||
|
||||
with open('bitio_test.dat', 'wb') as outfile:
|
||||
writer = bitio.BitWriter(outfile)
|
||||
chars = '12345abcde'
|
||||
for ch in chars:
|
||||
writer.writebits(ord(ch), 7)
|
||||
|
||||
with open('bitio_test.dat', 'rb') as infile:
|
||||
reader = bitio.BitReader(infile)
|
||||
chars = []
|
||||
while True:
|
||||
x = reader.readbits(7)
|
||||
if reader.read == 0:
|
||||
break
|
||||
chars.append(chr(x))
|
||||
print(''.join(chars))
|
||||
|
|
|
|||
|
|
@ -1,4 +1,3 @@
|
|||
#! /usr/bin/env python
|
||||
import sys
|
||||
import bitio
|
||||
|
||||
|
|
|
|||
|
|
@ -1,10 +1,9 @@
|
|||
#! /usr/bin/env python
|
||||
import sys
|
||||
import bitio
|
||||
|
||||
r = bitio.BitReader(sys.stdin)
|
||||
while True:
|
||||
x = r.readbits(7)
|
||||
if ( r.read == 0 ):
|
||||
if not r.read: # nothing read
|
||||
break
|
||||
sys.stdout.write(chr(x))
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue