Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import timeit
- import re
- def read_word(pstr, lower=True):
- if not pstr:
- return "", ""
- pstr = pstr.lstrip()
- locate = len(pstr)
- unmatch = False
- if locate == 0:
- return "", ""
- if pstr[0] == "'":
- locate = pstr.find("'", 1)+1
- if locate == 0:
- locate = len(pstr)
- unmatch = True
- elif pstr[0] == '"':
- locate = pstr.find('"', 1)+1
- if locate == 0:
- locate = len(pstr)
- unmatch = True
- else:
- for i, c in enumerate(pstr):
- if c.isspace():
- locate = i
- break
- word = pstr[:locate]
- strip = len(word)
- if not word:
- return pstr, word
- if word[0] in ['"', "'"]:
- word = word[1:]
- if not unmatch:
- word = word[:-1]
- if lower:
- word = word.lower()
- return pstr.lstrip()[strip:], word.strip()
- def is_name(arg, name):
- name, tmp = read_word(name)
- if not arg:
- return False
- while tmp:
- if tmp.lower().startswith(arg):
- return True
- name, tmp = read_word(name)
- return False
- def alt_read_word(pstr, lower=True):
- if not pstr:
- return '',''
- start = None
- end = None
- i = -1
- for c in pstr:
- i += 1
- if c == "'" and start is None:
- start = i + 1
- quote = pstr.find("'", i+1)
- if quote > -1:
- end = quote
- else:
- end = len(pstr)
- if lower:
- return pstr[end+1:], pstr[start:end].lower()
- else:
- return pstr[end+1:], pstr[start:end]
- break
- elif c == '"' and start is None:
- start = i + 1
- quote = pstr.find('"', i+1)
- if quote > -1:
- end = quote
- else:
- end = len(pstr)
- if lower:
- return pstr[end+1:], pstr[start:end].lower()
- else:
- return pstr[end+1:], pstr[start:end]
- break
- elif c.isspace():
- if start is not None:
- end = i
- break
- else:
- if start is None:
- start = i
- if not end:
- end = len(pstr)
- if lower:
- return pstr[end:], pstr[start:end].lower()
- else:
- return pstr[end:], pstr[start:end]
- def alt_is_name(arg, name):
- if not arg or not name:
- return False
- name, tmp = alt_read_word(name)
- while tmp:
- if tmp.startswith(arg):
- return True
- name, tmp = alt_read_word(name)
- return False
- _breakup = re.compile('(\".*?\"|\'.*?\'|[^\s]+)')
- def new_is_name(arg, name):
- if not arg or not name:
- return False
- arg = arg.lower()
- words = _breakup.findall(name)
- for word in words:
- if word[0] in ('"', "'"):
- word = word[1:-1]
- if word.lower().startswith(arg):
- return True
- return False
- def test_read_word(a: str, b: list, do_print: bool=False):
- if do_print: print("----------OLD-----------")
- if do_print: print('Start: %s' % a)
- leftovers, word = read_word(a)
- if do_print: print('(word, leftovers) = ("%s", "%s")' % (word, leftovers))
- while(leftovers):
- leftovers, word = read_word(leftovers)
- if do_print: print('(word, leftovers) = ("%s", "%s")' % (word, leftovers))
- def test_alt_read_word(a: str, b: list, do_print: bool=False):
- if do_print: print("----------ALT-----------")
- if do_print: print('Start: %s' % a)
- leftovers, word = alt_read_word(a)
- if do_print: print('(word, leftovers) = ("%s", "%s")' % (word, leftovers))
- while(leftovers):
- leftovers, word = alt_read_word(leftovers)
- if do_print: print('(word, leftovers) = ("%s", "%s")' % (word, leftovers))
- def test_is_name(a: str, b: list, do_print: bool=False):
- if do_print: print("----------OLD-----------")
- for x in b:
- check = is_name(x, a)
- if do_print: print('target_word "%s" in string, %s' % (x, check))
- def test_alt_is_name(a: str, b: list, do_print: bool=False):
- if do_print: print("----------ALT-----------")
- for x in b:
- check = alt_is_name(x, a)
- if do_print: print('target_word "%s" in string, %s' % (x, check))
- def test_new_is_name(a: str, b: list, do_print: bool=False):
- if do_print: print("----------NEW-----------")
- for x in b:
- check = new_is_name(x, a)
- if do_print: print('target_word "%s" in string, %s' % (x, check))
- test_string = 'This is a string, full of "words that" \'should be handled\' and NOT messed up. Even the empty "" quote-pair. It won\'t fail? "You can\'t touch this!"'
- test_words = ['this', 'messed', 'empty', 'words that', 'not found', 'bugger', ' of', '', "can't", "won't"]
- test_runs = 10000
- test_read_word(test_string, test_words, True)
- test_alt_read_word(test_string, test_words, True)
- test_is_name(test_string, test_words, True)
- test_alt_is_name(test_string, test_words, True)
- test_new_is_name(test_string, test_words, True)
- print("----------OLD-----------")
- print('%d read_word: ' % test_runs, timeit.timeit('test_read_word(test_string, test_words)', setup='from __main__ import test_read_word, test_string, test_words', number=test_runs))
- print('%d is_name: ' % test_runs, timeit.timeit('test_is_name(test_string, test_words)', setup='from __main__ import test_is_name, test_string, test_words', number=test_runs))
- print("----------ALT-----------")
- print('%d read_word: ' % test_runs, timeit.timeit('test_alt_read_word(test_string, test_words)', setup='from __main__ import test_alt_read_word, test_string, test_words', number=test_runs))
- print('%d is_name: ' % test_runs, timeit.timeit('test_alt_is_name(test_string, test_words)', setup='from __main__ import test_alt_is_name, test_string, test_words', number=test_runs))
- print("----------NEW-----------")
- print('%d is_name: ' % test_runs, timeit.timeit('test_new_is_name(test_string, test_words)', setup='from __main__ import test_new_is_name, test_string, test_words', number=test_runs))
Advertisement
Add Comment
Please, Sign In to add comment