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

Source Code for Module Bio._utils

  1  # Copyright 2010 by Eric Talevich. All rights reserved. 
  2  # Copyright 2012 by Wibowo Arindrarto. All rights reserved. 
  3  # This code is part of the Biopython distribution and governed by its 
  4  # license.  Please see the LICENSE file that should have been included 
  5  # as part of this package. 
  6   
  7  """Common utility functions for various Bio submodules.""" 
  8   
  9  import os 
 10   
 11   
12 -def iterlen(items):
13 """Count the number of items in an iterable. 14 15 If the argument supports len(items), and some iterators do, then 16 this returns len(items). Otherwise it will scan over the entries 17 in order to count them. 18 19 Exhausts a generator, but doesn't require creating a full list. 20 21 >>> iterlen("abcde") 22 5 23 >>> iterlen(iter("abcde")) 24 5 25 >>> iterlen(xrange(5)) 26 5 27 28 """ 29 try: 30 #e.g. Under Python 2, the xrange iterator defines __len__ 31 return len(items) 32 except TypeError: 33 for i, x in enumerate(items): 34 count = i 35 return count + 1
36 37
38 -def read_forward(handle):
39 """Reads through whitespaces, returns the first non-whitespace line.""" 40 while True: 41 line = handle.readline() 42 # if line has characters and stripping does not remove them, 43 # return the line 44 if line and line.strip(): 45 return line 46 # if line ends, return None 47 elif not line: 48 return line
49 50
51 -def trim_str(string, max_len, concat_char):
52 """Truncates the given string for display.""" 53 if len(string) > max_len: 54 return string[:max_len - len(concat_char)] + concat_char 55 return string
56 57
58 -def getattr_str(obj, attr, fmt=None, fallback='?'):
59 """Returns a string of the given object's attribute, defaulting to the 60 fallback value if attribute is not present.""" 61 if hasattr(obj, attr): 62 if fmt is not None: 63 return fmt % getattr(obj, attr) 64 return str(getattr(obj, attr)) 65 return fallback
66 67
68 -def find_test_dir(start_dir=None):
69 """Finds the absolute path of Biopython's Tests directory. 70 71 Arguments: 72 start_dir -- Initial directory to begin lookup (default to current dir) 73 74 If the directory is not found up the filesystem's root directory, an 75 exception will be raised. 76 77 """ 78 if not start_dir: 79 # no callbacks in function signatures! 80 # defaults to the current directory 81 # (using __file__ would give the installed Biopython) 82 start_dir = "." 83 84 target = os.path.abspath(start_dir) 85 while True: 86 if os.path.isdir(os.path.join(target, "Bio")) \ 87 and os.path.isdir(os.path.join(target, "Tests")): 88 #Good, we're in the Biopython root now 89 return os.path.abspath(os.path.join(target, "Tests")) 90 #Recurse up the tree 91 #TODO - Test this on Windows 92 new, tmp = os.path.split(target) 93 if target == new: 94 #Reached root 95 break 96 target = new 97 raise ValueError("Not within Biopython source tree: %r" % os.path.abspath(start_dir))
98 99
100 -def run_doctest(target_dir=None, *args, **kwargs):
101 """Runs doctest for the importing module.""" 102 import doctest 103 104 # default doctest options 105 default_kwargs = { 106 'optionflags': doctest.ELLIPSIS, 107 } 108 kwargs.update(default_kwargs) 109 110 cur_dir = os.path.abspath(os.curdir) 111 112 print "Runing doctests..." 113 try: 114 os.chdir(find_test_dir(target_dir)) 115 doctest.testmod(*args, **kwargs) 116 finally: 117 # and revert back to initial directory 118 os.chdir(cur_dir) 119 print "Done"
120 121 if __name__ == "__main__": 122 run_doctest() 123