-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
50 lines (40 loc) · 884 Bytes
/
test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#注册函数
def reg(cmd):
def _reg(fn):
cmd_tbl[cmd] = fn
return fn
return _reg
#自定义函数
@reg('war')
def foo1():
print('ward')
@reg('py')
def foo2():
print('python')
def command_dispatcher():
cmd_tbl = {} #构建全局函数
#注册函数
def reg(fn):
def _reg(fn):
cmd_tbl[cmd] = fn
return fn
return _reg
#缺省函数
def default_func():
print('Unknown command')
#调度器
def dispatcher():
while True:
cmd = input('>>>')
if cmd.strip() == '':
return
cmd_tbl.get(cmd,default_func)()
return reg,dispatcher
reg,dispatcher = command_dispatcher()
@reg('war')
def foo1():
print('ward')
@reg('py')
def foo2():
print('python')
dispatcher()