home *** CD-ROM | disk | FTP | other *** search
/ PC Welt 2006 November (DVD) / PCWELT_11_2006.ISO / casper / filesystem.squashfs / usr / lib / python2.4 / site-packages / BitTorrent / bitfield.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2006-08-31  |  4.5 KB  |  223 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.4)
  3.  
  4.  
  5. try:
  6.     True
  7. except:
  8.     True = 1
  9.     False = 0
  10.     
  11.     bool = lambda x: not (not x)
  12.  
  13.  
  14. try:
  15.     sum([
  16.         1])
  17.     
  18.     negsum = lambda a: len(a) - sum(a)
  19. except:
  20.     
  21.     negsum = lambda a: reduce((lambda x, y: x + (not y)), a, 0)
  22.  
  23.  
  24.  
  25. def _int_to_booleans(x):
  26.     r = []
  27.     for i in range(8):
  28.         r.append(bool(x & 128))
  29.         x <<= 1
  30.     
  31.     return tuple(r)
  32.  
  33. lookup_table = [ _int_to_booleans(i) for i in range(256) ]
  34. reverse_lookup_table = { }
  35. for i in xrange(256):
  36.     reverse_lookup_table[lookup_table[i]] = chr(i)
  37.  
  38.  
  39. class Bitfield:
  40.     
  41.     def __init__(self, length, bitstring = None):
  42.         self.length = length
  43.         if bitstring is not None:
  44.             extra = len(bitstring) * 8 - length
  45.             if extra < 0 or extra >= 8:
  46.                 raise ValueError
  47.             
  48.             t = lookup_table
  49.             r = []
  50.             for c in bitstring:
  51.                 r.extend(t[ord(c)])
  52.             
  53.             if extra > 0:
  54.                 if r[-extra:] != [
  55.                     0] * extra:
  56.                     raise ValueError
  57.                 
  58.                 del r[-extra:]
  59.             
  60.             self.array = r
  61.             self.numfalse = negsum(r)
  62.         else:
  63.             self.array = [
  64.                 False] * length
  65.             self.numfalse = length
  66.  
  67.     
  68.     def __setitem__(self, index, val):
  69.         val = bool(val)
  70.         self.numfalse += self.array[index] - val
  71.         self.array[index] = val
  72.  
  73.     
  74.     def __getitem__(self, index):
  75.         return self.array[index]
  76.  
  77.     
  78.     def __len__(self):
  79.         return self.length
  80.  
  81.     
  82.     def tostring(self):
  83.         booleans = self.array
  84.         t = reverse_lookup_table
  85.         s = len(booleans) % 8
  86.         r = [ t[tuple(booleans[x:x + 8])] for x in xrange(0, len(booleans) - s, 8) ]
  87.         return ''.join(r)
  88.  
  89.     
  90.     def complete(self):
  91.         return not (self.numfalse)
  92.  
  93.  
  94.  
  95. def test_bitfield():
  96.     
  97.     try:
  98.         x = Bitfield(7, 'ab')
  99.         if not False:
  100.             raise AssertionError
  101.     except ValueError:
  102.         pass
  103.  
  104.     
  105.     try:
  106.         x = Bitfield(7, 'ab')
  107.         if not False:
  108.             raise AssertionError
  109.     except ValueError:
  110.         pass
  111.  
  112.     
  113.     try:
  114.         x = Bitfield(9, 'abc')
  115.         if not False:
  116.             raise AssertionError
  117.     except ValueError:
  118.         pass
  119.  
  120.     
  121.     try:
  122.         x = Bitfield(0, 'a')
  123.         if not False:
  124.             raise AssertionError
  125.     except ValueError:
  126.         pass
  127.  
  128.     
  129.     try:
  130.         x = Bitfield(1, '')
  131.         if not False:
  132.             raise AssertionError
  133.     except ValueError:
  134.         pass
  135.  
  136.     
  137.     try:
  138.         x = Bitfield(7, '')
  139.         if not False:
  140.             raise AssertionError
  141.     except ValueError:
  142.         pass
  143.  
  144.     
  145.     try:
  146.         x = Bitfield(8, '')
  147.         if not False:
  148.             raise AssertionError
  149.     except ValueError:
  150.         pass
  151.  
  152.     
  153.     try:
  154.         x = Bitfield(9, 'a')
  155.         if not False:
  156.             raise AssertionError
  157.     except ValueError:
  158.         pass
  159.  
  160.     
  161.     try:
  162.         x = Bitfield(7, chr(1))
  163.         if not False:
  164.             raise AssertionError
  165.     except ValueError:
  166.         pass
  167.  
  168.     
  169.     try:
  170.         x = Bitfield(9, chr(0) + chr(64))
  171.         if not False:
  172.             raise AssertionError
  173.     except ValueError:
  174.         pass
  175.  
  176.     if not Bitfield(0, '').tostring() == '':
  177.         raise AssertionError
  178.     if not Bitfield(1, chr(128)).tostring() == chr(128):
  179.         raise AssertionError
  180.     if not Bitfield(7, chr(2)).tostring() == chr(2):
  181.         raise AssertionError
  182.     if not Bitfield(8, chr(255)).tostring() == chr(255):
  183.         raise AssertionError
  184.     if not Bitfield(9, chr(0) + chr(128)).tostring() == chr(0) + chr(128):
  185.         raise AssertionError
  186.     x = Bitfield(1)
  187.     if not x.numfalse == 1:
  188.         raise AssertionError
  189.     x[0] = 1
  190.     if not x.numfalse == 0:
  191.         raise AssertionError
  192.     x[0] = 1
  193.     if not x.numfalse == 0:
  194.         raise AssertionError
  195.     if not x.tostring() == chr(128):
  196.         raise AssertionError
  197.     x = Bitfield(7)
  198.     if not len(x) == 7:
  199.         raise AssertionError
  200.     x[6] = 1
  201.     if not x.numfalse == 6:
  202.         raise AssertionError
  203.     if not x.tostring() == chr(2):
  204.         raise AssertionError
  205.     x = Bitfield(8)
  206.     x[7] = 1
  207.     if not x.tostring() == chr(1):
  208.         raise AssertionError
  209.     x = Bitfield(9)
  210.     x[8] = 1
  211.     if not x.numfalse == 8:
  212.         raise AssertionError
  213.     if not x.tostring() == chr(0) + chr(128):
  214.         raise AssertionError
  215.     x = Bitfield(8, chr(196))
  216.     if not len(x) == 8:
  217.         raise AssertionError
  218.     if not x.numfalse == 5:
  219.         raise AssertionError
  220.     if not x.tostring() == chr(196):
  221.         raise AssertionError
  222.  
  223.