Package Bio :: Module _py3k
[hide private]
[frames] | no frames]

Source Code for Module Bio._py3k

 1  # Copyright 2010 by Peter Cock.  All rights reserved. 
 2  # This code is part of the Biopython distribution and governed by its 
 3  # license.  Please see the LICENSE file that should have been included 
 4  # as part of this package. 
 5  """Python 3 compatibility tools (PRIVATE).""" 
 6   
 7  import sys 
 8   
 9  if sys.version_info[0] >= 3: 
10      #Python 3 code (which will be converted using 2to3 script) 
11   
12      _bytes_to_string = lambda b: b.decode() # bytes to unicode string 
13      _string_to_bytes = lambda s: s.encode() # unicode string to bytes 
14   
15 - def _as_unicode(s):
16 """Turn byte string or unicode string into a unicode string.""" 17 if isinstance(s, str): 18 return s 19 #Assume it is a bytes string 20 return s.decode()
21 22
23 - def _as_bytes(s):
24 """Turn byte string or unicode string into a bytes string. 25 26 The Python 2 version returns a (byte) string. 27 """ 28 if isinstance(s, bytes): 29 return s 30 #Assume it is a unicode string 31 return s.encode()
32 33 _as_string = _as_unicode 34
35 - def _is_int_or_long(i):
36 """Check if the value is an integer. 37 38 Note there are no longs on Python 3. 39 """ 40 return isinstance(i, int)
41 42 else: 43 #Python 2 code 44 45 _bytes_to_string = lambda b: b # bytes to string, i.e. do nothing 46 _string_to_bytes = lambda s: str(s) # str (or unicode) to bytes string 47
48 - def _as_unicode(s):
49 """Turn a (byte) string or a unicode string into a (byte) string.""" 50 #Will be changed by 2to3 to "isinstance(s, str)" but doesn't matter: 51 if isinstance(s, unicode): 52 return s 53 return s.decode()
54
55 - def _as_bytes(s):
56 """Turn a (byte) string or a unicode string into a (byte) string.""" 57 return str(s)
58 59 _as_string = _as_bytes 60
61 - def _is_int_or_long(i):
62 """Check if the value is an integer or long.""" 63 #If the 2to3 long fixer is enabled (which it is by default), this 64 #will be changed to "isinstance(i, int) or isinstance(i, int)" 65 #but that doesn't matter. 66 return isinstance(i, int) or isinstance(i, long)
67