python - Working with multiple values for a dictionary key -


loanwolf_bank_accounts_length = {         '001': 7,       # banque de montréal         '002': 7,       # scotia         '003': 7,       # rbc         '004': 7 or 11, # td (7 or 11)         '006': 7,       # bnc         '010': 7,       # cibc         '016': 9,       # hsbc         '039': 9,       # banque laurentienne         '614': 10,      # tangerine         '815': 7,       # desjardins         '829': 7,       # desjardins ontario     } 

and

def clean_bank_account(self):     bank_account = self.form.cleaned_data.get('bank_account')     bank_transit = self.form.cleaned_data.get('bank_transit')     if bank_account not in (none, ''):         bank = self.form.cleaned_data.get('bank')         if bank not in (none, ''):              # check bank account format specific banks             length = settings.loanwolf_bank_accounts_length.get(bank)             if length:                 if bank_transit not in (none, ''):                     if not bank_account.isnumeric() or length != len(bank_account):                         if bank == '004':                             raise validationerror(                                 _('bank account number must contain 7 or 11 digits') % length  # noqa                             )                         else:                             raise validationerror(                                 _('bank account number must contain %d digits') % length  # noqa                             )                 else:                     raise validationerror(                         _('cannot validate bank account without valid bank transit')  # noqa                     )     return bank_account 

i wanted give more 1 arguments, definitions 004 if length different of 7 or 11, want display bank account number must contain 7 or 11 digits. how pass more 1 arguments keyword?

i'd recommend having dictionary store mapping of <bankid : set>, each set contains possible lengths:

loanwolf_bank_accounts_length = {              '001': {7},       # banque de montréal              '002': {7},       # scotia              '003': {7},       # rbc              '004': {7, 11}, # td (7 or 11)              '006': {7},       # bnc              '010': {7},       # cibc              '016': {9},       # hsbc              '039': {9},       # banque laurentienne              '614': {10},      # tangerine              '815': {7},       # desjardins              '829': {7},       # desjardins ontario          } 

your code need test set membership, this:

 ( len(bank_account) not in length ) 

you can simplify error handling using str.format.

... if length:     if bank_transit not in (none, ''):         if not bank_account.isnumeric() or (len(bank_account) not in length): # important part             raise validationerror(                     _('bank account number must contain {} digits').format(' or '.join(map(str, length)))  # noqa                 )      else:         raise validationerror(             _('cannot validate bank account without valid bank transit')  # noqa         ) ... 

Comments

Popular posts from this blog

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

python Tkinter Capturing keyboard events save as one single string -

sql server - Why does Linq-to-SQL add unnecessary COUNT()? -