开发者

Best way to confine given string to white list

I have a urlconf setup to catch /account/invoice开发者_如何学Cs/*/, and with it I want to catch a few optional qualifiers. In this case: unpaid, paid, disregarded. I'm not outputting this so I think accepting input in this fashion should be fine, but still I need to white list and translate these options into what the API expects. Is there a better way to do this (where the variable 'paid' comes in as a view argument)?

filter['unpaid'] = 0
filter['paid'] = 1
filter['disregarded'] = 2

if paid is in filter:
    paid = filter[paid]


Simple solution:

if paid in ['unpaid', 'paid', 'disregarded']

Your solution and DataGreed's are more verbose and require you to make up values for your dict/object elements.


Yes, your solution is good. But i just like dict.get a little more:

enum = {
        'unpaid'      : 0,
        'paid'        : 1,
        'disregarded' : 2,
        }


def view(request, option):

    value = enum.get(option, None)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜