Skip to content

Commit d77cc73

Browse files
committed
wip docs: add quizbowl trainer CLI example
1 parent 12ecc72 commit d77cc73

File tree

1 file changed

+75
-2
lines changed

1 file changed

+75
-2
lines changed

examples/sync.md

Lines changed: 75 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ for tossup in sync_client.random_tossup(number=3, categories=["Science"]):
2828
print(tossup)
2929
```
3030

31-
However, you are likely to get an error if you do not capitalize properly or make a spelling mistake. Instead, if you have some sort of LSP installed (or all-in-one extensions like VSCode Python), then figuring out what you want will be a lot easier through importing the QBReader module's `types`.
31+
However, you are likely to get an error if you do not capitalize properly or make a spelling mistake. Instead, if you have some sort of language server installed (or all-in-one extensions like VSCode Python), then figuring out what you want will be a lot easier through importing the QBReader module's `types`.
3232
```py
3333
from qbreader import types
3434

@@ -38,7 +38,7 @@ Category = types.Category
3838
for tossup in sync_client.random_tossup(number=3, categories=[Category.SCIENCE]):
3939
print(tossup)
4040
```
41-
This way, while typing (:smile:), your LSP should give an autocomplete on this. This is usually a better strategy than trying to guess the string that is right.
41+
This way, while typing (:smile:), your language server should give an autocomplete on this. This is usually a better strategy than trying to guess the string that is right.
4242

4343
## Tossup by Difficulty
4444

@@ -64,3 +64,76 @@ for bonus in sync_client.random_bonus(three_part_bonuses=True):
6464
for bonus in sync_client.random_bonus(number=3, difficulties=[4, 5], min_year=2010, max_year=2020, categories=[Category.FINE_ARTS] three_part_bonuses=True):
6565
print(bonus)
6666
```
67+
68+
# Examples
69+
70+
## Simple quizbowl trainer CLI
71+
72+
Here is a working example of a simple quizbowl trainer CLI.
73+
74+
```py
75+
from qbreader import Sync
76+
import time
77+
78+
# Set up synchronous client
79+
sync_client = Sync()
80+
81+
# Set up session configuration
82+
num_correct = 0
83+
num_tossups = int(input("How many tossups do you want to answer? "))
84+
85+
# Clear the screen
86+
print("\033c")
87+
88+
# Get tossups
89+
tossups = sync_client.random_tossup(number=num_tossups)
90+
91+
# Read each tossup
92+
for i, tossup in enumerate(tossups):
93+
94+
# Print title and set up ANSI color
95+
print(f"Tossup {i+1}:",
96+
end="\033[94m\n")
97+
98+
# Print word by word
99+
for word in tossup.question_sanitized.split():
100+
# Print word, flush to see tossup after `time.sleep`
101+
print(f"{word} ", end="", flush=True)
102+
103+
# Delay for each word in seconds
104+
time.sleep(0.1)
105+
106+
# Reset color, go to next line
107+
print("\033[0m\n")
108+
109+
# Get user input
110+
response = input("Answer: ", end="\033[90m\n")
111+
112+
# Check if answer is correct
113+
if sync_client.check_answer(tossup.answer, response):
114+
115+
num_correct += 1
116+
117+
# Green color for correct answer
118+
print("\033[92m", "Correct!", sep="")
119+
120+
# Reset color, go to next line
121+
print("\033[0m\n")
122+
123+
else:
124+
125+
# Red color for incorrect answer
126+
print("\033[91m",
127+
f"Incorrect. Correct Answer: {tossup.answer_sanitized}", sep="")
128+
129+
# Reset color, go to next line
130+
print("\033[0m\n")
131+
132+
# Clear the screen, end of program
133+
print("\033c")
134+
print("All done!")
135+
136+
# Print session summary
137+
accuracy = num_correct / num_tossups * 100
138+
print(f"Accuracy: {accuracy:.2f}%")
139+
```

0 commit comments

Comments
 (0)