How to simplify my if conditions using Boolean Logic?

126 Views Asked by At

My code:

def save_model(self, request, obj, form, change):
    if "_continue" in request.POST:
        if "PAN_ID" in request.POST and not change:
            try:
                data = Dematad.objects.filter(
                    Q(PAN1=obj.PAN_ID) | Q(PAN2=obj.PAN_ID) | Q(PAN3=obj.PAN_ID)
                ).get()
                
                obj.email= data.EMAIL1.strip() or data.EMAIL2.strip() or data.EMAIL3.strip() or None
                
                obj.full_name =  data.NAME
                
                obj.address =  ", ".join(
                        [data.AD1, data.AD2, data.AD3, data.AD4, data.PIN]
                    )
            except Dematad.DoesNotExist:
                    self.message_user(
                        request, "User created and saved. No data available in database."
                    )
                    
 
         
    if not change:
        obj.is_superuser = False
        obj.is_staff = True
        obj.set_password(User.objects.make_random_password(20))

​ return super().save_model(request, obj, form, change)

I want to simplify the code and thus tried to model the if conditions through logic. Consider the try, except block together as D

S1: $A \implies ((B \land \tilde C) \implies D)$

S2: $ \tilde C \implies E $

How do I connect these statements? How do I simplify my if conditions?

EDIT: By simplification I' m meaning to remove the inner block, or avoid nested if's. I want to know how can expressions be simplified.

1

There are 1 best solutions below

2
On

I personally don't think you can combine or simplify because:

  1. You don't have repeated expressions, so playing with different variables is not a good idea IMHO
  2. We don't know if combining the if conditions would lead into a different program, so better a programmer should answer that question

Edit. WolframAlpha cannot reduce the combined expressions.