1from __future__ import absolute_import 2 3from struct import pack, unpack 4import sys 5 6if sys.version_info < (3,): 7 compat_ord = ord 8else: 9 def compat_ord(char): 10 return char 11 12try: 13 from itertools import izip 14 compat_izip = izip 15except ImportError: 16 compat_izip = zip 17 18try: 19 from cStringIO import StringIO 20except ImportError: 21 from io import StringIO 22 23try: 24 from BytesIO import BytesIO 25except ImportError: 26 from io import BytesIO 27 28if sys.version_info < (3,): 29 def iteritems(d, **kw): 30 return d.iteritems(**kw) 31 32 def intround(num): 33 return int(round(num)) 34 35else: 36 def iteritems(d, **kw): 37 return iter(d.items(**kw)) 38 39 # python3 will return an int if you round to 0 decimal places 40 intround = round 41 42 43def ntole(v): 44 """convert a 2-byte word from the network byte order (big endian) to little endian; 45 replaces socket.ntohs() to work on both little and big endian architectures 46 """ 47 return unpack('<H', pack('!H', v))[0] 48 49 50def isstr(s): 51 """True if 's' is an instance of basestring in py2, or of str in py3""" 52 bs = getattr(__builtins__, 'basestring', str) 53 return isinstance(s, bs) 54