I have write the following function to build parameters, i want to know that is there any other way in python to do that so that the code efficient is improved...
build_parameters(users[keys]["email"], Null , xsmtpapi, Message_Subject, Message_Content, Message_Content, 'support@brightspyre.com', 'BrightSpyre', 'support@brightspyre.com', Null, Null, Null, Null)
Here is the function
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
UPDATED
I have updated the code as described by @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')
error
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