[Edu-sig] more pythonic andragogy...
kirby urner
kirby.urner at gmail.com
Fri Jul 29 01:39:38 EDT 2016
What I like about the appended module is it exercises constructs in a
somewhat meaningless fashion that nevertheless has a story, a strong
metaphor. We're in a spooky castle and want to leave having guessed a
Python keyword, hidden in self.__xyjk during initialization.
If we guess it right in five tries, an exception is raised, but one
signalling we're victorious, and so the __exit__ method handles it in a
congratulatory manner and returns True, and we're released from the castle
without fuss or bother.
If we can't guess it in five tries, even after two hints, then the
exception is a kick in the pants; we're booted out with an unhandled
exception (return False).
The outer handler takes care of KickFromCastle in that case. The print()
statements tell the story.
All pretty pointless... except to those learning Python.
Tonight I interleaved the code below with a more serious-looking use of
context managers in a Jupyter Notebook.
http://bit.ly/2aiFaIA <--- looks through nbviewer (Jupyter Notebook
viewer) at one of my Github JNs (just another web page)
Kirby
==
from keyword import kwlist
from random import choice
import sys
class EscapeVictorious(Exception):
pass
class KickFromCastle(Exception):
pass
class Castle:
def __init__(self, secret):
self.__xyjk = secret
self.guesses = 0
self.hints = 0
def __enter__(self):
"""get the ball rolling"""
print("Welcome to Spooky Castle. To Escape, "
"guess the secret keyword")
return self # <-- make me available within scope via 'as'
def hint(self):
if self.hints == 0:
print("The keyword begins with", self.__xyjk[0])
elif self.hints == 1:
print("The keyword is", len(self.__xyjk), "letters long.")
else:
print("You've had your two hints, sorry")
self.hints += 1
def query(self):
"""gradations"""
print("So what is the secret keyword then? "
" Guess so far:", self.guesses)
ans = input("You may answer (or type 'hint'): ")
if ans == self.__xyjk:
print("Excellent, we're done here")
print("You have won the Copper Key") # <-- RealPlayer One
(novel)
raise EscapeVictorious("Copper Key")
elif self.guesses == 5:
print("Uh oh, you're out of guesses, sigh")
raise KickFromCastle("We're done!")
elif ans == "hint":
self.hint()
return
else:
self.guesses += 1
print("No, that's not it.")
def __exit__(self, *exception_data):
"""raise a ruckus"""
if exception_data[0] == EscapeVictorious:
print("Congratulations!")
print("Here is your", exception_data[1])
return True
if exception_data[0] == KickFromCastle:
print("Better Luck Next Time")
print("The keyword was", self.__xyjk)
return False
if __name__ == "__main__":
the_secret = choice(kwlist)
try:
with Castle(the_secret) as spooky:
while True:
spooky.query()
except:
print("Handling: ", sys.exc_info()[0].__name__)
print(sys.exc_info()[1]) # <--- triggers __str__
print(type(sys.exc_info()[1]))
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/edu-sig/attachments/20160728/d1aef0b7/attachment.html>
More information about the Edu-sig
mailing list