Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 28 additions & 21 deletions stackit/stackit_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,24 +39,31 @@ def focusQuestion(questions, count):
userInput = '0'
#Looping while the user wants to see more input
while(userInput != 'm'):
userInput = promptUser("Press m for more, or a number to select: ")
if(userInput == 'm'):
break
if(0 < int(userInput) and int(userInput) <= count):
print("\n\n\n\n\n\n")
printFullQuestion(questions[int(userInput)- 1])
branchInput = '0' #user deciding whether to branch to open browser, or to return to search
while(branchInput != 'x'):
branchInput = promptUser("Press b to launch browser, or x to return to search: ")
if(branchInput == 'b'):
webbrowser.open(questions[int(userInput)-1].json['link'], new=0, autoraise=True)
#User selects x to return to search
if(branchInput == 'x'):
print("\n\n\n\n\n\n\n\n\n\n\n\n")
#Ranging over the 5 questions including the user's choice
for j in range(5*int((int(userInput)-1)/5), 5*int((int(userInput)-1)/5)+5):
printQuestion(questions[j], j+1)
continue #exit the inner while loop
userInput = promptUser("Enter m for more, a question number to select, or q to exit: ")
try:
if(userInput == 'q'):
sys.exit()
if(userInput == 'm'):
break
if(0 < int(userInput) and int(userInput) <= count):
print("\n\n\n\n\n\n")
printFullQuestion(questions[int(userInput)- 1])
branchInput = '0' #user deciding whether to branch to open browser, or to return to search
while(branchInput != 'x'):
branchInput = promptUser("Enter b to launch browser, or x to return to search: ")
if(branchInput == 'b'):
webbrowser.open(questions[int(userInput)-1].json['link'], new=0, autoraise=True)
#User selects x to return to search
if(branchInput == 'x'):
print("\n\n\n\n\n\n\n\n\n\n\n\n")
#Ranging over the 5 questions including the user's choice
for j in range(5*int((int(userInput)-1)/5), 5*int((int(userInput)-1)/5)+5):
printQuestion(questions[j], j+1)
continue #exit the inner while loop
except:
if (userInput != 'q'):
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The userInput == 'q' case is handled above, in line 44––is there a reason for this redundancy?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When you throw the exception after the check, it automatically propagates to the except: block. So when letting the user know what happened, you need to check if the exception occured because of q, if it didn't then let the user know why. If it did then just exit as normal.

print("The input entered was not recognized as a valid choice. Exiting...")
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Though there's a possibility that this try/except structure could throw errors besides having an invalid character, that seems unlikely.

Why is this "invalid input" warning not printed every time an error is thrown in this block?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is printed everytime, only not printed when the choice q is entered...

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Gotcha––that makes sense. I wasn't thinking of sys.exit() as throwing an exception, but this is definitely cleared up.

sys.exit()
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would rather this point returned to the initial prompt––"Enter m for more, a question number to select, or q to exit: "––instead of sys.exit()-ing. Allows for user mistakes. There is a manual sys.exit() option in the input 'q' now, as well as control-C.

tl;dr: would rather not quit without explicit user request.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So would you rather it be continue then sys.exit()?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I'd rather it was continue than sys.exit() in the case that
some invalid character is entered. e.g.:

except:
    if (userInput != 'q'):
        print("The input entered was not recognizes as a valid choice.")
        continue
    else: # If the user input is 'q', no warning is necessary
        sys.exit()

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it, added in latest commit, works for both userInput and branchInput. It does make more sense. So now it prompts the user to re-enter, while also letting them know it was invalid input.
👍

Enter b to launch browser, x to return to search, or q to exit: s
The input entered was not recognized as a valid choice.
Enter b to launch browser, x to return to search, or q to exit:


def searchTerm(term, tags):
print('Searching for: %s... \n' % term,)
Expand Down Expand Up @@ -124,7 +131,7 @@ def printFullQuestion(question):
answertext = h.handle(answerdiv.find('div', attrs={'class': 'post-text'}).prettify())
for cell in soup.find_all('td', attrs={'class': 'postcell'}):
questiontext = h.handle(cell.find('div', attrs={'class': 'post-text'}).prettify())
print(pColor.BLUE + "-------------------------QUESTION------------------------\n" + question.title + "\n" + questiontext
print(pColor.BLUE + "-------------------------QUESTION------------------------\n" + question.title + "\n" + questiontext
+ pColor.END + "\n\n-------------------------------ANSWER------------------------------------\n" + answertext)

def searchVerbose(term):
Expand All @@ -133,11 +140,11 @@ def searchVerbose(term):
questionurl = question.json['link']
answerid = question.json['accepted_answer_id']
printFullQuestion(question)


def getParser():
parser = argparse.ArgumentParser(description="Parses command-line arguments for StackIt")
parser.add_argument("-s", "--search", metavar="QUERY", help="Searches StackOverflow for your query")
parser.add_argument("-s", "--search", metavar="QUERY", help="Searches StackOverflow for your query")
parser.add_argument("-e", "--stderr", metavar="EXECUTE", help="Runs an executable command (i.e. python script.py) and automatically inputs error message to StackOverflow")
parser.add_argument("-t", "--tag", metavar="TAG1 TAG2", help="Searches StackOverflow for your tags")
parser.add_argument("--verbose", help="displays full text of most relevant question and answer", action="store_true")
Expand Down