0

I am trying to match the details with database, if the user gives input data in json format. we have to check the keys, if the keys are present in data(which we have) it should to do some preprocessing and returns the results. I am creating an API, which returns according to the needs.

Here i have used multiple if statements but i want to reduce my code, is there any way to get that?

Below is my code for reference

@app.route('/api',methods=['GET'])
def aadhar_pan_users_check():
    data=request.get_json()
    aadhar=data.get('aadhar')
    pancard=data.get('pan')
    fname = data.get('firstName')
    lname = data.get('lastName')
    gender = data.get('gender')
    cust_type = data.get('cust_type')
    dob = data.get('dob')

   
    res=[]
    if aadhar!=None and pancard!=None:
        res.append((aadhar_check(aadhar), pan_check(pancard)))
        return str(res)
    elif pancard!=None:
        return str(pan_check(pancard))
    elif aadhar!=None:
        return str(aadhar_check(aadhar))
        # return jsonify({'aadhar': str(aadhar_check(aadhar))})
    elif fname!=None and lname!=None and gender!=None and cust_type!=None and dob!=None and aadhar!=None and pancard!=None:
        ret=[fname,lname,gender,cust_type,dob, aadhar, pancard]
        ret_ty=[formatting_data['fname'],formatting_data['lname'],formatting_data['gen'],formatting_data['client_type'],formatting_data['dob'],formatting_data['aadhar'],formatting_data['pan']]
        # print(ret_ty)
        return (preprocess_user_data(ret,ret_ty),aadhar_check(aadhar),pan_check(pancard))

    elif fname!=None and lname!=None and gender!=None and cust_type!=None:
        ret=[fname,lname,gender,cust_type]

        ret_ty=[formatting_data['fname'],formatting_data['lname'],formatting_data['gen'],formatting_data['client_type']]
        # print(ret_ty)
        return preprocess_user_data(ret,ret_ty),aadhar_check(aadhar)
    elif fname!=None and lname!=None and gender!=None:
        ret=[fname,lname,gender]
        ret_ty=[formatting_data['fname'],formatting_data['lname'],formatting_data['gen']]
        # print(ret_ty)
        return preprocess_user_data(ret,ret_ty)
    elif fname!=None and lname!=None and dob!=None:
        ret=[fname,lname,dob]
        print(1000*'3')
        ret_ty=[formatting_data['fname'],formatting_data['lname'],formatting_data['dob']]
        return preprocess_user_data(ret,ret_ty),aadhar_check(aadhar)
    elif fname!=None and lname!=None:
        ret=[fname,lname]
        ret_ty=[formatting_data['fname'],formatting_data['lname']]
        # print(ret_ty)
        return preprocess_user_data(ret,ret_ty)
    elif fname!=None and lname!=None and cust_type!=None and dob!=None:
        ret=[fname,lname,gender,cust_type,dob] 
        print(1000*'4')
        ret_ty=[formatting_data['fname'],formatting_data['lname'],formatting_data['client_type'],formatting_data['dob']]
        # print(ret_ty)
        return preprocess_user_data(ret,ret_ty),aadhar_check(aadhar)
    elif fname!=None and lname!=None and gender!=None and cust_type!=None:
        ret=[fname,lname,gender,cust_type]
        print(1000*'5')
        ret_ty=[formatting_data['fname'],formatting_data['lname'],formatting_data['gen'],formatting_data['client_type']]
        # print(ret_ty)
        return preprocess_user_data(ret,ret_ty),aadhar_check(aadhar)
    elif fname!=None:
        ret=[fname]
        print(type(fname))
        ret_ty=[formatting_data['fname']]
        # print(ret_ty)
        return preprocess_user_data(ret,ret_ty)
    else:
        return "Something wrong"

I have some functions which helps in preprocessing.

1 Answers1

2

I'm not 100% clear on what your code is doing, but it looks like you are checking to see if aadhar or pancard are None -- returning some string representation if they are None -- and then using whatever extra information is provided.

Using that, could you just loop through the 'extra' data, if it is there, append it to a list, and if not, do nothing?

Then return the preprocess_function as normal.

Something like below:

def aadhar_pan_users_check():
    data=request.get_json()
    aadhar=data.get('aadhar')
    pancard=data.get('pan')

    cust_data = {
        'fname' : data.get('firstName'),
        'lname' : data.get('lastName'),
        'gen' : data.get('gender'),
        'client_type' : data.get('cust_type'),
        'dob' : data.get('dob')
    }

    res = []
          
    ret = []
    ret_ty = []
    for key,value in cust_data.items():
        if value is not None:
            ret.append(value)
            ret_ty.append( formatting_data[key] )  

    if (pan is not None) and (aadhar is not None):
        check_result = 'YOUR_CODE_HERE' # your_code_here
    elif (pan is not None) and (aadhar is None):
        check_result = pan_check(pan)
    elif (pan is None) and (aadhar is not None):
        check_result = aadhar_check(aadhar)
    else:
        check_result = None

    if len(ret) == 0:
        preprocess_result = None
    else:
        preprocess_result = preprocess_user_data(ret,ret_ty)

    return preprocess_result, check_result

I think the function should now do this:

If both are not None: you need to fill this in, i don't know what behaviour you expect. If pan is not None, aadhar is None: keep the result of the 'check_pan' function. If aadhar is not None, pan is None: similar to pan If both are none: keep None as the result of the check.

If there are customer data available, keep the reuslt of the preprocess function. If there are no customer data, keep None as the result of the preprocess function.

then:

return the result of the preprocess function and the check_function.

There are more if/else than the previous function, but I think that it is quite clear their purpose now.

I don't have any sample data to test what this would do, so consider this 'pseudocode'.

You should generally compare is not None rather than `!= None, too, see for example What is the difference between " is None " and " ==None "

aidanS
  • 93
  • 8
  • Thanks for helping me @aidanS, You Solution helps me to think in different way. – Gujjalapati Raju Aug 02 '20 at 11:12
  • Is there any way to get the details of the aadhar, i mean if the json data has aadhar key, i have to call aadhar function same for pan too, is there any way to include aadhar and pan? @aidanS – Gujjalapati Raju Aug 02 '20 at 11:30
  • You could include them in the cust_data dictionary, but if you want to return the preprocess_user_data only if and only if the aadhar and pancard pass whatever checks happen inside aadhar_check() and pan_check(), then it's probably better to explicity call these functions outside the loop, to make it more clear what is going on. This is only my suggestion, however! – aidanS Aug 02 '20 at 13:11
  • Edited original solution. Also, please remember to upvote if the solution is useful! – aidanS Aug 04 '20 at 11:34
  • 1
    Hey @aidanS thank you for sacrificing your time for me. You really dont know how you helped me. Thank you so much. I tried in so many but i couldn't able to full fill my requirements. Just minimal changes of your code works for me. Hatsoff for your problem understanding. – Gujjalapati Raju Aug 04 '20 at 13:50