wimglenn / advent-of-code-data

Get your Advent of Code data with a single import statement
MIT License
515 stars 54 forks source link

Get response from submission #101

Closed czlucius closed 1 year ago

czlucius commented 1 year ago

Currently, after something is submitted, the response is printed onto the terminal. However, in scripting, getting this response is not convenient, and the submit function returns None. Is there any way AoCD can be modified to return the response, so it can be used in scripting, etc.? Thanks

wimglenn commented 1 year ago

Hmm, the response from server is already returned, and None is returned if it was not necessary to POST to server. The terminal print can be disabled just by passing quiet=True. So I'm not sure what else is really required here, it already can be used as is can't it? Can you give an example of what sort of scripting you mean?

czlucius commented 1 year ago

I was referring to the conditions where the submission is incorrect after submitting the same answer again, and also checking guesses against existing sucessful submissions. In these cases, the caller of the function may not have any way to know about these since the return type is None

But looking at the code, I see that since response is not a string, it will also create some confusion/errors in the caller parsing the result, as it will return different types at different times. Maybe you have a solution to this?

I think I can get the output through

import io
from contextlib import redirect_stdout

f = io.StringIO()
with redirect_stdout(f):
    # Submit answer here...
out = f.getvalue()

Thanks

czlucius commented 1 year ago

Also, with the OOP style interfaces, there is no way to get the response from server and one must use the older method to do so.

wimglenn commented 1 year ago

Can't change the return value, at least not until aocd v2.0.0 when I'm intending to make a bunch of other backward incompatible changes (drop py2 support for example). A possibility may be to add a new argument like

def submit(..., f=None):
    if f is None:
        f = sys.stdout
    ...
    print(msg, file=f)

The caller could pass in a StringIO instance or similar. It's probably not much better than doing the same thing with redirect_stdout tbh.

For the answer setter interface, well, there is no way to return a value from a property setter. We could save the submit result as an attribute:

>>> puzzle.answer_a = 123
>>> # That's the right answer blah blah
>>> puzzle.submit_result
"That's the right answer blah blah"

What sort of usage would you like?

czlucius commented 1 year ago

I agree that passing in the argument(StringIO) is not much better than wrapping with redirect_stdout, perhaps there could be a parameter that accepts a callback which the function will then call with the output? The setter interface (submit_result) looks fine.