1

If it is possible to simplify if condition code in python.

download_mis = MisFromSite(id) check_success = download_mis.getModelObject() login_success = 'Login Failed' if check_success is None: check_success = download_mis.loginSite() login_success = check_success if check_success is None: check_success = download_mis.getMis() if check_success is None: check_success = download_mis.convertOurformat() if check_success is None: check_success = download_mis.saveModelObject() if login_success is None: check_success = download_mis.terminateLogin() 

I write a code to fetch data from the website. I am unable to fetch data without login. without data, I could not process the data. each and every function is relay previous function output, except terminateLogin function. once login was a success need to terminate the login cookie so terminatelogin function should run if any function executed or not.

2
  • I think this is quite common, check for error or success before moving on to next step, the syntax might be strange to you, you can change it to a function ex: handle(check_success) and return or stop, this would make you code cleaner I think Commented Feb 21, 2020 at 6:31
  • can you send a sample code for it? Commented Feb 21, 2020 at 6:46

2 Answers 2

2

I'm not fully aware of your intention, but the code you provided can be simplified as this:

download_mis = MisFromSite(id) # An ordered list of functions to execute # Remember: don't use parenthesis as they result in execution of functions functions = [download_mis.loginSite, download_mis.getMis, download_mis.convertOurformat, download_mis.saveModelObject, download_mis.terminateLogin] # This function always executes check_success = download_mis.getModelObject() for function in functions: # if at any step result is not None then nothing will execute if check_success: break # otherwise next function executes until result is not None check_success = function() 
Sign up to request clarification or add additional context in comments.

4 Comments

Did you see check_success = function()?
@JuhilSomaiya As I mentioned I'm not aware of the intention of the OP, but as far as I can understand the code he provided, it's a simplified version of that.
once the login is done. I need to terminate a login cookie if any functions run successfully or not. @szamani20, please change code once login success I need to run the logout function. please change the condition on function name not index. this one missing login_success = check_success
@vignesh You should edit your question and include necessary information.
0

I find it much more pythonic to raise exceptions whenever something fails. For instance, maybe getModelObject' raises aModelFetchErrorif it can't get the model. PerhapsloginSiteraisesLoginError` if it can't log in. With that in place, your flow can be simplified to:

download_mis = MisFromSite(id) download_mis.getModelObject() download_mis.loginSite() download_mis.getMis() download_mis.convertOurformat() download_mis.saveModelObject() download_mis.terminateLogin() 

Each line must succeed before the program can move on to the next one. If convertOurformat failed for some reason, saveModelObject would never be reached. You also get nice tracebacks explaining exactly what failed and where, basically for free.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.