Package Bio :: Module stringfns
[hide private]
[frames] | no frames]

Source Code for Module Bio.stringfns

 1  # Copyright 2000 by Jeffrey Chang.  All rights reserved. 
 2  # This code is part of the Biopython distribution and governed by its 
 3  # license.  Please see the LICENSE file that should have been included 
 4  # as part of this package. 
 5   
 6  """This provides useful general functions for working with strings (OBSOLETE). 
 7   
 8  This module and its C code equivalent are considered to be obsolete, and 
 9  are likely to be deprecated in a future release of Biopython, before being 
10  removed.  Please get in touch via the mailing list if this will affect you. 
11   
12  Functions: 
13  splitany       Split a string using many delimiters. 
14  find_anychar   Find one of a list of characters in a string. 
15  rfind_anychar  Find one of a list of characters in a string, from end to start. 
16  starts_with    Check whether a string starts with another string [DEPRECATED]. 
17   
18  """ 
19 -def splitany(s, sep=" \011\012\013\014\015", maxsplit=None, negate=0):
20 """splitany(s [,sep [,maxsplit [,negate]]]) -> list of strings 21 22 Split a string. Similar to string.split, except that this considers 23 any one of the characters in sep to be a delimiter. If negate is 24 true, then everything but sep will be a separator. 25 26 """ 27 strlist = [] 28 prev = 0 29 for i in range(len(s)): 30 if maxsplit is not None and len(strlist) >= maxsplit: 31 break 32 if (s[i] in sep) == (not negate): 33 strlist.append(s[prev:i]) 34 prev = i+1 35 strlist.append(s[prev:]) 36 return strlist
37
38 -def find_anychar(string, chars, index=None, negate=0):
39 """find_anychar(string, chars[, index]) -> index of a character or -1 40 41 Find a character in string. chars is a list of characters to look 42 for. Return the index of the first occurrence of any of the 43 characters, or -1 if not found. index is the index where the 44 search should start. By default, I search from the beginning of 45 the string. 46 47 """ 48 if index is None: 49 index = 0 50 while index < len(string) and \ 51 ((not negate and string[index] not in chars) or 52 (negate and string[index] in chars)): 53 index += 1 54 if index == len(string): 55 return -1 56 return index
57
58 -def rfind_anychar(string, chars, index=None, negate=0):
59 """rfind_anychar(string, chars[, index]) -> index of a character or -1 60 61 Find a character in string, looking from the end to the start. 62 chars is a list of characters to look for. Return the index of 63 the first occurrence of any of the characters, or -1 if not found. 64 index is the index where the search should start. By default, I 65 search from the end of the string. 66 67 """ 68 if index is None: 69 index = len(string)-1 70 while index >= 0 and \ 71 ((not negate and string[index] not in chars) or 72 (negate and string[index] in chars)): 73 index -= 1 74 # If not found, index will already be -1. 75 return index
76 77 # Try and load C implementations of functions. If I can't, 78 # then just ignore and use the pure python implementations. 79 try: 80 from cstringfns import * 81 except ImportError: 82 pass 83