Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

109 lines (86 sloc) 3.297 kb
# Copyright 2010 by Eric Talevich. All rights reserved.
# Copyright 2012 by Wibowo Arindrarto. All rights reserved.
# This code is part of the Biopython distribution and governed by its
# license. Please see the LICENSE file that should have been included
# as part of this package.
"""Common utility functions for various Bio submodules."""
import os
def iterlen(items):
"""Count the number of items in an iterable.
Exhausts a generator, but doesn't require creating a full list.
"""
for i, x in enumerate(items):
count = i
return count + 1
def read_forward(handle):
"""Reads through whitespaces, returns the first non-whitespace line."""
while True:
line = handle.readline()
# if line has characters and stripping does not remove them,
# return the line
if line and line.strip():
return line
# if line ends, return None
elif not line:
return line
def trim_str(string, max_len, concat_char):
"""Truncates the given string for display."""
if len(string) > max_len:
return string[:max_len - len(concat_char)] + concat_char
return string
def getattr_str(obj, attr, fmt=None, fallback='?'):
"""Returns a string of the given object's attribute, defaulting to the
fallback value if attribute is not present."""
if hasattr(obj, attr):
if fmt is not None:
return fmt % getattr(obj, attr)
return str(getattr(obj, attr))
return fallback
def find_test_dir(start_dir=None):
"""Finds the absolute path of Biopython's Tests directory.
Arguments:
start_dir -- Initial directory to begin lookup (default to current dir)
If the directory is not found up the filesystem's root directory, an
exception will be raised.
"""
if not start_dir:
# no callbacks in function signatures!
# defaults to the current directory
# (using __file__ would give the installed Biopython)
start_dir = "."
target = os.path.abspath(start_dir)
while True:
if os.path.isdir(os.path.join(target, "Bio")) \
and os.path.isdir(os.path.join(target, "Tests")):
#Good, we're in the Biopython root now
return os.path.abspath(os.path.join(target, "Tests"))
#Recurse up the tree
#TODO - Test this on Windows
new, tmp = os.path.split(target)
if target == new:
#Reached root
break
target = new
raise ValueError("Not within Biopython source tree: %r" % os.path.abspath(start_dir))
def run_doctest(target_dir=None, *args, **kwargs):
"""Runs doctest for the importing module."""
import doctest
# default doctest options
default_kwargs = {
'optionflags': doctest.ELLIPSIS,
}
kwargs.update(default_kwargs)
test_dir = find_test_dir()
# set target directory if it's not None
if target_dir is not None:
target_dir = os.path.join(test_dir, target_dir)
else:
target_dir = test_dir
cur_dir = os.path.abspath(os.curdir)
print "Runing doctests..."
# change to test directory
os.chdir(target_dir)
doctest.testmod(*args, **kwargs)
# and revert back to initial directory
os.chdir(cur_dir)
print "Done"
Jump to Line
Something went wrong with that request. Please try again.