# Routing
def compile_route(route):
""" Compiles a route string and returns a precompiled RegexObject. Routes may contain regular expressions with named groups to support url parameters.
Example:
'/user/(?P<id>[0-9]+)' will match '/user/5' with {'id':'5'} A more human readable syntax is supported too.
Example:
'/user/:id/:action' will match '/user/5/kiss' with {'id':'5', 'action':'kiss'}
Placeholders match everything up to the next slash.
'/user/:id#[0-9]+#' will match '/user/5' but not '/user/tim'
Instead of "#" you can use any single special char other than "/"
"""
route = route.strip().lstrip('$^/ ').rstrip('$^ ')
# 将 /user/:id#[0-9]+#/ 转换为 /user/(?P<id>[0-9]+)/
route = re.sub(r':([a-zA-Z_]+)(?P<uniq>[^\w/])(?P<re>.+?)(?P=uniq)',r'(?P<\1>\g<re>)',route)
# 将 /user/:id/ 转换为 /user/(?P<id>[^/]+)/
route = re.sub(r':([a-zA-Z_]+)',r'(?P<\1>[^/]+)', route)
return re.compile('^/%s$' % route)

bottle.py中的路由解析代码的更多相关文章

  1. bottle.py中的路由搜索优化

    # Now search regexp routes # ROUTES_REGEXP是一个字典,键是请求方法,值是[路由, 处理函数]的列表 # 例如:{"GET", [[路由1, ...

  2. WCF技术解剖2-TcpTracer路由解析代码

    TcpTrace路由解析,参考页面-http://www.cnblogs.com/artech/archive/2008/09/19/1294227.html. TcpTrace工具下载地址:http ...

  3. iOS 中的XML解析代码(SAX)

    1.XML解析(SAX) NSXMLParser SAX 大文件 1)打开文档 - (void)parserDidStartDocument:(NSXMLParser *)parser 2)开始查找起 ...

  4. bottle.py中的SimpleTemplate

    import re class SimpleTemplate(object): re_block = re.compile(r'^\s*%\s*((if|elif|else|try|except|fi ...

  5. AspNet Mvc 路由解析中添加.html 等后缀 出现404错误的解决办法

    使用Mvc 有时候我们希望,浏览地址以.html .htm 等后缀名进行结尾. 于是我们就在RouteConfig 中修改路由配置信息,修改后的代码如下 routes.IgnoreRoute(&quo ...

  6. 函数体中return下面的代码不执行,但是需要预解析

    //函数体中return下面的代码不执行,但是需要预解析 function fn(){ console.log(num);//undefined return function(){ }; var n ...

  7. html中可以写php代码,但是文件后缀名需要是.php而不是.html。否则php程序不会被解析执行。

    html中可以写php代码,但是文件后缀名需要是.php而不是.html.否则php程序不会被解析执行. <div class="goods_title"><?p ...

  8. MVC中的路由

    authour: chenboyi updatetime: 2015-05-02 16:10:04 friendly link:   目录 1,思维导图 2,MVC处理机制简图(讲解路由解析) 3,默 ...

  9. urls.py的配置[路由配置]

    urls.py的配置[路由配置] Get请求与Post请求的方式 get请求: (1)地址栏输入url (2)<a href="请求url">点击</a> ...

随机推荐

  1. inode满处理

    https://blog.csdn.net/liuxiao723846/article/details/79423581 [root@monitor251 bin]# for i in /boot; ...

  2. 阿里云 RDS for MySQL支持什么引擎

    问题:我们的服务器是买的是阿里云,mysql版本5.011 ,本地和服务器配置一样,在本地可以安装discuzX3.4,但是在服务器上却报错了,如下图: 找了半天,才知道阿里云RDS 支持的mysql ...

  3. ImageWatch 无法安装在VS2017环境下的解决方案

    Download: https://marketplace.visualstudio.com/items?itemName=VisualCPPTeam.ImageWatch#qna For Visua ...

  4. restore not found的错误(问题2)

    最近在写gan,那么就牵扯到在一个session中加载两个图,restore的时候会有问题.如这篇文章写的(http://blog.csdn.net/u014659656/article/detail ...

  5. 找到多个与名为“Home”的控制器匹配的类型

    “/”应用程序中的服务器错误. 找到多个与名为“Home”的控制器匹配的类型.如果为此请求(“{controller}/{action}/{id}”)提供服务的路由没有指定命名空间以搜索与此请求相匹配 ...

  6. decode函数解决oracle报错"除数为0"的问题

    公司的网站在运行的时候突然报错打不开了,打开一看发现报了一个错:ORA-01476:除数为0. 网上一搜发现还是挺多人遇到这个问题的,解决办法就是用decode函数. decode是oracle内置的 ...

  7. Linux和windows 平台下启动和关闭mysql服务

    Linux平台下启动和关闭mysql服务 一.linux下查看mysql服务的两种方式: 方式一: [root@localhost bin]ps -ef|grep mysql 方式二: [root@l ...

  8. 配置python3

    1. 下载解压.$ wget https://www.python.org/ftp/python/3.4.1/Python-3.4.1.tgz$ tar zxvf Python-3.4.1.tgz 2 ...

  9. 答题卡作文模块的一种方法-VSTO

    在开始做之前,首先百度了Word有没有简单的生成方法,果然有--页面布局->稿纸设置->方格式稿纸 效果如下图所示.很规范,但是不是答题卡所需要的,因为这样会把所有页面都设置为这样的稿纸. ...

  10. linux图像界面连接-xdm

    有两种方法可以激活 --:修改/etc/gdm/custom.conf文件在 [security]字段下增加AllowRemoteRoot=true [xdmcp]字段下增加Enable=true - ...