减少if语句以最大化python中的效率

时间:2015-01-08 07:51:04

标签: python performance if-statement dictionary

我已经编写了以下函数来构建参数,我想知道在python中有没有其他方法可以做到这一点,以便提高代码效率...

build_parameters(users[keys]["email"], Null , xsmtpapi, Message_Subject, Message_Content, Message_Content, 'support@brightspyre.com', 'BrightSpyre', 'support@brightspyre.com', Null, Null, Null, Null)

这是函数

def build_parameters(to = None, toname = None, x-smtpapi = None, subject = None, text = None, html = None, from = None, cc = None, ccname = None, bcc = None, bccname = None, fromname = None, replyto = None, date = None, files = None, content = None, headers = None):
    param = {}
    if headers:
        param['headers'] = headers
    if content:
        param['content'] = content
    if files:
        param['files'] = files
    if date:
        param['date'] = date
    if replyto:
        param['replyto'] = replyto
    if fromname:
        param['fromname'] = fromname
    if bccname:
        param['bccname'] = bccname
    if bcc:
        param['bcc'] = bcc
    if ccname:
        param['ccname'] = ccname
    if cc:
        param['cc'] = cc
    if from:
        param['from'] = from
    if html:
        param['html'] = html
    if text:
        param['text'] = text
    if subject:
        param['subject'] = subject
    if x-smtpapi:
        param['x-smtpapi'] = x-smtpapi
    if toname:
        param['toname'] = toname
    if to:
        param['to'] = to

    return param

已更新

我更新了@J0HN

所描述的代码
_allowed_keys = {'to', 'toname', 'x-smtpapi', 'subject', 'text', 'html', 'from', 'cc', 'ccname', 'bcc', 'bccname', 'fromname', 'replyto', 'date', 'files', 'content', 'headers'}

def build_parameter(**kwargs):
    return {key:value for key, value in kwargs.items() if key in _allowed_keys}


params = build_parameter(to = users[keys]["email"], toname = users[keys]["name"], x-smtpapi = xsmtpapi, subject = Message_Subject,text = Message_Content, html = Message_Content, from = 'support@bs.com', fromname = 'BS', replyto = 'support@bs.com')

错误

params = build_parameter(to = users[keys]["email"],toname = users[keys]["name"], x-smtpapi = xsmtpapi, subject = Message_Subject,text = Message_Content, html = Message_Content, from = '
support@bs.com', fromname = 'BSe', replyto = 'support@bs.com')                                                                                                     
                                                                                                                                                                                        ^    
SyntaxError: invalid syntax 

2 个答案:

答案 0 :(得分:4)

_allowed_keys = {'to', 'toname', 'x-smtpapi', ...]

def build_parameters(**kwargs):
    return {key:value for key, value in kwargs.items() if key in _allowed_keys}

一步一步:

因此,此函数接收任意数量的关键字参数,但过滤掉_allowed_keys以外的密钥。

UPD :好的,from是一个关键字,x-smtpapi不能是关键字参数,因为它包含-。这是一种预期的行为,但canonical way解决方法会导致整个方法无用。

所以你需要以不同的方式表示它们,例如:

_transforms = {'x_smtpapi'='x-smtpapi', `_from`='from'}

def build_parameter(**kwargs):
    return {_transforms.get(key, key):value for key, value in kwargs.items() if _transforms.get(key, key) in _allowed_keys}

并像这样使用build_parameter(_from='from value', x_smtpapi: 'x-smtpapi value', ...)

但是,我不建议这样做可能会让人感到困惑。相反,考虑替代方法,例如构建一个类来封装创建params dict(并可能使用它)

答案 1 :(得分:0)

只需建一本字典。

params = {"to": "to value",
          "from": "from value}

没有必要使用这种方法。