Back to index

python-biopython  1.60
_sugar.py
Go to the documentation of this file.
00001 # Copyright (C) 2010 by Eric Talevich (eric.talevich@gmail.com)
00002 # This code is part of the Biopython distribution and governed by its
00003 # license. Please see the LICENSE file that should have been included
00004 # as part of this package.
00005 
00006 """Short helper functions (syntax sugar) used in Bio.Phylo.
00007 
00008 The amount of code in this file should be kept to a minimum.
00009 """
00010 __docformat__ = "restructuredtext en"
00011 
00012 
00013 def iterlen(items):
00014     """Count the number of items in an iterable.
00015 
00016     Exhausts a generator, but doesn't require creating a full list.
00017     """
00018     for i, x in enumerate(items):
00019         count = i
00020     return count + 1
00021 
00022 
00023 def trim_str(text, maxlen=60):
00024     """Truncate a string to maxlen characters, including ellipsis."""
00025     assert isinstance(text, basestring), \
00026             "%s should be a string, not a %s" % (text, type(text))
00027     if len(text) > maxlen:
00028         return text[:maxlen-3] + '...'
00029     return text