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  from __future__ import print_function 
 10   
 11  import os 
 12   
 13   
14 -def iterlen(items):
15 """Count the number of items in an iterable. 16 17 If the argument supports len(items), and some iterators do, then 18 this returns len(items). Otherwise it will scan over the entries 19 in order to count them. 20 21 Exhausts a generator, but doesn't require creating a full list. 22 23 >>> iterlen("abcde") 24 5 25 >>> iterlen(iter("abcde")) 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 is empty or line has characters and stripping does not remove 43 # them, return the line 44 if (not line) or (line and line.strip()): 45 return line
46 47
48 -def trim_str(string, max_len, concat_char):
49 """Truncates the given string for display.""" 50 if len(string) > max_len: 51 return string[:max_len - len(concat_char)] + concat_char 52 return string
53 54
55 -def getattr_str(obj, attr, fmt=None, fallback='?'):
56 """Returns a string of the given object's attribute, defaulting to the 57 fallback value if attribute is not present.""" 58 if hasattr(obj, attr): 59 if fmt is not None: 60 return fmt % getattr(obj, attr) 61 return str(getattr(obj, attr)) 62 return fallback
63 64
65 -def find_test_dir(start_dir=None):
66 """Finds the absolute path of Biopython's Tests directory. 67 68 Arguments: 69 start_dir -- Initial directory to begin lookup (default to current dir) 70 71 If the directory is not found up the filesystem's root directory, an 72 exception will be raised. 73 74 """ 75 if not start_dir: 76 # no callbacks in function signatures! 77 # defaults to the current directory 78 # (using __file__ would give the installed Biopython) 79 start_dir = "." 80 81 target = os.path.abspath(start_dir) 82 while True: 83 if os.path.isdir(os.path.join(target, "Bio")) \ 84 and os.path.isdir(os.path.join(target, "Tests")): 85 #Good, we're in the Biopython root now 86 return os.path.abspath(os.path.join(target, "Tests")) 87 #Recurse up the tree 88 #TODO - Test this on Windows 89 new, tmp = os.path.split(target) 90 if target == new: 91 #Reached root 92 break 93 target = new 94 raise ValueError("Not within Biopython source tree: %r" % os.path.abspath(start_dir))
95 96
97 -def run_doctest(target_dir=None, *args, **kwargs):
98 """Runs doctest for the importing module.""" 99 import doctest 100 101 # default doctest options 102 default_kwargs = { 103 'optionflags': doctest.ELLIPSIS, 104 } 105 kwargs.update(default_kwargs) 106 107 cur_dir = os.path.abspath(os.curdir) 108 109 print("Runing doctests...") 110 try: 111 os.chdir(find_test_dir(target_dir)) 112 doctest.testmod(*args, **kwargs) 113 finally: 114 # and revert back to initial directory 115 os.chdir(cur_dir) 116 print("Done")
117 118 if __name__ == "__main__": 119 run_doctest() 120