推荐学习书目
› 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 · Apr 15, 2015 · 6173 views
    This topic created in 4239 days ago, the information mentioned may be changed or developed.
    RT.

    刚学2天……

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

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

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

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

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

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

    然后用的时候这样

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

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

    楼主如果想看看装饰器作 类型限制的话可以看看这个. 当时我连decorator是啥都不知道呢, 看完这个就多少明白了.
    ligyxy
    ligyxy
    Jul 15, 2015
    34
    property装饰器
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   1180 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 64ms · UTC 17:21 · PVG 01:21 · LAX 09:21 · JFK 12:21
    ♥ Do have faith in what you're doing.