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 """Read through whitespaces, return 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 """Truncate 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 """Return string of the given object's attribute. 57 58 Defaults to the given fallback value if attribute is not present. 59 """ 60 if hasattr(obj, attr): 61 if fmt is not None: 62 return fmt % getattr(obj, attr) 63 return str(getattr(obj, attr)) 64 return fallback
65 66
67 -def find_test_dir(start_dir=None):
68 """Find the absolute path of Biopython's Tests directory. 69 70 Arguments: 71 start_dir -- Initial directory to begin lookup (default to current dir) 72 73 If the directory is not found up the filesystem's root directory, an 74 exception will be raised. 75 76 """ 77 if not start_dir: 78 # no callbacks in function signatures! 79 # defaults to the current directory 80 # (using __file__ would give the installed Biopython) 81 start_dir = "." 82 83 target = os.path.abspath(start_dir) 84 while True: 85 if os.path.isdir(os.path.join(target, "Bio")) and \ 86 os.path.isdir(os.path.join(target, "Tests")): 87 # Good, we're in the Biopython root now 88 return os.path.abspath(os.path.join(target, "Tests")) 89 # Recurse up the tree 90 # TODO - Test this on Windows 91 new, tmp = os.path.split(target) 92 if target == new: 93 # Reached root 94 break 95 target = new 96 raise ValueError("Not within Biopython source tree: %r" % 97 os.path.abspath(start_dir))
98 99
100 -def run_doctest(target_dir=None, *args, **kwargs):
101 """Run 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("Running 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 122 if __name__ == "__main__": 123 run_doctest() 124