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")) and \ 84 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" % 95 os.path.abspath(start_dir))
96 97
98 -def run_doctest(target_dir=None, *args, **kwargs):
99 """Runs doctest for the importing module.""" 100 import doctest 101 102 # default doctest options 103 default_kwargs = { 104 'optionflags': doctest.ELLIPSIS, 105 } 106 kwargs.update(default_kwargs) 107 108 cur_dir = os.path.abspath(os.curdir) 109 110 print("Running doctests...") 111 try: 112 os.chdir(find_test_dir(target_dir)) 113 doctest.testmod(*args, **kwargs) 114 finally: 115 # and revert back to initial directory 116 os.chdir(cur_dir) 117 print("Done")
118 119 if __name__ == "__main__": 120 run_doctest() 121