def get_stackoverflow(query): """ Prints the results of interrogating SO with a search query (str). """ # get link to top question questionlink, search_url = _get_link_so_top_answer(query) if questionlink is None: warn( "Failed to find anything on stack overflow, sorry.", "While parsing the URL with the top SO answer, could not detect any answer. Nothing to report", ) return _parse_so_top_answer(questionlink) out = Text.from_markup(f""" [{white}]Link to top [{lightsalmon}]Stack Overflow[/{lightsalmon}] question for the error: [{ls}]{questionlink}[/{ls}] [{white}]To find more related answers on [{lightsalmon}]Stack Overflow[/{lightsalmon}], visit: [{ls}]{search_url}[/{ls}] """) console.print(out)
def ask(query): """ Got a question? Google it! Looks on google for a hit from stack overflow matching a search query and prints it out nicely formatted """ if not isinstance(query, str): raise ValueError( f"Search query must be a string, not {_class_name(query)}") answer = get_google(query, n_answers=10, best_so=True, mute=True, break_on_best=True) if answer is not None: _parse_so_top_answer(answer) console.print( f"\nTo continue reading about this question, head to: [{lightblue}]{answer}\n" ) else: warn( "Failed to get a SO", f"Looked on google for {query}, but didn't find a Stack Overflow answer, sorry.", )
def _parse_so_top_answer(url): """ Parses a link to a SO question to return the formatted text of the question and top answer """ # get content res = requests.get(url) if not res.ok: return None # get question and answer console.print("[white]Parsing SO answer...") bs = BeautifulSoup(res.content, features="html.parser") question = bs.find("div", attrs={"class": "question"}) answer = bs.find("div", attrs={"class": "answer"}) if answer is None or question is None: warn( "Failed to parse SO answer", f"We tried to parse the SO question but failed...", ) return # Print as nicely formatted panels panels = [] for name, obj, color in zip(["Question", "Answer"], [question, answer], [lightsalmon, lightblue]): panels.append(_style_so_element(obj, name, color)) if panels: console.print( f"[{mocassin}]\n\nAnswer to the top [i]Stack Overflow[/i] answer for your question.", Columns( panels, equal=True, width=88, ), sep="\n", ) else: warn( "Failed to find answer on the SO page", "While parsing the URL with the top SO answer, could not detect any answer. Nothing to report", )
def get_answers(hide_panel=False): """ Looks for solution to the last error encountered (as cached). Prints the error message, it's meaning and links to possible answers on google and stack overflow. It also parses the question and first answer from the top hit from google if that's a link to a SO page. :param hide_panel: bool, False. If true the panel with the error message is hidden """ try: query, msg = load_cached() except ValueError: warn( "Failed to load cached error.", "This could be because no error had been cached, or it could be a bug", ) # show a panel with a recap of the error message if not hide_panel: out = f""" [bold {white}]Searching online for solutions to: [bold {white}]>[/bold {white}] [bold {salmon}]{query}", [bold {white}]>[/bold {white}] [bold {white}]>[/bold {white}] [{salmon}]{query.split(":")[0]}:[/{salmon}][{lightgray}] {msg}', """ panel = Panel.fit( Text.from_markup(out), padding=(1, 2), border_style=salmon, ) console.print(panel) else: console.print("\n") # ask google and stack overflow best_answer = get_google(query) # get_stackoverflow(query) if "stackoverflow" in best_answer: _parse_so_top_answer(best_answer)
def whats_pi(): """ Prints a Report with an overview of `pyinspect`. """ # ? Intro rep = Report(f"Pynspect", dim=orange, accent=orange) rep._type = "Pyinspect info" rep.width = 100 rep.add( f"[b {lightorange}]The python package for lazy programmers", justify="center", ) # Features summary rep.add(f""" [{salmon}]Don't remember a function's name?[/{salmon}] Use `pyinspect` to look for it. [{salmon}]Don't remember what a function does?[/{salmon}] Use `pyinspect` to print its source code directly to your terminal. [{salmon}]Can't figure out why you keep getting an error?[/{salmon}] Use `pyinspect`'s fancy tracebacks to figure it out [{salmon}]Still can't figure it out, but too lazy to google it?[/{salmon}] Use `pyinspect` to print Stack Overflow's top answer for your error message directly to your terminal! """) # Package / Repo info as a nested panel info = NestedPanel(color=mocassin, dim=mocassin) _info = dict( Author=__author__, License=__license__, Version=__version__, Website=__website__, ) if Github is not None: n_stars = Github().get_repo("FedeClaudi/pyinspect").stargazers_count _info["Github stars"] = n_stars else: warn( "Could not fetch repo info", "Perhaps `PyGithub` is not installed?s", ) for k, v in _info.items(): info.add(f"[b {gray}]{k}[/b {gray}]: [{orange}]{v}", justify="right") rep.add(info, "rich") # Features examples rep.add("""## Features""", "markdown", style=lightsalmon) features = { "Look up local variables": "pinspect.what()", "Search functions by name": "pinspect.search(package, function_name)", "Print source code to console": "pinspect.showme(function)", "Enhanced tracebacks": "pinspect.install_traceback()", "Render [i]Stack Overflow[/i] answers in the terminal": 'pinspect.ask("How to python?")', } for txt, code in features.items(): rep.spacer() rep.add(f"[{gray}]" + txt, justify="center") rep.add(" " + code, "code") rep.spacer() rep.add(f"[{lightorange}]... and a bunch of others!") rep.spacer(2) rep.add(f"[{lightsalmon}]Get in touch at:[/{lightsalmon}] {__website__}") console.print(rep)