Skip to content

Instantly share code, notes, and snippets.

@philippkeller
Created July 28, 2016 13:04
Show Gist options
  • Save philippkeller/3abd95a6bd23065bbf4675b939b8d539 to your computer and use it in GitHub Desktop.
Save philippkeller/3abd95a6bd23065bbf4675b939b8d539 to your computer and use it in GitHub Desktop.
base code of stage 1 and 2 of sysengquiz3
import urwid, random, re, time
blank = urwid.Divider()
palette = [
('body','light gray','black', 'standout'),
('reverse','light gray','black'),
('header','white','dark red', 'bold'),
('important','dark red','black',('standout','underline')),
('editfc','white', 'dark blue', 'bold'),
('editbx','light gray', 'dark blue'),
('editcp','black','light gray', 'standout'),
('bright','dark gray','light gray', ('bold','standout')),
('buttn','white','black'),
('buttnf','dark red','dark gray','bold'),
]
def stage1():
text_header = "hello hello.."
listbox_content = [
blank,
urwid.Padding(urwid.Text(text_header), left=2, right=2, min_width=20),
blank,
]
status = urwid.Text("")
questions = [
("Question 1",
["answer 1",
"answer 2",
"answer 3",
"answer 4",]),
("Question 2",
["answer 1",
"answer 2",
"answer 3",
"answer 4",]),
("Question 3",
["answer 1",
"answer 2",
"answer 3",
"answer 4",]),
("Question 4",
["answer 1",
"answer 2",
"answer 3",
"answer 4",]),
]
solution = 0
for i,j in [(0,3),(1,1),(2,2),(3,2),(3,3)]:
solution += hash(questions[i][1][j])
def handler(checkbox, new_state, index):
fails1 = ["keep trying",
"no, still not correct",
"there are still errors in there",]
fails2 = [
"hint: try ctrl-d",
"at least you know how to navigate around",
]
handler.checked[hash(checkbox.get_label())] = not checkbox.get_state()
handler.seen_indexes.add(index)
if len(handler.seen_indexes) != len(questions):
return
if sum([k for k,v in handler.checked.items() if v == True]) == solution:
status.set_text("yay!")
time.sleep(0.5)
raise urwid.ExitMainLoop()
handler.fails += 1
if handler.fails < 9:
status.set_text(random.choice(fails1))
else:
status.set_text(random.choice(fails2))
handler.fails = 0
handler.checked = dict()
handler.seen_indexes = set()
index = 1
for left_right in [questions[i:i+2] for i in range(0, len(questions), 2)]:
if len(left_right) == 2:
left,right = left_right
else:
left,right = left_right,None
cols = []
for question, answers in [left, right]:
cols.append(urwid.Padding(
urwid.Pile([urwid.Text(('important', question)), blank] +
[urwid.AttrWrap(urwid.CheckBox(txt, on_state_change=handler, user_data=index),'buttn','buttnf') for txt in answers]),
left=4, right=3, min_width=7))
index += 1
listbox_content += [urwid.AttrWrap(urwid.Columns(cols), 'body'), blank]
listbox_content.append(urwid.Padding(status, left=2, right=2, min_width=20))
listbox = urwid.ListBox(urwid.SimpleListWalker(listbox_content))
frame = urwid.Frame(urwid.AttrWrap(listbox, 'body'))
urwid.MainLoop(frame, palette).run()
def stage2():
text_header = "hello hello..."
q1 = "Question 1"
q2 = "Question 3"
q3 = "Question 3"
q4 = "Question 4"
(e1,e2,e3,e4) = (urwid.Edit("", ""),urwid.Edit("", ""),urwid.Edit("", ""),urwid.Edit("", ""))
fields = ["","","",""]
status = urwid.Text("")
def input_change(widget, text):
text = text.strip().lower()
text = "".join(re.findall("([a-z0-9]+)", text))
if widget == e1:
fields[0] = text
elif widget == e2:
fields[1] = text
elif widget == e3:
fields[2] = text
elif widget == e4:
fields[3] = text
if hash("".join(fields)) == 9026828681774512230:
raise urwid.ExitMainLoop()
# status.set_text(str(hash("".join(fields))))
listbox_content = [
blank,
urwid.Padding(urwid.Text(text_header), left=2, right=2, min_width=20),
blank,
urwid.AttrWrap(urwid.Columns([
urwid.Pile([
urwid.Padding(
urwid.Pile([urwid.Text((q2)),blank,urwid.AttrWrap(e2,'editbx', 'editfc'),]),
left=4, right=3, min_width=7),
urwid.Divider(),
urwid.Padding(
urwid.Pile([urwid.Text((q1)),blank,urwid.AttrWrap(e1,'editbx', 'editfc'),]),
left=4, right=3, min_width=7),
]),
urwid.Pile([
urwid.Padding(
urwid.Pile([urwid.Text((q3)),blank,urwid.AttrWrap(e3,'editbx', 'editfc'),]),
left=4, right=3, min_width=7),
urwid.Divider(),
urwid.Padding(
urwid.Pile([urwid.Text((q4)),blank,urwid.AttrWrap(e4,'editbx', 'editfc'),]),
left=4, right=3, min_width=7),
]),
]), 'body'),
urwid.Divider(),
urwid.Padding(status, left=2, right=2, min_width=20)
]
listbox = urwid.ListBox(urwid.SimpleListWalker(listbox_content))
frame = urwid.Frame(urwid.AttrWrap(listbox, 'body'))
for i in [e1,e2,e3,e4]:
urwid.connect_signal(i, 'change', input_change)
urwid.MainLoop(frame, palette).run()
if __name__ == '__main__':
try:
stage1()
stage2()
except KeyboardInterrupt:
pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment