home *** CD-ROM | disk | FTP | other *** search
- # Source Generated with Decompyle++
- # File: in.pyc (Python 2.4)
-
-
- try:
- True
- except:
- True = 1
- False = 0
-
- bool = lambda x: not (not x)
-
-
- try:
- sum([
- 1])
-
- negsum = lambda a: len(a) - sum(a)
- except:
-
- negsum = lambda a: reduce((lambda x, y: x + (not y)), a, 0)
-
-
-
- def _int_to_booleans(x):
- r = []
- for i in range(8):
- r.append(bool(x & 128))
- x <<= 1
-
- return tuple(r)
-
- lookup_table = [ _int_to_booleans(i) for i in range(256) ]
- reverse_lookup_table = { }
- for i in xrange(256):
- reverse_lookup_table[lookup_table[i]] = chr(i)
-
-
- class Bitfield:
-
- def __init__(self, length, bitstring = None):
- self.length = length
- if bitstring is not None:
- extra = len(bitstring) * 8 - length
- if extra < 0 or extra >= 8:
- raise ValueError
-
- t = lookup_table
- r = []
- for c in bitstring:
- r.extend(t[ord(c)])
-
- if extra > 0:
- if r[-extra:] != [
- 0] * extra:
- raise ValueError
-
- del r[-extra:]
-
- self.array = r
- self.numfalse = negsum(r)
- else:
- self.array = [
- False] * length
- self.numfalse = length
-
-
- def __setitem__(self, index, val):
- val = bool(val)
- self.numfalse += self.array[index] - val
- self.array[index] = val
-
-
- def __getitem__(self, index):
- return self.array[index]
-
-
- def __len__(self):
- return self.length
-
-
- def tostring(self):
- booleans = self.array
- t = reverse_lookup_table
- s = len(booleans) % 8
- r = [ t[tuple(booleans[x:x + 8])] for x in xrange(0, len(booleans) - s, 8) ]
- return ''.join(r)
-
-
- def complete(self):
- return not (self.numfalse)
-
-
-
- def test_bitfield():
-
- try:
- x = Bitfield(7, 'ab')
- if not False:
- raise AssertionError
- except ValueError:
- pass
-
-
- try:
- x = Bitfield(7, 'ab')
- if not False:
- raise AssertionError
- except ValueError:
- pass
-
-
- try:
- x = Bitfield(9, 'abc')
- if not False:
- raise AssertionError
- except ValueError:
- pass
-
-
- try:
- x = Bitfield(0, 'a')
- if not False:
- raise AssertionError
- except ValueError:
- pass
-
-
- try:
- x = Bitfield(1, '')
- if not False:
- raise AssertionError
- except ValueError:
- pass
-
-
- try:
- x = Bitfield(7, '')
- if not False:
- raise AssertionError
- except ValueError:
- pass
-
-
- try:
- x = Bitfield(8, '')
- if not False:
- raise AssertionError
- except ValueError:
- pass
-
-
- try:
- x = Bitfield(9, 'a')
- if not False:
- raise AssertionError
- except ValueError:
- pass
-
-
- try:
- x = Bitfield(7, chr(1))
- if not False:
- raise AssertionError
- except ValueError:
- pass
-
-
- try:
- x = Bitfield(9, chr(0) + chr(64))
- if not False:
- raise AssertionError
- except ValueError:
- pass
-
- if not Bitfield(0, '').tostring() == '':
- raise AssertionError
- if not Bitfield(1, chr(128)).tostring() == chr(128):
- raise AssertionError
- if not Bitfield(7, chr(2)).tostring() == chr(2):
- raise AssertionError
- if not Bitfield(8, chr(255)).tostring() == chr(255):
- raise AssertionError
- if not Bitfield(9, chr(0) + chr(128)).tostring() == chr(0) + chr(128):
- raise AssertionError
- x = Bitfield(1)
- if not x.numfalse == 1:
- raise AssertionError
- x[0] = 1
- if not x.numfalse == 0:
- raise AssertionError
- x[0] = 1
- if not x.numfalse == 0:
- raise AssertionError
- if not x.tostring() == chr(128):
- raise AssertionError
- x = Bitfield(7)
- if not len(x) == 7:
- raise AssertionError
- x[6] = 1
- if not x.numfalse == 6:
- raise AssertionError
- if not x.tostring() == chr(2):
- raise AssertionError
- x = Bitfield(8)
- x[7] = 1
- if not x.tostring() == chr(1):
- raise AssertionError
- x = Bitfield(9)
- x[8] = 1
- if not x.numfalse == 8:
- raise AssertionError
- if not x.tostring() == chr(0) + chr(128):
- raise AssertionError
- x = Bitfield(8, chr(196))
- if not len(x) == 8:
- raise AssertionError
- if not x.numfalse == 5:
- raise AssertionError
- if not x.tostring() == chr(196):
- raise AssertionError
-
-