From 7768f73f1be1c7812de32ba005d9ea15ea9023e8 Mon Sep 17 00:00:00 2001 From: pi-rho Date: Thu, 7 Jun 2012 21:52:03 -0500 Subject: [PATCH] added a few helper methods to gapstring --- netarch/gapstr.py | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/netarch/gapstr.py b/netarch/gapstr.py index 6ae005d..c4ae04d 100644 --- a/netarch/gapstr.py +++ b/netarch/gapstr.py @@ -192,6 +192,15 @@ class GapString(object): new.append(i) 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): pos = 0 for i in self.contents: @@ -222,6 +231,43 @@ class GapString(object): def endswith(self, 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__': gs = GapString()