New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add a simple order strategy for result of search command #4131
Changes from 2 commits
18adff6
b3ba31c
a8a84d2
8828252
cd47418
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,10 @@ | ||
|
||
from __future__ import absolute_import | ||
|
||
import logging | ||
import sys | ||
import textwrap | ||
import re | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Global imports should be alphabetically ordered There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Got it. Modified. |
||
|
||
from pip.basecommand import Command, SUCCESS | ||
from pip.compat import OrderedDict | ||
|
@@ -44,6 +46,7 @@ def run(self, options, args): | |
query = args | ||
pypi_hits = self.search(query, options) | ||
hits = transform_hits(pypi_hits) | ||
hits = sort_hits(query, hits) | ||
|
||
terminal_width = None | ||
if sys.stdout.isatty(): | ||
|
@@ -131,3 +134,39 @@ def print_results(hits, name_column_width=None, terminal_width=None): | |
|
||
def highest_version(versions): | ||
return max(versions, key=parse_version) | ||
|
||
def sort_hits(query, hits): | ||
if len(query) != 1: | ||
# Now only handle the easiest condition, | ||
# which is possibly the most frequent condition | ||
return hits | ||
query = query[0] | ||
p = re.compile(query) | ||
p_ignorecase = re.compile(query, re.IGNORECASE) | ||
for index, item in enumerate(hits): | ||
hits[index]['match_score'] = 0 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. for any use of the enumerate is also unnecessary then There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. sorry, the habbit took from php :-) |
||
name = item['name'] | ||
summary = item['summary'] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. just use |
||
if summary is None: | ||
summary = '' | ||
m1 = p.match(name) | ||
m2 = p_ignorecase.match(name) | ||
m3 = p_ignorecase.findall(name) | ||
m4 = p_ignorecase.findall(summary) | ||
|
||
# Ordering strategy | ||
if name == query: | ||
hits[index]['match_score'] += 10000 | ||
elif name.lower() == query.lower(): | ||
hits[index]['match_score'] += 9000 | ||
if m1 is not None: | ||
hits[index]['match_score'] += 1000 | ||
elif m2 is not None: | ||
hits[index]['match_score'] += 900 | ||
elif m3 is not None: | ||
hits[index]['match_score'] += 800 | ||
if m4 is not None: | ||
hits[index]['match_score'] += len(m3) | ||
|
||
hits.sort(key = lambda i: i['match_score'], reverse=True) | ||
return hits |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unnecessary.