0

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.

Jyrki Lahtonen
  • 133,153

1 Answers1

1

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.

manooooh
  • 2,269
  • I have seen people compressing 10 line code to single line on codegolf. It must be possible but I don't know what will be the right channel to ask that. – Vishesh Mangla Jan 27 '21 at 04:48
  • It really depends on the code. You can have a code where you can apply absorption law, which is an equivalence, but it is weird to see it. Also, you don't have repeated expressions in one if statement. – manooooh Jan 27 '21 at 04:56