home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2011 February / maximum-cd-2011-02.iso / DiscContents / digsby_setup85.exe / lib / util / primitives / bits.pyo (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2010-11-24  |  3.5 KB  |  73 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.6)
  3.  
  4. import mapping
  5. import itertools
  6. import random
  7.  
  8. def hex2bin(s):
  9.     return ''.join((lambda .0: for x in .0:
  10. chr(int(x, 16)))(s.split()))
  11.  
  12.  
  13. def rand32():
  14.     return random.randint(1, 0xFFFFFFFFL)
  15.  
  16.  
  17. def getrandbytes(n):
  18.     return ''.join((lambda .0: for _i in .0:
  19. chr(random.getrandbits(8)))(range(n)))
  20.  
  21.  
  22. def rol(i, n, bits = 32):
  23.     (div, mod) = divmod(i << n, 2 ** bits - 1)
  24.     return mod | div >> bits
  25.  
  26.  
  27. def ror(i, n, bits = 32):
  28.     return (i % 2 ** n << bits - n) + (i >> n)
  29.  
  30.  
  31. def bitfields(*names):
  32.     bits = [ 2 ** i for i in xrange(len(names)) ]
  33.     return mapping.Storage(itertools.izip(names, bits))
  34.  
  35.  
  36. class BitFlags(object):
  37.     
  38.     def __init__(self, names):
  39.         self._field = bitfields(*names)
  40.         self.__dict__.update(dict.fromkeys(names, False))
  41.  
  42.     
  43.     def Pack(self):
  44.         return reduce((lambda a, b: a | b), (map,)((lambda x: getattr(self._field, x) * getattr(self, x)), self._field))
  45.  
  46.     
  47.     def Unpack(self, val):
  48.         _[1]
  49.  
  50.  
  51.  
  52. def leftrotate(s, shift, size = 32):
  53.     max = pow(2, size)
  54.     s = s % max << shift
  55.     (wrap, s) = divmod(s, max)
  56.     return s | wrap
  57.  
  58.  
  59. def utf7_to_int(databuf):
  60.     total = i = 0
  61.     more_bytes = True
  62.     while more_bytes:
  63.         byte = ord(databuf[i])
  64.         more_bytes = bool(byte & 128)
  65.         total |= (byte & 127) * (1 << 7 * i)
  66.         i += 1
  67.     return (total, i)
  68.  
  69. if __name__ == '__main__':
  70.     import doctest
  71.     doctest.testmod(verbose = True)
  72.  
  73.