一个简单的url方案解析器

lwe-mapper的Python项目详细描述


mapper-简单的url方案解析器

Build Statuspypi

mapper是我在处理其他stuff时创建的一个小型项目,需要一个超级简单的url reslover。
其思想是在不依赖任何python模块的情况下,尽可能地减少的占用空间。

你用它做什么取决于你。

如果f.e.需要一个简单的json服务器,请按照下面的步骤签出mjs 同样的原则。
占用空间小,易于使用,而且只有一个依赖映射器(显然)。

它是怎么工作的?非常简单。
检查The very basic并从那里开始。

目录

要求

您需要什么:

  • python 2.7或更高版本

安装

您有两个选项:

  1. 通过pypi安装pip install lwe-mapper
  2. 下载mapper.py并将其放到项目的根目录中

用法

注册函数

最基本的
frommapperimportMappermpr=Mapper.get()# Note: A path will ALWAYS end with a "/" regardless# if your URL contains a trailing "/" or not# Choose one of the two decorators@mpr.url('^/some/path/$')# Regex pattern@mpr.s_url('/some/path/')# Simple pathdeffunc():print('func called')# What e.g. your webserver would do...mpr.call('http://some.url/some/path')

带有查询的url
frommapperimportMappermpr=Mapper.get()# Note: Adding a query does NOT change the fact that# the path will end with a "/" for the regex pattern@mpr.s_url('/some/path/')deffunc(param1,param2='default'):print(param1,param2)# We don't supply "param2" and "param3" which will result in "param2" being None and param3 being 'default'mpr.call('http://some.url/some/path?param1=123')# Following would cause a:# TypeError: func() missing 1 required positional argument: 'param1'mpr.call('http://some.url/some/path')

查询值类型转换
frommapperimportMappermpr=Mapper.get()# By default all parameters will be of type "string".# You can change the type by supplying a dict where the key matches your parameters name and the value is one of:# int, float, bool## Note for bool:#  1. Casting is case-insensitive.#  2. 1 and 0 can be casted as well@mpr.s_url('/some/path/',type_cast={'a_int':int,'a_float':float,'a_bool':bool})deffunc(a_int,a_float,a_bool):print(a_int,a_float,a_bool)mpr.call('http://some.url/some/path?a_int=123&a_float=1.0&a_bool=true')

从URL路径提取值

frommapperimportMappermpr=Mapper.get()# In pure python regex fashion we define a named capture group within our pattern to# match whatever we want.# We can use a simplified url as well though.# Not that type-casting works as well.@mpr.url('^/some/path/(?P<param1>[^/]*)/(?P<param2>[0-9]*)/$',type_cast={'param2':int})# Regex pattern@mpr.s_url('/some/path/<param1>/<param2>/',type_cast={'param2':int})# Simple pathdeffunc(param1,param2):print(param1,param2)mpr.call('http://some.url/some/path/abc/456/')

Python
frommapperimportMappermpr=Mapper.get()# It's pretty simple and type-casting works as well@mpr.s_url('/some/path/',type_cast={'param1':int,'param2':float,'param3':bool})deffunc(param1,**kwargs):print(param1,kwargs)mpr.call('http://some.url/some/path?param1=123&param2=1.0&param3=true')

返回值

frommapperimportMappermpr=Mapper.get()# Whatever you return will be returned by mapper@mpr.s_url('/some/path/')deffunc():return('str',1,1.0,True)a_str,a_int,a_float,a_bool=mpr.call('http://some.url/some/path')

使用“add”函数而不是decorator

有时,以后可能需要向映射器注册函数。这可以通过使用mappers“add”函数轻松实现。

frommapperimportMappermpr=Mapper.get()deffunc(param1,param2):print(param1,param2)# It works the same way as the decorator.# The only difference is, that we have to specify the function ourselves.mpr.add('^/some/path/(?P<param1>[0-9]*)/$',func,type_cast={'param1':int,'param2':int})mpr.s_add('/some/path/<param1>/',func,type_cast={'param1':int,'param2':int})mpr.call('http://some.url/some/path/123?param2=456')

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java应用程序不是在Eclipse中运行,而是在命令行中运行   swing Java图形组件问题。似乎找不到错误   我需要键盘。close();让代码正常工作?   Springboot中的java HttpSession   抽象语法树我想添加一个语句。我试图解析它,java解析器异常被抛出。如何克服它?   java Hibernate:清理会话   具有不连续子集的java划分问题   java正则表达式查找最后一个冒号后的字符串   java从SpringShell执行OS命令   Java扫描器字符串输入   java字符串索引越界异常(charAt)   java执行器服务终止被卡住   Springockito没有继承java@ContextConfiguration   java如何为一个servlet映射多个url   java安卓获取命令的stderr   java生成类型。表:数据库中的大数字   安卓 Getter Setter返回NothingJava