siznax / wptools

Wikipedia tools (for Humans): easily extract data from Wikipedia, Wikidata, and other MediaWikis
MIT License
574 stars 79 forks source link

do not print results from get_parse to stderr #153

Open boompig opened 4 years ago

boompig commented 4 years ago

Currently get_parse always outputs results to stderr. Is there a way to run this function silently? This is especially annoying when crawling or running in Jupyter notebook.

siznax commented 4 years ago

Yes, try page = wptools.page(silent=True), as mentioned in our Usage wiki page.

boompig commented 4 years ago

thanks for the quick response. consider making this the default?

siznax commented 4 years ago

Hmm. That might be a good idea. Anyone else have a preference? (thinking about it)

TsUNaMyWaVe commented 3 years ago

While on the subject, is there a way for errors not to be printed as well, while using "silent=True"? I'm searching for pages by name, and sometimes they don't exist. I handle it, but the error of not finding the page is still showing up.

siznax commented 3 years ago

Yes @TsUNaMyWaVe, try catching the exception raised, probably LookupError. Something like this?

try:
    <your code>
except LookupError:
    pass
TsUNaMyWaVe commented 3 years ago

@siznax thanks! I actually found a little bit cleaner solution:

with contextlib.redirect_stderr(io.StringIO()):
                <your code>

But I think adding this option as part of the tool could be a good improvement as well. By either adding it to the "silent" argument, or adding an additional argument for error printing.

siznax commented 3 years ago

@TsUNaMyWaVe thanks for the suggestion. In the case of an error, redirecting to stderr seems appropriate. Still trying to decide if "silent by default" is best. Any opinion on that?