0

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 
TheCodeArtist
  • 21,479
  • 4
  • 69
  • 130
Muhammad Taqi
  • 5,356
  • 7
  • 36
  • 61
  • 3
    Cant' you just directly build a dictionary from where you are using it? – J0HN Jan 08 '15 at 07:53
  • Just assign the values to the Keys without checking at all. You could also change the default value of the arguments to "". – Christian Rapp Jan 08 '15 at 07:55
  • No, actually its a function that check which arguments are passed and build the dictionary on arguments that are passed? some time all arguments are passed to function, something only 3 or 4 arguments. So i want to make it dynamic... – Muhammad Taqi Jan 08 '15 at 07:55
  • Then do as I said. Don't check and change default value to "". Empty strings won't harm when building the e-mail. If some of those parameters for example represent a list for cc in params['cc']: bla an empty string won't do any harm. – Christian Rapp Jan 08 '15 at 07:59
  • @ChristianRapp i had to build the api url using this param dictionary, if some value is empty then the url will be invalid... i have to make sure that only those keys are in the dictionary who has values. – Muhammad Taqi Jan 08 '15 at 08:04
  • The syntax error is because `from` is a keyword in Python. You can't use it as an identifier. – Janne Karila Jan 08 '15 at 09:33
  • yes i know, but know that, but how would i avoid that because in the dictionary i am creating the from keyword is must. – Muhammad Taqi Jan 08 '15 at 10:01

2 Answers2

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}

Step by step:

  • Define _allowed_keys to contain all keyword arguments' names. Refer to set documentation for details.
  • Replace parameters with **kwargs. Refer to Understanding kwargs in Python for details on what's that.
  • Use a dictionary comprehension to build a new dictionary out of kwargs
  • items are used to iterate over key-value pair of a dictionary
  • key in _allowed_keys is self-explanatory I believe

As a result, this function receives any number of keyword arguments, but filter out keys not in _allowed_keys.

UPD: ok, from is a keyword and x-smtpapi can't be a keyword argument as it contains -. It's an expected behavior, but the canonical way to workaround it renders the whole method useless.

So you'll need to represent them differently, e.g.:

_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}

And use like this build_parameter(_from='from value', x_smtpapi: 'x-smtpapi value', ...)

However, I wouldn't recommend doing this is it might be quite confusing. Instead, consider alternative approaches, e.g. build a class to encapsulate creating params dict (and probably using it)

Community
  • 1
  • 1
J0HN
  • 26,063
  • 5
  • 54
  • 85
  • then how would i call the function, is there need to specify the paramets name like`'build_parameters(to = 'taqi',...)` or needs to keep the order as definded in `allowed_keys`. I am beginner to python kindly help out. see teh question, i have updated. – Muhammad Taqi Jan 08 '15 at 08:43
  • @MuhammadTaqiHassanBukhari see that I have updated the answer. – J0HN Jan 08 '15 at 10:04
0

Just build a dictionary.

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

There is no need to use a method for this.