home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / var / lib / python-support / python2.6 / gdata / tlslite / utils / compat.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2009-04-20  |  6.8 KB  |  182 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. '''Miscellaneous functions to mask Python version differences.'''
  5. import sys
  6. import os
  7. if sys.version_info < (2, 2):
  8.     raise AssertionError('Python 2.2 or later required')
  9. sys.version_info < (2, 2)
  10. if sys.version_info < (2, 3):
  11.     
  12.     def enumerate(collection):
  13.         return zip(range(len(collection)), collection)
  14.  
  15.     
  16.     class Set:
  17.         
  18.         def __init__(self, seq = None):
  19.             self.values = { }
  20.             if seq:
  21.                 for e in seq:
  22.                     self.values[e] = None
  23.                 
  24.             
  25.  
  26.         
  27.         def add(self, e):
  28.             self.values[e] = None
  29.  
  30.         
  31.         def discard(self, e):
  32.             if e in self.values.keys():
  33.                 del self.values[e]
  34.             
  35.  
  36.         
  37.         def union(self, s):
  38.             ret = Set()
  39.             for e in self.values.keys():
  40.                 ret.values[e] = None
  41.             
  42.             for e in s.values.keys():
  43.                 ret.values[e] = None
  44.             
  45.             return ret
  46.  
  47.         
  48.         def issubset(self, other):
  49.             for e in self.values.keys():
  50.                 if e not in other.values.keys():
  51.                     return False
  52.             
  53.             return True
  54.  
  55.         
  56.         def __nonzero__(self):
  57.             return len(self.values.keys())
  58.  
  59.         
  60.         def __contains__(self, e):
  61.             return e in self.values.keys()
  62.  
  63.         
  64.         def __iter__(self):
  65.             return iter(set.values.keys())
  66.  
  67.  
  68.  
  69. if os.name != 'java':
  70.     import array
  71.     
  72.     def createByteArraySequence(seq):
  73.         return array.array('B', seq)
  74.  
  75.     
  76.     def createByteArrayZeros(howMany):
  77.         return array.array('B', [
  78.             0] * howMany)
  79.  
  80.     
  81.     def concatArrays(a1, a2):
  82.         return a1 + a2
  83.  
  84.     
  85.     def bytesToString(bytes):
  86.         return bytes.tostring()
  87.  
  88.     
  89.     def stringToBytes(s):
  90.         bytes = createByteArrayZeros(0)
  91.         bytes.fromstring(s)
  92.         return bytes
  93.  
  94.     import math
  95.     
  96.     def numBits(n):
  97.         if n == 0:
  98.             return 0
  99.         s = '%x' % n
  100.         return (len(s) - 1) * 4 + {
  101.             '0': 0,
  102.             '1': 1,
  103.             '2': 2,
  104.             '3': 2,
  105.             '4': 3,
  106.             '5': 3,
  107.             '6': 3,
  108.             '7': 3,
  109.             '8': 4,
  110.             '9': 4,
  111.             'a': 4,
  112.             'b': 4,
  113.             'c': 4,
  114.             'd': 4,
  115.             'e': 4,
  116.             'f': 4 }[s[0]]
  117.         return int(math.floor(math.log(n, 2)) + 1)
  118.  
  119.     BaseException = Exception
  120.     import sys
  121.     import traceback
  122.     
  123.     def formatExceptionTrace(e):
  124.         newStr = ''.join(traceback.format_exception(sys.exc_type, sys.exc_value, sys.exc_traceback))
  125.         return newStr
  126.  
  127. else:
  128.     import java
  129.     import jarray
  130.     
  131.     def createByteArraySequence(seq):
  132.         return jarray.array(seq, 'h')
  133.  
  134.     
  135.     def createByteArrayZeros(howMany):
  136.         return jarray.zeros(howMany, 'h')
  137.  
  138.     
  139.     def concatArrays(a1, a2):
  140.         l = list(a1) + list(a2)
  141.         return createByteArraySequence(l)
  142.  
  143.     
  144.     def bytesToString(bytes):
  145.         return []([ chr(b) for b in bytes ])
  146.  
  147.     
  148.     def stringToBytes(s):
  149.         bytes = createByteArrayZeros(len(s))
  150.         for count, c in enumerate(s):
  151.             bytes[count] = ord(c)
  152.         
  153.         return bytes
  154.  
  155.     
  156.     def numBits(n):
  157.         if n == 0:
  158.             return 0
  159.         n = 0x1L * n
  160.         return n.__tojava__(java.math.BigInteger).bitLength()
  161.  
  162.     
  163.     def stringToJavaByteArray(s):
  164.         bytes = jarray.zeros(len(s), 'b')
  165.         for count, c in enumerate(s):
  166.             x = ord(c)
  167.             if x >= 128:
  168.                 x -= 256
  169.             
  170.             bytes[count] = x
  171.         
  172.         return bytes
  173.  
  174.     BaseException = java.lang.Exception
  175.     import sys
  176.     import traceback
  177.     
  178.     def formatExceptionTrace(e):
  179.         newStr = ''.join(traceback.format_exception(sys.exc_type, sys.exc_value, sys.exc_traceback))
  180.         return newStr
  181.  
  182.