V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
chenqh
V2EX  ›  Python

Python 有没有什么库可以方便的支持 router 的

  •  
  •   chenqh · 2020-08-26 13:21:00 +08:00 · 1540 次点击
    这是一个创建于 1338 天前的主题,其中的信息可能已经有所发展或是发生改变。

    因为要对接上下游,不同的上游有不同的请求参数,处理参数这样
    我的想法就是

    rule.add("test1", test1func)
    rule.add("test2", test2func)
    

    然后 rule.handle(name, *args) 就自动路由到对应的 func 里面去

    有没有什么类库可以方便的支持这个的,本来看到了一个 dispatcher, 但是 dispatcher 是根据类型来的

    7 条回复    2020-08-26 19:01:02 +08:00
    vissssa
        1
    vissssa  
       2020-08-26 13:43:18 +08:00
    反向代理?
    xiri
        2
    xiri  
       2020-08-26 14:09:29 +08:00
    我感觉你这需求跟装饰器有点像
    Vegetable
        3
    Vegetable  
       2020-08-26 14:15:33 +08:00
    你代码都快写完了,就是基于字典写一个简单的类就行了
    Ricardoo
        4
    Ricardoo  
       2020-08-26 15:54:14 +08:00
    参照 functools 中的 singledispatch,写个装饰器把类型改成普通的值就可以了
    imn1
        5
    imn1  
       2020-08-26 16:45:05 +08:00
    1.一对一,字典
    2.多对几,就是参数有分类,bisect,不过要自己处理无序参数变成有序
    3.参看#4
    4.状态机
    5.如果是类,动态继承
    abersheeran
        6
    abersheeran  
       2020-08-26 17:26:07 +08:00
    如果 name 的字符串里不存在通配符号,那么直接用 dict 就行了。哈希挺快的。
    如果有通配符,可以用 RadixTree,https://github.com/abersheeran/index.py/blob/master/indexpy/routing/tree.py
    chenqh
        7
    chenqh  
    OP
       2020-08-26 19:01:02 +08:00
    @imn1 1 对 1
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5334 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 29ms · UTC 07:51 · PVG 15:51 · LAX 00:51 · JFK 03:51
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.