当前位置: 首页 > news >正文

python--route

routes是用python重新实现的Rails routes系统,用于将url映射到应用程序的actions ,并反过来生成url
它也是在openstack实现restful通信的方式,它被用来做将 URL 映射为 App 的 action,以及为 App的action 产生 URL

两个重要的方法:map.connect (定义映射规则) 和 map.match (获取映射结果)

map.connect映射规则:(map名称,路由地址,controller,action,conditions,requirements)
除了路由地址,其余都是附加参数,对映射进行一些处理
controller:资源控制类
action:资源处理操作
requirements:对输入格式进行限制
conditions:路由方式

map.routematch返回结果:如果匹配到了,会返回一个二元元组,元组第一个元素为匹配到的字典,第二个元素一个Route对象
map.match:比routematch少返回了一个route对象

# !/usr/bin/env python
# coding: utf-8from routes.mapper import Mapper# 实例化一个Mapper对象
mapper = Mapper()# 注册一个/hi路由
mapper.connect("/hi")mapper.connect(None, "/error/{action}/{id}", controller="error")
mapper.connect("home", "/hi2", controller="main", action="index")
mapper.connect('/hi2/{action}')
mapper.connect('/hi3/{action}/{id}')m4_res = mapper.routematch("/error/az/a")
print(m4_res)# 查找/hi
m_result = mapper.routematch("/hi")
print(m_result)# 查找/hi2
m2_result = mapper.routematch("/hi2/h2")
print(m2_result)# 查找/hi3
m3_result = mapper.routematch("/hi3/boy/23")
print(m3_result)

执行结果如下:

({'action': 'az', 'id': 'a', 'controller': 'error'}, <routes.route.Route object at 0x0000020481DF5D90>)
({}, <routes.route.Route object at 0x0000020481DF5D00>)
({'action': 'h2'}, <routes.route.Route object at 0x0000020481DF5B50>)
({'action': 'boy', 'id': '23'}, <routes.route.Route object at 0x0000020481DF57C0>

当映射规则很多的时候,需要使用很多次 map.connect,这时可以使用 map.resource 方法
map.resource内部定义了默认的匹配条件
第一个参数message为 member_name(资源名),第二个参数messages为collection_name(资源集合名),一般定义资源集合名为资源名的复数
collection_name作为访问的路径名,且当没有传入参数controller时,controller=collection_name

from routes.mapper import Mapper# 实例化一个Mapper对象
mapper = Mapper(always_scan=True)
mapper.resource("message", "messages", controller="testuse")
# 等同于以下匹配条件:mapper.connect('/messages', controller="testuse", action='index', conditions={'method': ['GET']})mapper.connect('/messages', controller="testuse", action='create', conditions={'method': ['POST']})mapper.connect('/messages/{id:[0-9]+}', controller="testuse", action='show', conditions={'method': ['GET']})mapper.connect('/messages/{id}', controller="testuse", action='update', conditions={'method': ['PUT']},requirements=dict(id=r"[a-z]+"))
mapper.connect('/messages/{id:[XYZ]}', controller="testuse", action='delete', conditions={'method': ['DELETE']})
http://www.lryc.cn/news/2113.html

相关文章:

  • java面试中被问到项目中的难点,怎么回答
  • 【速通版】吴恩达机器学习笔记Part1
  • 面试(九)小米C++开发一面 21.11.02
  • 儿童书写台灯哪个牌子比较好?2023儿童护眼台灯分享
  • 市场调研计划书如何写?
  • python网络爬虫—快速入门(理论+实战)(七)
  • 机器学习笔记——Chapter 1 – The Machine Learning landscape
  • skimage.feature--corner_harris、hog、local_binary_pattern说明
  • 致敬白衣天使,学习Python读取
  • JVM - 认识JVM规范
  • 文献阅读笔记 # CodeBERT: A Pre-Trained Model for Programming and Natural Languages
  • openHarmony的UI开发
  • 【JavaSE】深入HashMap
  • 华为机试题:HJ62 查找输入整数二进制中1的个数(python)
  • 代码随想录训练营一刷总结|
  • CSS中的几种尺寸单位
  • 运维必会:ansible剧本(piaybook)
  • 活动星投票午间修身自习室制作在线投票投票制作网页
  • C#泛型:高级静态语言的效率利器
  • 澳大利亚访问学者申请流程总结
  • cookie和Session的作用和比较
  • 测试员都是背锅侠?测试人员避“锅”攻略,拿走不谢
  • C++: C++模板<template>
  • chmod命令详解
  • 状态机设计中的关键技术
  • 单片机开发---ESP32S3移植NES模拟器(二)
  • 微信小程序nodej‘s+vue警局便民服务管理系统
  • 第18章 MongoDB $type 操作符教程
  • 【MySQL主从复制】快速配置
  • Typescript - interface 关键字(通俗易懂的详细教程)