bottle自定义过滤器

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
from bottle import Bottle, run
import re


def filter(config):
delimiter = config or ','
regexp = r'\d+(%s\d)*' % re.escape(delimiter)

def to_python(match):
return map(int, match.split(delimiter))
def to_url(numbers):
return delimiter.join(map(str,numbers))

return regexp, to_python, to_url

web = Bottle()
web.router.add_filter('idslist',filter) #在装饰器中可以直接适用这个名称'idslist'



@web.route('/list/<ids:idslist>')#装饰器中使用了filter的名称
def getsomthing(ids):
res=''
for i in ids:
res+=str(i)+'-'
return "hello %s "%res
#匹配逗号和单个数字1,3,4,2,
#匹配失败则返回404





run(web,port=8088, debug=True, reloader=True)

转载请注明来源 https://tianweiye.github.io