0

I have a member variable;

valid_add_customer_request = {
    "client": {0},
    "name": "CustomerName",
    "account_number": "W/L141123512",
    "mobile_number": "1232 414122",
    "landline_number": "1234515123",
    "email": "CustomerName@email.com"
}

Where the client is an id of another object. Ideally I'd like to do something like but because the variable is a dictionary this isn't possible.

valid_add_customer_request.format(1)

Is there a clean way of achieving the above.

Daniel Sims
  • 531
  • 4
  • 21
  • Please restate your question. The value of `"client"` is a set – user8408080 Dec 16 '18 at 16:25
  • 1
    This may help https://www.programiz.com/python-programming/methods/dictionary/update. `valid_add_customer_request.update(dict(client=1))` – Mohammad Albakri Dec 16 '18 at 16:25
  • Similar to [this](https://stackoverflow.com/questions/5952344/how-do-i-format-a-string-using-a-dictionary-in-python-3-x) or the opposite? – Alex W Dec 16 '18 at 16:26
  • Exactly what @MohammadAlbakri posted is what I was looking for, if you post it as an answer I'll accept it. – Daniel Sims Dec 16 '18 at 16:27
  • in the example data value of 'client' is set, whar @MohammadAlbakri code does is to make it int. Also, no need to do update, just `valid_add_customer_request['client'] = 1` or {1} to preserve the set type – buran Dec 16 '18 at 16:31

3 Answers3

2
valid_add_customer_request = lambda x: {
    "client": x,
    "name": "CustomerName",
    "account_number": "W/L141123512",
    "mobile_number": "1232 414122",
    "landline_number": "1234515123",
    "email": "CustomerName@email.com"
}

valid_add_customer_request(1)
hda
  • 192
  • 1
  • 4
1

This may help

https://www.programiz.com/python-programming/methods/dictionary/update.

valid_add_customer_request.update(dict(client=1))

  • Links to external websites may be invalid in the future. It is recommended to explain the solution in the answer to avoid this. – amanb Dec 16 '18 at 16:33
1

you can do one of the following, depending what desired outcome is:

valid_add_customer_request['client'] = 1 # client = 1 i.e. int
valid_add_customer_request['client'] = {1} # client = {1} i.e. set with one element 1
valid_add_customer_request.setdefault('client', set()).add(1) # client = {0,1}, i.e. add element to the existing set
buran
  • 13,682
  • 10
  • 36
  • 61
  • I went with valid_add_customer_request['client'] = 1 in the end, thanks for your help! – Daniel Sims Dec 16 '18 at 16:47
  • you know what you want it to be. just make sure it doesn't break you code elsewhere if it expects `client` to be `set` as in example. – buran Dec 16 '18 at 16:48
  • I'm only really using this in unit tests at the minute, so it changes for every request. Definitely a good thing to know I can do this though. – Daniel Sims Dec 16 '18 at 16:59