0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

python Bottleの基本4 特殊ルーティング(独自のフィルター)の話

Posted at

正規表現文字列、URLフラグメントをpython値に変換する、その逆の呼び出し可能オブジェクト3つの要素を返す関数をつくり独自のフィルターをルーターに追加できます。

例題.py
app = Bottle()

def list_filter(config):
    ''' 正規表現文字列'''
    delimiter = config or ','
    regexp = r'\d+(%s\d)*' % re.escape(delimiter)
    def to_python(match): #URLフラグメントをpython値に変換する
        return map(int, match.split(delimiter))
    def to_url(numbers): #その逆の呼び出し可能オブジェクト
        return delimiter.join(map(str, numbers))
    return regexp, to_python, to_url
app.router.add_filter('list', list_filter)
@app.route('/follow/<ids:list>')
def follow_users(ids):
    for id in ids:
        ...

以下のスニペットは、正規表現を用い 1,2,3,4のような1桁数字をカンマで区切ったルーティングを可能とします。

regexp = r'\d+(%s\d)*' % re.escape(delimiter)
ex5.py
# bottleの基本5
from bottle import *
import re
app = Bottle()
def list_filter(config):
    ''' Matches a comma separated list of numbers. '''
    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
app.router.add_filter('list', list_filter)
@app.route('/follow/<ids:list>')
def follow_users(ids):
    x="\n".join(['<h3>%s</h3>'%x for x in ids])
    for id in ids:
        print(id)
    return x
@app.error(404)
def error404(error):
  return template("<h1>{{error_message}}</h1>", error_message="404 ページが存在しません。")
HOST,PORT='localhost',8080
if __name__ =='__main__':
    app.run(host=HOST,port=PORT)

上記のプログラムを実行すると http://localhost:8080/follow/1,2,3

image.png
パラメータを配列を受け取ることができます。

また、urlをhttp://localhost:8080/follow/1,2,3,aにすると以下のようになります。
regexp = r'\d+(%s\d)*' % re.escape(delimiter)この正規化フィルタで除外されるので
image.png
となります。

Rest API などで可変長パラメータを使いたい時などの例をいかに示します。
Url http://localhost:8080/follow/東京,千葉,茨木
image.png

ex6.py
# bottleの基本6
from bottle import *
import re
app = Bottle()

def list_filter(config):
    ''' Matches a comma separated list of numbers. '''
    delimiter = config or ','
    regexp = r'.*' #文字だったらなんでも許可する

    def to_python(match):
        return map(str, match.split(delimiter))

    def to_url(numbers):
        return delimiter.join(map(str, numbers))

    return regexp, to_python, to_url

app.router.add_filter('list', list_filter)

@app.route('/follow/<ids:list>')
def follow_users(ids):
    x="\n".join(['<h3>%s</h3>'%x for x in ids])
    for id in ids:
        print(id)
    return x
@app.error(404)
def error404(error):
  return template("<h1>{{error_message}}</h1>", error_message="404 ページが存在しません。")
HOST,PORT='localhost',8080
if __name__ =='__main__':
    app.run(host=HOST,port=PORT)
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?