1"""Miscellaneous functions to mask Python version differences.""" 2 3import sys 4import os 5 6if sys.version_info < (2,2): 7 raise AssertionError("Python 2.2 or later required") 8 9if sys.version_info < (2,3): 10 11 def enumerate(collection): 12 return zip(range(len(collection)), collection) 13 14 class Set: 15 def __init__(self, seq=None): 16 self.values = {} 17 if seq: 18 for e in seq: 19 self.values[e] = None 20 21 def add(self, e): 22 self.values[e] = None 23 24 def discard(self, e): 25 if e in self.values.keys(): 26 del(self.values[e]) 27 28 def union(self, s): 29 ret = Set() 30 for e in self.values.keys(): 31 ret.values[e] = None 32 for e in s.values.keys(): 33 ret.values[e] = None 34 return ret 35 36 def issubset(self, other): 37 for e in self.values.keys(): 38 if e not in other.values.keys(): 39 return False 40 return True 41 42 def __nonzero__( self): 43 return len(self.values.keys()) 44 45 def __contains__(self, e): 46 return e in self.values.keys() 47 48 def __iter__(self): 49 return iter(set.values.keys()) 50 51 52if os.name != "java": 53 54 import array 55 def createByteArraySequence(seq): 56 return array.array('B', seq) 57 def createByteArrayZeros(howMany): 58 return array.array('B', [0] * howMany) 59 def concatArrays(a1, a2): 60 return a1+a2 61 62 def bytesToString(bytes): 63 return bytes.tostring() 64 def stringToBytes(s): 65 bytes = createByteArrayZeros(0) 66 bytes.fromstring(s) 67 return bytes 68 69 import math 70 def numBits(n): 71 if n==0: 72 return 0 73 s = "%x" % n 74 return ((len(s)-1)*4) + \ 75 {'0':0, '1':1, '2':2, '3':2, 76 '4':3, '5':3, '6':3, '7':3, 77 '8':4, '9':4, 'a':4, 'b':4, 78 'c':4, 'd':4, 'e':4, 'f':4, 79 }[s[0]] 80 return int(math.floor(math.log(n, 2))+1) 81 82 BaseException = Exception 83 import sys 84 import traceback 85 def formatExceptionTrace(e): 86 newStr = "".join(traceback.format_exception(sys.exc_type, sys.exc_value, sys.exc_traceback)) 87 return newStr 88 89else: 90 #Jython 2.1 is missing lots of python 2.3 stuff, 91 #which we have to emulate here: 92 #NOTE: JYTHON SUPPORT NO LONGER WORKS, DUE TO USE OF GENERATORS. 93 #THIS CODE IS LEFT IN SO THAT ONE JYTHON UPDATES TO 2.2, IT HAS A 94 #CHANCE OF WORKING AGAIN. 95 96 import java 97 import jarray 98 99 def createByteArraySequence(seq): 100 if isinstance(seq, type("")): #If it's a string, convert 101 seq = [ord(c) for c in seq] 102 return jarray.array(seq, 'h') #use short instead of bytes, cause bytes are signed 103 def createByteArrayZeros(howMany): 104 return jarray.zeros(howMany, 'h') #use short instead of bytes, cause bytes are signed 105 def concatArrays(a1, a2): 106 l = list(a1)+list(a2) 107 return createByteArraySequence(l) 108 109 #WAY TOO SLOW - MUST BE REPLACED------------ 110 def bytesToString(bytes): 111 return "".join([chr(b) for b in bytes]) 112 113 def stringToBytes(s): 114 bytes = createByteArrayZeros(len(s)) 115 for count, c in enumerate(s): 116 bytes[count] = ord(c) 117 return bytes 118 #WAY TOO SLOW - MUST BE REPLACED------------ 119 120 def numBits(n): 121 if n==0: 122 return 0 123 n= 1L * n; #convert to long, if it isn't already 124 return n.__tojava__(java.math.BigInteger).bitLength() 125 126 #Adjust the string to an array of bytes 127 def stringToJavaByteArray(s): 128 bytes = jarray.zeros(len(s), 'b') 129 for count, c in enumerate(s): 130 x = ord(c) 131 if x >= 128: x -= 256 132 bytes[count] = x 133 return bytes 134 135 BaseException = java.lang.Exception 136 import sys 137 import traceback 138 def formatExceptionTrace(e): 139 newStr = "".join(traceback.format_exception(sys.exc_type, sys.exc_value, sys.exc_traceback)) 140 return newStr