mercredi 22 septembre 2021

Simplifying if statements in python

My app currently runs based on user-set settings. Each of the settings looks like this

  Trial_balance_Year_to_date= models.BooleanField(default = False)
    tbytd_Include_opening_balances=models.BooleanField(default = False)
    tbytd_Only_use_main_accounts=models.BooleanField(default = False)
    tbytd_Print_null_values=models.BooleanField(default = False)
    tbytd_Print_description=models.BooleanField(default = True)
    tbytd_Print_account=models.BooleanField(default = True)
    tbytd_Sort_by_account_name=models.BooleanField(default = True)

The settings are used to generate financial statements, and I am trying to use if statements for this.

However, from my calculations, I would need to write over 300 if statements for each financial document.

I would need to write something like this for every possible option the user could set :

if form.tbytd_Include_opening_balances == True and form.tbytd_Only_use_main_accounts == False and form.tbytd_Print_null_values == False and form.tbytd_Print_description == False and form.tbytd_Print_account == False and form.tbytd_Sort_by_account_name == False:
            ctrbYTD = trbYTD
        else:
            ctrbYTD = trbYTD

Is there an easier way to do something like this , and if so , please assist.

Aucun commentaire:

Enregistrer un commentaire