Misc cleanup.

pull/430/head
William Ting 8 years ago
parent eafd6ac451
commit cf013c6875

@ -27,20 +27,23 @@ def match_anywhere(needles, haystack, ignore_case=False):
needles = ['foo', 'baz'] needles = ['foo', 'baz']
regex needle = r'.*foo.*baz.*' regex needle = r'.*foo.*baz.*'
haystack = [ haystack = [
(path="/foo/bar/baz", weight=10), (path='/foo/bar/baz', weight=10),
(path="/baz/foo/bar", weight=10), (path='/baz/foo/bar', weight=10),
(path="/foo/baz", weight=10)] (path='/foo/baz', weight=10),
]
result = [ result = [
(path="/moo/foo/baz", weight=10), (path='/moo/foo/baz', weight=10),
(path="/foo/baz", weight=10)] (path='/foo/baz', weight=10),
]
""" """
regex_needle = '.*' + '.*'.join(needles).replace('\\', '\\\\') + '.*' regex_needle = '.*' + '.*'.join(needles).replace('\\', '\\\\') + '.*'
regex_flags = re.IGNORECASE | re.UNICODE if ignore_case else re.UNICODE regex_flags = re.IGNORECASE | re.UNICODE if ignore_case else re.UNICODE
found = lambda haystack: re.search( found = lambda haystack: re.search(
regex_needle, regex_needle,
haystack.path, haystack.path,
flags=regex_flags) flags=regex_flags,
)
return ifilter(found, haystack) return ifilter(found, haystack)
@ -51,10 +54,11 @@ def match_consecutive(needles, haystack, ignore_case=False):
For example: For example:
needles = ['foo', 'baz'] needles = ['foo', 'baz']
haystack = [ haystack = [
(path="/foo/bar/baz", weight=10), (path='/foo/bar/baz', weight=10),
(path="/foo/baz/moo", weight=10), (path='/foo/baz/moo', weight=10),
(path="/moo/foo/baz", weight=10), (path='/moo/foo/baz', weight=10),
(path="/foo/baz", weight=10)] (path='/foo/baz', weight=10),
]
regex_needle = re.compile(r''' regex_needle = re.compile(r'''
foo # needle #1 foo # needle #1
@ -67,8 +71,9 @@ def match_consecutive(needles, haystack, ignore_case=False):
''') ''')
result = [ result = [
(path="/moo/foo/baz", weight=10), (path='/moo/foo/baz', weight=10),
(path="/foo/baz", weight=10)] (path='/foo/baz', weight=10),
]
""" """
# The normal \\ separator needs to be escaped again for use in regex. # The normal \\ separator needs to be escaped again for use in regex.
sep = '\\\\' if is_windows() else os.sep sep = '\\\\' if is_windows() else os.sep
@ -81,7 +86,8 @@ def match_consecutive(needles, haystack, ignore_case=False):
found = lambda entry: re.search( found = lambda entry: re.search(
regex_needle, regex_needle,
entry.path, entry.path,
flags=regex_flags) flags=regex_flags,
)
return ifilter(found, haystack) return ifilter(found, haystack)
@ -93,17 +99,19 @@ def match_fuzzy(needles, haystack, ignore_case=False, threshold=0.6):
For example: For example:
needles = ['foo', 'bar'] needles = ['foo', 'bar']
haystack = [ haystack = [
(path="/foo/bar/baz", weight=11), (path='/foo/bar/baz', weight=11),
(path="/foo/baz/moo", weight=10), (path='/foo/baz/moo', weight=10),
(path="/moo/foo/baz", weight=10), (path='/moo/foo/baz', weight=10),
(path="/foo/baz", weight=10), (path='/foo/baz', weight=10),
(path="/foo/bar", weight=10)] (path='/foo/bar', weight=10),
]
result = [ result = [
(path="/foo/bar/baz", weight=11), (path='/foo/bar/baz', weight=11),
(path="/moo/foo/baz", weight=10), (path='/moo/foo/baz', weight=10),
(path="/foo/baz", weight=10), (path='/foo/baz', weight=10),
(path="/foo/bar", weight=10)] (path='/foo/bar', weight=10),
]
This is a weak heuristic and used as a last resort to find matches. This is a weak heuristic and used as a last resort to find matches.
""" """

Loading…
Cancel
Save