quixadhal

test script for is_name/read_word

Aug 23rd, 2014
322
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.14 KB | None | 0 0
  1. import timeit
  2. import re
  3.  
  4. def read_word(pstr, lower=True):
  5.     if not pstr:
  6.         return "", ""
  7.     pstr = pstr.lstrip()
  8.     locate = len(pstr)
  9.     unmatch = False
  10.     if locate == 0:
  11.         return "", ""
  12.     if pstr[0] == "'":
  13.         locate = pstr.find("'", 1)+1
  14.         if locate == 0:
  15.             locate = len(pstr)
  16.             unmatch = True
  17.     elif pstr[0] == '"':
  18.         locate = pstr.find('"', 1)+1
  19.         if locate == 0:
  20.             locate = len(pstr)
  21.             unmatch = True
  22.     else:
  23.         for i, c in enumerate(pstr):
  24.             if c.isspace():
  25.                 locate = i
  26.                 break
  27.  
  28.     word = pstr[:locate]
  29.     strip = len(word)
  30.     if not word:
  31.         return pstr, word
  32.     if word[0] in ['"', "'"]:
  33.         word = word[1:]
  34.         if not unmatch:
  35.             word = word[:-1]
  36.     if lower:
  37.         word = word.lower()
  38.  
  39.     return pstr.lstrip()[strip:], word.strip()
  40.  
  41.  
  42. def is_name(arg, name):
  43.     name, tmp = read_word(name)
  44.     if not arg:
  45.         return False
  46.     while tmp:
  47.         if tmp.lower().startswith(arg):
  48.             return True
  49.         name, tmp = read_word(name)
  50.     return False
  51.  
  52. def alt_read_word(pstr, lower=True):
  53.     if not pstr:
  54.         return '',''
  55.  
  56.     start = None
  57.     end = None
  58.     i = -1
  59.     for c in pstr:
  60.         i += 1
  61.         if c == "'" and start is None:
  62.             start = i + 1
  63.             quote = pstr.find("'", i+1)
  64.             if quote > -1:
  65.                 end = quote
  66.             else:
  67.                 end = len(pstr)
  68.             if lower:
  69.                 return pstr[end+1:], pstr[start:end].lower()
  70.             else:
  71.                 return pstr[end+1:], pstr[start:end]
  72.             break
  73.         elif c == '"' and start is None:
  74.             start = i + 1
  75.             quote = pstr.find('"', i+1)
  76.             if quote > -1:
  77.                 end = quote
  78.             else:
  79.                 end = len(pstr)
  80.             if lower:
  81.                 return pstr[end+1:], pstr[start:end].lower()
  82.             else:
  83.                 return pstr[end+1:], pstr[start:end]
  84.             break
  85.         elif c.isspace():
  86.             if start is not None:
  87.                 end = i
  88.                 break
  89.         else:
  90.             if start is None:
  91.                 start = i
  92.  
  93.     if not end:
  94.         end = len(pstr)
  95.     if lower:
  96.         return pstr[end:], pstr[start:end].lower()
  97.     else:
  98.         return pstr[end:], pstr[start:end]
  99.  
  100.  
  101.  
  102. def alt_is_name(arg, name):
  103.     if not arg or not name:
  104.         return False
  105.     name, tmp = alt_read_word(name)
  106.     while tmp:
  107.         if tmp.startswith(arg):
  108.             return True
  109.         name, tmp = alt_read_word(name)
  110.     return False
  111.  
  112.  
  113. _breakup = re.compile('(\".*?\"|\'.*?\'|[^\s]+)')
  114.  
  115. def new_is_name(arg, name):
  116.     if not arg or not name:
  117.         return False
  118.     arg = arg.lower()
  119.     words = _breakup.findall(name)
  120.     for word in words:
  121.         if word[0] in ('"', "'"):
  122.             word = word[1:-1]
  123.         if word.lower().startswith(arg):
  124.             return True
  125.     return False
  126.  
  127.  
  128. def test_read_word(a: str, b: list, do_print: bool=False):
  129.     if do_print: print("----------OLD-----------")
  130.     if do_print: print('Start: %s' % a)
  131.  
  132.     leftovers, word = read_word(a)
  133.     if do_print: print('(word, leftovers) = ("%s", "%s")' % (word, leftovers))
  134.  
  135.     while(leftovers):
  136.         leftovers, word = read_word(leftovers)
  137.         if do_print: print('(word, leftovers) = ("%s", "%s")' % (word, leftovers))
  138.  
  139.  
  140. def test_alt_read_word(a: str, b: list, do_print: bool=False):
  141.     if do_print: print("----------ALT-----------")
  142.     if do_print: print('Start: %s' % a)
  143.  
  144.     leftovers, word = alt_read_word(a)
  145.     if do_print: print('(word, leftovers) = ("%s", "%s")' % (word, leftovers))
  146.  
  147.     while(leftovers):
  148.         leftovers, word = alt_read_word(leftovers)
  149.         if do_print: print('(word, leftovers) = ("%s", "%s")' % (word, leftovers))
  150.  
  151. def test_is_name(a: str, b: list, do_print: bool=False):
  152.     if do_print: print("----------OLD-----------")
  153.     for x in b:
  154.         check = is_name(x, a)
  155.         if do_print: print('target_word "%s" in string, %s' % (x, check))
  156.  
  157. def test_alt_is_name(a: str, b: list, do_print: bool=False):
  158.     if do_print: print("----------ALT-----------")
  159.     for x in b:
  160.         check = alt_is_name(x, a)
  161.         if do_print: print('target_word "%s" in string, %s' % (x, check))
  162.  
  163. def test_new_is_name(a: str, b: list, do_print: bool=False):
  164.     if do_print: print("----------NEW-----------")
  165.     for x in b:
  166.         check = new_is_name(x, a)
  167.         if do_print: print('target_word "%s" in string, %s' % (x, check))
  168.  
  169.  
  170.  
  171. 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!"'
  172. test_words = ['this', 'messed', 'empty', 'words that', 'not found', 'bugger', ' of', '', "can't", "won't"]
  173. test_runs = 10000
  174.  
  175. test_read_word(test_string, test_words, True)
  176. test_alt_read_word(test_string, test_words, True)
  177.  
  178. test_is_name(test_string, test_words, True)
  179. test_alt_is_name(test_string, test_words, True)
  180.  
  181. test_new_is_name(test_string, test_words, True)
  182.  
  183. print("----------OLD-----------")
  184. 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))
  185. 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))
  186. print("----------ALT-----------")
  187. 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))
  188. 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))
  189. print("----------NEW-----------")
  190. 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