1
2
3
4
5
6
7 """Common utility functions for various Bio submodules."""
8
9 import os
10
11
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
31 return len(items)
32 except TypeError:
33 for i, x in enumerate(items):
34 count = i
35 return count + 1
36
37
39 """Reads through whitespaces, returns the first non-whitespace line."""
40 while True:
41 line = handle.readline()
42
43
44 if line and line.strip():
45 return line
46
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
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
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
80
81
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
89 return os.path.abspath(os.path.join(target, "Tests"))
90
91
92 new, tmp = os.path.split(target)
93 if target == new:
94
95 break
96 target = new
97 raise ValueError("Not within Biopython source tree: %r" % os.path.abspath(start_dir))
98
99
101 """Runs doctest for the importing module."""
102 import doctest
103
104
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
118 os.chdir(cur_dir)
119 print "Done"
120
121 if __name__ == "__main__":
122 run_doctest()
123