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
Him
V2EX  ›  Python

[初学]判断请求参数是否正确用了一大堆 if,有什么简单的方法

  •  
  •   Him · 2015-04-15 16:23:00 +08:00 · 4831 次点击
    这是一个创建于 3306 天前的主题,其中的信息可能已经有所发展或是发生改变。
    RT.

    刚学2天……

    每一个请求中都要用一大堆if 去判断传进来的参数是否合理,有没有什么简单的方法去处理?
    34 条回复    2015-07-15 14:15:09 +08:00
    jasonding
        1
    jasonding  
       2015-04-15 16:25:07 +08:00   ❤️ 1
    封装成一个独立的方法呗,
    royzhanggy
        2
    royzhanggy  
       2015-04-15 16:28:17 +08:00   ❤️ 1
    装饰器
    batman2010
        3
    batman2010  
       2015-04-15 16:28:48 +08:00
    用一个数组把所有合法值封起来。
    batman2010
        4
    batman2010  
       2015-04-15 16:30:20 +08:00
    @batman2010 看错要求了,sorry
    sumhat
        5
    sumhat  
       2015-04-15 16:33:16 +08:00   ❤️ 1
    如果是面向对象的语言,尽量把参数封装成自定义的类,然后在类构造的时候检查,类还可以提供类似 isValidForXXX() 的方法作特殊的检查。
    Anybfans
        6
    Anybfans  
       2015-04-15 16:45:44 +08:00
    @sumhat 这是Python区。。明显面向对象嘛、、哈哈哈
    Septembers
        7
    Septembers  
       2015-04-15 16:48:10 +08:00
    不合格的问题 - 背景信息不足
    1. 用的什么框架
    2. 现在的例示代码(请发 http://gist.github.com )
    loading
        8
    loading  
       2015-04-15 16:53:39 +08:00 via iPhone
    把判断写到一个函数里调用,这样一般情况下就看不到了,等发现更好方法时,再修改那个函数。

    你没代码,帮不到你。
    gkiwi
        9
    gkiwi  
       2015-04-15 17:02:08 +08:00
    听楼主的意思像是前端post过来数据,需要不停的写if来进行判断.
    说实话,没有太好的方法.
    不过,而一些较为通用的规则,比如不能为空啊,必须为数字之类的可以封装成装饰器~
    我的某个not_empty: https://gist.github.com/5d84822a5b0d707c53d8.git

    不过wtforms也许会对你有些帮助.

    https://wtforms.readthedocs.org/en/latest/
    gkiwi
        10
    gkiwi  
       2015-04-15 17:04:12 +08:00
    上个git貌似打不开,可以瞅下我的代码: https://gist.github.com/bugkiwi/5d84822a5b0d707c53d8
    gkiwi
        11
    gkiwi  
       2015-04-15 17:05:27 +08:00   ❤️ 1
    🌰

    ```
    @bp.route('/order/comment',methods=['GET','POST'])
    @login_required
    @not_empty('orderId',POST=['servicesc','envsc','tastesc','foods'])
    def order_comment():
    pass
    ```
    bcxx
        12
    bcxx  
       2015-04-15 17:07:28 +08:00
    @gkiwi 你试试用 class 来实现这个 decorator ... 嵌套这么多层太难看了。
    bcxx
        13
    bcxx  
       2015-04-15 17:08:02 +08:00   ❤️ 1
    FYI 可以参考一下 WTForm 这种声明式的方法来做,会好看很多(也好维护)
    gkiwi
        14
    gkiwi  
       2015-04-15 17:18:25 +08:00
    @bcxx thanks:) __call__确实会舒服好多

    不过这个是,之前老代码就一直用着没改了~~~
    155
        15
    155  
       2015-04-15 17:21:18 +08:00   ❤️ 1
    schema、marshmallow
    incompatible
        16
    incompatible  
       2015-04-15 17:21:57 +08:00   ❤️ 2
    大量的if else基本可以用decorator模式或chain of responsibility来代替
    mulog
        17
    mulog  
       2015-04-15 17:28:49 +08:00   ❤️ 1
    FastMem
        18
    FastMem  
       2015-04-15 17:44:13 +08:00
    封装, PHP function is_chinese($value) {//Do}

    然后用的时候这样

    if(is_chinses($_POST['Comments'])) {} else {}
    Richardzhibu
        19
    Richardzhibu  
       2015-04-15 19:09:54 +08:00
    我一般把参数类型分类,比如int、 uint、long、float、str、mail、url、password,然后把业务规则分配到到这些类型里
    FrankFang128
        20
    FrankFang128  
       2015-04-15 19:11:35 +08:00
    表编程
    xavierskip
        21
    xavierskip  
       2015-04-15 19:20:43 +08:00
    什么样的请求参数?
    可以用 **kwds 参数传递来接受参数
    python判断流程可以用 try...except...
    laike9m
        22
    laike9m  
       2015-04-15 19:47:06 +08:00
    贴代码吧,说不定真正的问题并不出在if else上面
    OpooPages
        23
    OpooPages  
       2015-04-15 22:08:25 +08:00 via iPad
    适配器模式。
    messense
        24
    messense  
       2015-04-15 22:12:32 +08:00
    可以试试用 JSON Schema 描述校验,用 https://github.com/sunlightlabs/validictory 这个库校验。
    saber000
        25
    saber000  
       2015-04-15 22:42:07 +08:00
    @messense JSON Schema更多的文档可以看这里:http://spacetelescope.github.io/understanding-json-schema/

    这种方法很推荐,现在基本上已经成为我的标准方法.具体有多爱呢,我基于JSON Schema写了一个序列化任意Python对象的库,不要脸发出来求指教.
    virusdefender
        26
    virusdefender  
       2015-04-16 00:03:59 +08:00
    参考django的form或者django rest framework的serializer 就是写一个类,里面写明字段和验证方法,然后进行验证
    kzzhr
        27
    kzzhr  
       2015-04-16 01:44:34 +08:00
    可以在某些地方放一些规则,比如正则。然后用循环的方式依次判断。
    这样可以吧。。随口说的莫喷。。。
    m_z
        28
    m_z  
       2015-04-16 09:24:14 +08:00
    前端验证呢?不通过不提交...
    Him
        29
    Him  
    OP
       2015-04-16 09:34:25 +08:00
    我自己写的: https://github.com/shaotianchi/easycheckmodel4python
    用的是上面说的装饰器的方法。
    多谢各位。
    Catstyle
        30
    Catstyle  
       2015-04-16 09:40:00 +08:00
    @gkiwi
    if params is None and params == [] and len(kargs) == 0:
    raise Exception("You should give me some params;")
    这个判断确定能跑过?
    前两个有什么情况能为True?
    gkiwi
        31
    gkiwi  
       2015-04-16 10:19:34 +08:00
    @Catstyle 额,确实触发不了 = =
    已经修改~:)
    MarioLuisGarcia
        32
    MarioLuisGarcia  
       2015-04-16 11:31:41 +08:00
    定义里是kkargs, 判断里是kargs了啊
    staticor
        33
    staticor  
       2015-04-16 12:31:06 +08:00   ❤️ 1
    http://simeonfranklin.com/blog/2012/jul/1/python-decorators-in-12-steps/

    楼主如果想看看装饰器作 类型限制的话可以看看这个. 当时我连decorator是啥都不知道呢, 看完这个就多少明白了.
    ligyxy
        34
    ligyxy  
       2015-07-15 14:15:09 +08:00
    property装饰器
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2168 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 02:33 · PVG 10:33 · LAX 19:33 · JFK 22:33
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.