From 46100d63fa462aad2732d01d9621fe799700c453 Mon Sep 17 00:00:00 2001 From: Trygve Laugstøl Date: Mon, 12 Aug 2019 12:54:49 +0200 Subject: digikey-search-parts: Sorting result table by SPN so the output is consistent. --- src/ee/digikey/__init__.py | 7 +++---- src/ee/digikey/search_parts.py | 3 ++- test/test_digikey.py | 2 +- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/ee/digikey/__init__.py b/src/ee/digikey/__init__.py index 3eb723e..15eb5e6 100644 --- a/src/ee/digikey/__init__.py +++ b/src/ee/digikey/__init__.py @@ -283,14 +283,13 @@ class DigikeyClient(object): self.cache = ee._utils.maybe_cache(cache_dir) self._driver: Optional[webdriver.Chrome] = None - def search(self, query: str, page_size=10) -> str: - return self.product_search(query, page_size) + def search(self, query: str) -> str: + return self.product_search(query) def _find_first_visible(self, xpath): return next((e for e in self.driver.find_elements_by_xpath(xpath) if e.is_displayed()), None) - def product_search(self, query: str, page_size=10) -> str: - params = {'lang': 'en', 'site': 'us', 'keywords': query, 'pageSize': str(page_size)} + def product_search(self, query: str) -> str: cache_key = urllib.parse.quote(query) cached = self.cache.lookup(cache_key) diff --git a/src/ee/digikey/search_parts.py b/src/ee/digikey/search_parts.py index e79959a..a1236bb 100644 --- a/src/ee/digikey/search_parts.py +++ b/src/ee/digikey/search_parts.py @@ -94,11 +94,12 @@ class QueryEngine(object): out_part = resolved(self.store.url, response.products[0]) result = "found" elif response.response_type == SearchResponseTypes.MANY: + # TODO: order by spn/mpn so the output is consistent between runs. print("Got many responses:\n", file=self.log) from ee._utils import gen_rst_table - data = [[p.part_number, p.mpn] for p in response.products] + data = [[p.part_number, p.mpn] for p in sorted(response.products, key=lambda x: x.part_number)] print(gen_rst_table(["DK", "MPN"], data), file=self.log) # find those with an exact match. Digikey uses a "contains" search so a query for "FOO" will return "FOO", diff --git a/test/test_digikey.py b/test/test_digikey.py index 4961a9d..cc7dfc7 100644 --- a/test/test_digikey.py +++ b/test/test_digikey.py @@ -48,7 +48,7 @@ def test_digikey_1(tmpdir): @pytest.mark.digikey def test_digikey_2(): - content = client.product_search("TCR2LF", page_size=500) + content = client.product_search("TCR2LF") res = parser.parse_string(client.baseurl, content) assert res.response_type == dk.SearchResponseTypes.MANY -- cgit v1.2.3