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 """ 59 if hasattr(obj, attr): 60 if fmt is not None: 61 return fmt % getattr(obj, attr) 62 return str(getattr(obj, attr)) 63 return fallback
64 65
66 -def find_test_dir(start_dir=None):
67 """Finds the absolute path of Biopython's Tests directory. 68 69 Arguments: 70 start_dir -- Initial directory to begin lookup (default to current dir) 71 72 If the directory is not found up the filesystem's root directory, an 73 exception will be raised. 74 75 """ 76 if not start_dir: 77 # no callbacks in function signatures! 78 # defaults to the current directory 79 # (using __file__ would give the installed Biopython) 80 start_dir = "." 81 82 target = os.path.abspath(start_dir) 83 while True: 84 if os.path.isdir(os.path.join(target, "Bio")) and \ 85 os.path.isdir(os.path.join(target, "Tests")): 86 # Good, we're in the Biopython root now 87 return os.path.abspath(os.path.join(target, "Tests")) 88 # Recurse up the tree 89 # TODO - Test this on Windows 90 new, tmp = os.path.split(target) 91 if target == new: 92 # Reached root 93 break 94 target = new 95 raise ValueError("Not within Biopython source tree: %r" % 96 os.path.abspath(start_dir))
97 98
99 -def run_doctest(target_dir=None, *args, **kwargs):
100 """Runs doctest for the importing module.""" 101 import doctest 102 103 # default doctest options 104 default_kwargs = { 105 'optionflags': doctest.ELLIPSIS, 106 } 107 kwargs.update(default_kwargs) 108 109 cur_dir = os.path.abspath(os.curdir) 110 111 print("Running doctests...") 112 try: 113 os.chdir(find_test_dir(target_dir)) 114 doctest.testmod(*args, **kwargs) 115 finally: 116 # and revert back to initial directory 117 os.chdir(cur_dir) 118 print("Done")
119 120 121 if __name__ == "__main__": 122 run_doctest() 123