From 96f3c38aa99d271d42254e43362ec29a4720c483 Mon Sep 17 00:00:00 2001 From: "Minghao Li (FA Talent)" Date: Fri, 12 Oct 2018 17:18:21 +0800 Subject: [PATCH] support more than one answer code detection --- README.rst | 11 +++++++++++ main.py | 12 ++---------- stackoverflow/__init__.py | 19 ++++++++++++------- 3 files changed, 25 insertions(+), 17 deletions(-) diff --git a/README.rst b/README.rst index 16a7e43..17527a1 100644 --- a/README.rst +++ b/README.rst @@ -1,6 +1,17 @@ StackOverflow Importer ====================== +Update to take the code of at most 10 answers into consider + +.. code:: python + + >>> from stackoverflow import quick_sort + + >>> print(quick_sort.quickSort([1, 3, 2, 5, 4])) + [1, 2, 3, 4, 5] + +====================== + Do you ever feel like all you’re doing is copy/pasting from Stack Overflow? diff --git a/main.py b/main.py index 826071a..51b8706 100644 --- a/main.py +++ b/main.py @@ -1,10 +1,2 @@ -from stackoverflow import quick_sort, split_into_chunks -print(quick_sort.sort([1, 3, 2, 5, 4])) -print(list(split_into_chunks.chunk("very good chunk func"))) -print("gotta take a break") -from time import time -t1 = time() -from stackoverflow import time_delay -print("that's enough, let's continue", time()-t1) -print("I wonder who made split_into_chunks", split_into_chunks.__author__) -print("but what's the license? Can I really use this?", quick_sort.__license__) +from stackoverflow import quick_sort +print(quick_sort.quickSort([1, 3, 2, 5, 4])) diff --git a/stackoverflow/__init__.py b/stackoverflow/__init__.py index 73500cb..96656aa 100644 --- a/stackoverflow/__init__.py +++ b/stackoverflow/__init__.py @@ -65,16 +65,21 @@ def _fetch_url(cls, query): }).json() if not ans["items"]: raise ImportError("Couldn't find any question matching `" + query + "`") - return ans["items"][0]["link"] + # return ans["items"][0]["link"] + return ans["items"] @classmethod - def _fetch_code(cls, url): - q = requests.get(url) - return cls._find_code_in_html(q.text) + def _fetch_code(cls, anses): + ans_html = [] + for ans in anses[:10]: + ans_html.append(requests.get(ans["link"])) + return cls._find_code_in_html(ans_html) @staticmethod - def _find_code_in_html(s): - answers = re.findall(r'
share<")] author = author[author.rfind('