次のコードでは、デコレータを使用してヒント ロジックとコマンド ロジックを分離しています。すべてのコマンドは、Hint オブジェクトによって装飾できる関数によって処理されます。
関数が False を返すと、Hint オブジェクトのカウントが増加し、上限を超えるとヒント メッセージが出力されます。
関数がタプルを返すと、メインループによって呼び出されます。
class Hint(object):
def __init__(self, n, msg):
self.n = n
self.msg = msg
def __call__(self, f):
def wrap(*args, **kw):
count = 1
while True:
ret = f(*args, **kw)
if ret == False:
count += 1
if count > self.n:
print self.msg
count = 0
else:
break
return ret
return wrap
def start_at(place):
print "start at %d" % place
return "start"
@Hint(3, "You may choose from 1, 2, 3.")
def start():
print "What place will you start?"
cmd = raw_input("> ")
try:
place = int(cmd)
if place not in (1,2,3):
return False
else:
return start_at, (place,)
except ValueError:
return False
def quit():
print "I will quit"
return "quit"
@Hint(4, "You may choose from yes and no.")
def begin():
print "Would you like to begin?"
cmd = raw_input("> ")
if "yes" in cmd:
print "Great!\n"
return start, ()
elif "no" in cmd:
print "Bad!\n"
return quit, ()
else:
return False
call_func, args = begin, ()
while True:
ret = call_func(*args)
if isinstance(ret, tuple):
call_func, args = ret
else:
break
ここにいくつかのテストがあります:
Would you like to begin?
> abc
Would you like to begin?
> def
Would you like to begin?
> 123
Would you like to begin?
> 345
You may choose from yes and no.
Would you like to begin?
> yes
Great!
What place will you start?
> 5
What place will you start?
> fg
What place will you start?
> sd
You may choose from 1, 2, 3.
What place will you start?
> 2
start at 2