added a few helper methods to gapstring

This commit is contained in:
pi-rho 2012-06-07 21:52:03 -05:00
parent 5ee05be32c
commit 7768f73f1b
1 changed files with 46 additions and 0 deletions

View File

@ -192,6 +192,15 @@ class GapString(object):
new.append(i) new.append(i)
return new return new
def __contains__(self, needle):
for i in self.contents:
try:
if needle in i:
return True
except TypeError:
pass
return False
def index(self, needle): def index(self, needle):
pos = 0 pos = 0
for i in self.contents: for i in self.contents:
@ -222,6 +231,43 @@ class GapString(object):
def endswith(self, what): def endswith(self, what):
return (what == str(self[-len(what):])) return (what == str(self[-len(what):]))
def is_printable(self):
"""
Tests to confirm that all the characters
in the string are printable
"""
for element in self.contents:
if type(element) == int:
continue
for character in element:
if character not in string.printable:
return False
return True
def mostly_printable(self, ratio=.90):
"""
Tests calculate the percentate of printable characters
if there are more printable characters than the ratio
given return true.
"""
numb_chars = 0
numb_printable = 0
for element in self.contents:
if type(element) == int:
continue
for character in element:
numb_chars += 1
if character in string.printable:
numb_printable += 1
threshold = int(numb_chars * ratio)
if numb_printable > threshold:
return True
return False
if __name__ == '__main__': if __name__ == '__main__':
gs = GapString() gs = GapString()