How to avoid repetitive code block of an if conditional?How to merge two dictionaries in a single expression?How do I check if a list is empty?How do I check whether a file exists without exceptions?How can I safely create a nested directory in Python?How can I make a time delay in Python?How do I sort a dictionary by value?How to make a chain of function decorators?How to make a flat list out of list of listsHow do I list all files of a directory?Catch multiple exceptions in one line (except block)

All superlinear runtime algorithms are asymptotically equivalent to convex function?

Is Iron Man stronger than the Hulk?

Disabling quote conversion in docstrings

How to remap repeating commands i.e. <number><command>?

Make me a minimum magic sum

Motion-trail-like lines

Clarification of algebra in moment generating functions

Speed up this NIntegrate

Meaning of the (idiomatic?) expression "seghe mentali"

What is a common way to tell if an academic is "above average," or outstanding in their field? Is their h-index (Hirsh index) one of them?

Understanding ties

How did the Apollo guidance computer handle parity bit errors?

Counting the Number of Real Roots of A Polynomial

Why would one crossvalidate the random state number?

How to pass hash as password to ssh server

Page count conversion from single to double-space for submissions

The origin of list data structure

How can I get people to remember my character's gender?

Enabling a minor mode in all but some buffers

Why did the Apollo 13 crew extend the LM landing gear?

Dirichlet series with a single zero

Why are oscilloscope input impedances so low?

Sparring against two opponents test

Is there a proof that the set of real numbers can exactly represent distances?



How to avoid repetitive code block of an if conditional?


How to merge two dictionaries in a single expression?How do I check if a list is empty?How do I check whether a file exists without exceptions?How can I safely create a nested directory in Python?How can I make a time delay in Python?How do I sort a dictionary by value?How to make a chain of function decorators?How to make a flat list out of list of listsHow do I list all files of a directory?Catch multiple exceptions in one line (except block)






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








-1















I have some code in python, based on a specific argument I code it like below. I want to avoid repeating the same code blocks, I don't think it's intelligent or aesthetic.



If arg is x:
Do_1(x)
Do_2
Do_3
...
else:
Do_1(arg)
Do_2
Do_3
...


Alternatives?



Edit: sorry for ambiguous question, the actual code is:



for k in range(0,21):



iteration_directory = '%s/%s_round_%s' %(os.getcwd(), structure_name, k)

if not os.path.exists('%s/%s_round_%s' %(os.getcwd(), structure_name, k)):
os.makedirs('%s/%s_round_%s' %(os.getcwd(), structure_name, k))

if start_model == 'null':

if os.path.isfile( '%s/%s.1.silent' % (iteration_directory, structure_name))==False:
Parallel(n_jobs=num_cores)(delayed(fragment_search_nomodel)(iteration_directory, rosetta_path, fasta, frag_file, structure_name, map_file, i) for i in residues)

if os.path.isfile( '%s/scores1' % (iteration_directory))==False:
fragment_score(rosetta_path, iteration_directory, structure_name)

if os.path.isfile( '%s/assembled.1_0001.silent' % (iteration_directory))==False:
Parallel(n_jobs=num_cores)(delayed(fragment_assembly)(rosetta_path, iteration_directory, structure_name, i) for i in residues)

consensus_assignment(rosetta_path, iteration_directory, structure_name, k)

start_model = '%s/%s_round_%s.pdb' % (os.getcwd(), structure_name, k)


print('Time for round #%s is: ' %k, datetime.now() - startTime)

else:

coverage = model_coverage(start_model,fasta)

if coverage <= 70:

if os.path.isfile( '%s/%s.1.silent' % (iteration_directory, structure_name))==False:
Parallel(n_jobs=num_cores)(delayed(fragment_search)(iteration_directory, rosetta_path, fasta, frag_file, start_model, structure_name, map_file, i) for i in residues)

if os.path.isfile( '%s/scores1' % (iteration_directory))==False:
fragment_score(rosetta_path, iteration_directory, structure_name)

if os.path.isfile( '%s/assembled.1_0001.silent' % (iteration_directory))==False:
Parallel(n_jobs=num_cores)(delayed(fragment_assembly)(rosetta_path, iteration_directory, structure_name, i) for i in residues)

consensus_assignment(rosetta_path, iteration_directory, structure_name, k)

start_model = '%s/%s_round_%s.pdb' % (os.getcwd(), structure_name, k)

print('Time for round #%s is: ' %k, datetime.now() - startTime)









share|improve this question



















  • 1





    In Python indentation is a critical part of valid syntax. Please fix your indentation so your intent is clear.

    – lurker
    Mar 21 at 18:34






  • 2





    Do_2 and Do_3 can just be done after the if else block and unintended, such that they are always executed.

    – Paritosh Singh
    Mar 21 at 18:35











  • You only pass x if it's the same as arg, so you don't need an if, just always pass arg.

    – Blorgbeard
    Mar 21 at 18:35

















-1















I have some code in python, based on a specific argument I code it like below. I want to avoid repeating the same code blocks, I don't think it's intelligent or aesthetic.



If arg is x:
Do_1(x)
Do_2
Do_3
...
else:
Do_1(arg)
Do_2
Do_3
...


Alternatives?



Edit: sorry for ambiguous question, the actual code is:



for k in range(0,21):



iteration_directory = '%s/%s_round_%s' %(os.getcwd(), structure_name, k)

if not os.path.exists('%s/%s_round_%s' %(os.getcwd(), structure_name, k)):
os.makedirs('%s/%s_round_%s' %(os.getcwd(), structure_name, k))

if start_model == 'null':

if os.path.isfile( '%s/%s.1.silent' % (iteration_directory, structure_name))==False:
Parallel(n_jobs=num_cores)(delayed(fragment_search_nomodel)(iteration_directory, rosetta_path, fasta, frag_file, structure_name, map_file, i) for i in residues)

if os.path.isfile( '%s/scores1' % (iteration_directory))==False:
fragment_score(rosetta_path, iteration_directory, structure_name)

if os.path.isfile( '%s/assembled.1_0001.silent' % (iteration_directory))==False:
Parallel(n_jobs=num_cores)(delayed(fragment_assembly)(rosetta_path, iteration_directory, structure_name, i) for i in residues)

consensus_assignment(rosetta_path, iteration_directory, structure_name, k)

start_model = '%s/%s_round_%s.pdb' % (os.getcwd(), structure_name, k)


print('Time for round #%s is: ' %k, datetime.now() - startTime)

else:

coverage = model_coverage(start_model,fasta)

if coverage <= 70:

if os.path.isfile( '%s/%s.1.silent' % (iteration_directory, structure_name))==False:
Parallel(n_jobs=num_cores)(delayed(fragment_search)(iteration_directory, rosetta_path, fasta, frag_file, start_model, structure_name, map_file, i) for i in residues)

if os.path.isfile( '%s/scores1' % (iteration_directory))==False:
fragment_score(rosetta_path, iteration_directory, structure_name)

if os.path.isfile( '%s/assembled.1_0001.silent' % (iteration_directory))==False:
Parallel(n_jobs=num_cores)(delayed(fragment_assembly)(rosetta_path, iteration_directory, structure_name, i) for i in residues)

consensus_assignment(rosetta_path, iteration_directory, structure_name, k)

start_model = '%s/%s_round_%s.pdb' % (os.getcwd(), structure_name, k)

print('Time for round #%s is: ' %k, datetime.now() - startTime)









share|improve this question



















  • 1





    In Python indentation is a critical part of valid syntax. Please fix your indentation so your intent is clear.

    – lurker
    Mar 21 at 18:34






  • 2





    Do_2 and Do_3 can just be done after the if else block and unintended, such that they are always executed.

    – Paritosh Singh
    Mar 21 at 18:35











  • You only pass x if it's the same as arg, so you don't need an if, just always pass arg.

    – Blorgbeard
    Mar 21 at 18:35













-1












-1








-1








I have some code in python, based on a specific argument I code it like below. I want to avoid repeating the same code blocks, I don't think it's intelligent or aesthetic.



If arg is x:
Do_1(x)
Do_2
Do_3
...
else:
Do_1(arg)
Do_2
Do_3
...


Alternatives?



Edit: sorry for ambiguous question, the actual code is:



for k in range(0,21):



iteration_directory = '%s/%s_round_%s' %(os.getcwd(), structure_name, k)

if not os.path.exists('%s/%s_round_%s' %(os.getcwd(), structure_name, k)):
os.makedirs('%s/%s_round_%s' %(os.getcwd(), structure_name, k))

if start_model == 'null':

if os.path.isfile( '%s/%s.1.silent' % (iteration_directory, structure_name))==False:
Parallel(n_jobs=num_cores)(delayed(fragment_search_nomodel)(iteration_directory, rosetta_path, fasta, frag_file, structure_name, map_file, i) for i in residues)

if os.path.isfile( '%s/scores1' % (iteration_directory))==False:
fragment_score(rosetta_path, iteration_directory, structure_name)

if os.path.isfile( '%s/assembled.1_0001.silent' % (iteration_directory))==False:
Parallel(n_jobs=num_cores)(delayed(fragment_assembly)(rosetta_path, iteration_directory, structure_name, i) for i in residues)

consensus_assignment(rosetta_path, iteration_directory, structure_name, k)

start_model = '%s/%s_round_%s.pdb' % (os.getcwd(), structure_name, k)


print('Time for round #%s is: ' %k, datetime.now() - startTime)

else:

coverage = model_coverage(start_model,fasta)

if coverage <= 70:

if os.path.isfile( '%s/%s.1.silent' % (iteration_directory, structure_name))==False:
Parallel(n_jobs=num_cores)(delayed(fragment_search)(iteration_directory, rosetta_path, fasta, frag_file, start_model, structure_name, map_file, i) for i in residues)

if os.path.isfile( '%s/scores1' % (iteration_directory))==False:
fragment_score(rosetta_path, iteration_directory, structure_name)

if os.path.isfile( '%s/assembled.1_0001.silent' % (iteration_directory))==False:
Parallel(n_jobs=num_cores)(delayed(fragment_assembly)(rosetta_path, iteration_directory, structure_name, i) for i in residues)

consensus_assignment(rosetta_path, iteration_directory, structure_name, k)

start_model = '%s/%s_round_%s.pdb' % (os.getcwd(), structure_name, k)

print('Time for round #%s is: ' %k, datetime.now() - startTime)









share|improve this question
















I have some code in python, based on a specific argument I code it like below. I want to avoid repeating the same code blocks, I don't think it's intelligent or aesthetic.



If arg is x:
Do_1(x)
Do_2
Do_3
...
else:
Do_1(arg)
Do_2
Do_3
...


Alternatives?



Edit: sorry for ambiguous question, the actual code is:



for k in range(0,21):



iteration_directory = '%s/%s_round_%s' %(os.getcwd(), structure_name, k)

if not os.path.exists('%s/%s_round_%s' %(os.getcwd(), structure_name, k)):
os.makedirs('%s/%s_round_%s' %(os.getcwd(), structure_name, k))

if start_model == 'null':

if os.path.isfile( '%s/%s.1.silent' % (iteration_directory, structure_name))==False:
Parallel(n_jobs=num_cores)(delayed(fragment_search_nomodel)(iteration_directory, rosetta_path, fasta, frag_file, structure_name, map_file, i) for i in residues)

if os.path.isfile( '%s/scores1' % (iteration_directory))==False:
fragment_score(rosetta_path, iteration_directory, structure_name)

if os.path.isfile( '%s/assembled.1_0001.silent' % (iteration_directory))==False:
Parallel(n_jobs=num_cores)(delayed(fragment_assembly)(rosetta_path, iteration_directory, structure_name, i) for i in residues)

consensus_assignment(rosetta_path, iteration_directory, structure_name, k)

start_model = '%s/%s_round_%s.pdb' % (os.getcwd(), structure_name, k)


print('Time for round #%s is: ' %k, datetime.now() - startTime)

else:

coverage = model_coverage(start_model,fasta)

if coverage <= 70:

if os.path.isfile( '%s/%s.1.silent' % (iteration_directory, structure_name))==False:
Parallel(n_jobs=num_cores)(delayed(fragment_search)(iteration_directory, rosetta_path, fasta, frag_file, start_model, structure_name, map_file, i) for i in residues)

if os.path.isfile( '%s/scores1' % (iteration_directory))==False:
fragment_score(rosetta_path, iteration_directory, structure_name)

if os.path.isfile( '%s/assembled.1_0001.silent' % (iteration_directory))==False:
Parallel(n_jobs=num_cores)(delayed(fragment_assembly)(rosetta_path, iteration_directory, structure_name, i) for i in residues)

consensus_assignment(rosetta_path, iteration_directory, structure_name, k)

start_model = '%s/%s_round_%s.pdb' % (os.getcwd(), structure_name, k)

print('Time for round #%s is: ' %k, datetime.now() - startTime)






python






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 23 at 2:34







ahmadkhalifa

















asked Mar 21 at 18:32









ahmadkhalifaahmadkhalifa

124




124







  • 1





    In Python indentation is a critical part of valid syntax. Please fix your indentation so your intent is clear.

    – lurker
    Mar 21 at 18:34






  • 2





    Do_2 and Do_3 can just be done after the if else block and unintended, such that they are always executed.

    – Paritosh Singh
    Mar 21 at 18:35











  • You only pass x if it's the same as arg, so you don't need an if, just always pass arg.

    – Blorgbeard
    Mar 21 at 18:35












  • 1





    In Python indentation is a critical part of valid syntax. Please fix your indentation so your intent is clear.

    – lurker
    Mar 21 at 18:34






  • 2





    Do_2 and Do_3 can just be done after the if else block and unintended, such that they are always executed.

    – Paritosh Singh
    Mar 21 at 18:35











  • You only pass x if it's the same as arg, so you don't need an if, just always pass arg.

    – Blorgbeard
    Mar 21 at 18:35







1




1





In Python indentation is a critical part of valid syntax. Please fix your indentation so your intent is clear.

– lurker
Mar 21 at 18:34





In Python indentation is a critical part of valid syntax. Please fix your indentation so your intent is clear.

– lurker
Mar 21 at 18:34




2




2





Do_2 and Do_3 can just be done after the if else block and unintended, such that they are always executed.

– Paritosh Singh
Mar 21 at 18:35





Do_2 and Do_3 can just be done after the if else block and unintended, such that they are always executed.

– Paritosh Singh
Mar 21 at 18:35













You only pass x if it's the same as arg, so you don't need an if, just always pass arg.

– Blorgbeard
Mar 21 at 18:35





You only pass x if it's the same as arg, so you don't need an if, just always pass arg.

– Blorgbeard
Mar 21 at 18:35












3 Answers
3






active

oldest

votes


















2














Seems



Do_1(arg)
Do_2()
Do_3()


will get executed regardless of your if clause, because regardless of arg value you're passing it to Do_1() method.






share|improve this answer























  • Please take a look at the actual code to know what I mean. I apologize for being vague with the question.

    – ahmadkhalifa
    Mar 23 at 2:35


















1














Although the two if/else cases are equivalent, this seems to be what you are after:



if arg is x:
Do_1(x)
else:
Do_1(arg)
Do_2
Do_3





share|improve this answer






























    0














    Or you could make the parameter value of Do_1 conditional:



    Do_1(x if arg is x else arg)
    Do_2()
    Do_3()





    share|improve this answer























      Your Answer






      StackExchange.ifUsing("editor", function ()
      StackExchange.using("externalEditor", function ()
      StackExchange.using("snippets", function ()
      StackExchange.snippets.init();
      );
      );
      , "code-snippets");

      StackExchange.ready(function()
      var channelOptions =
      tags: "".split(" "),
      id: "1"
      ;
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function()
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled)
      StackExchange.using("snippets", function()
      createEditor();
      );

      else
      createEditor();

      );

      function createEditor()
      StackExchange.prepareEditor(
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader:
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      ,
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      );



      );













      draft saved

      draft discarded


















      StackExchange.ready(
      function ()
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55287065%2fhow-to-avoid-repetitive-code-block-of-an-if-conditional%23new-answer', 'question_page');

      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      2














      Seems



      Do_1(arg)
      Do_2()
      Do_3()


      will get executed regardless of your if clause, because regardless of arg value you're passing it to Do_1() method.






      share|improve this answer























      • Please take a look at the actual code to know what I mean. I apologize for being vague with the question.

        – ahmadkhalifa
        Mar 23 at 2:35















      2














      Seems



      Do_1(arg)
      Do_2()
      Do_3()


      will get executed regardless of your if clause, because regardless of arg value you're passing it to Do_1() method.






      share|improve this answer























      • Please take a look at the actual code to know what I mean. I apologize for being vague with the question.

        – ahmadkhalifa
        Mar 23 at 2:35













      2












      2








      2







      Seems



      Do_1(arg)
      Do_2()
      Do_3()


      will get executed regardless of your if clause, because regardless of arg value you're passing it to Do_1() method.






      share|improve this answer













      Seems



      Do_1(arg)
      Do_2()
      Do_3()


      will get executed regardless of your if clause, because regardless of arg value you're passing it to Do_1() method.







      share|improve this answer












      share|improve this answer



      share|improve this answer










      answered Mar 21 at 18:40









      GiollaGiolla

      336




      336












      • Please take a look at the actual code to know what I mean. I apologize for being vague with the question.

        – ahmadkhalifa
        Mar 23 at 2:35

















      • Please take a look at the actual code to know what I mean. I apologize for being vague with the question.

        – ahmadkhalifa
        Mar 23 at 2:35
















      Please take a look at the actual code to know what I mean. I apologize for being vague with the question.

      – ahmadkhalifa
      Mar 23 at 2:35





      Please take a look at the actual code to know what I mean. I apologize for being vague with the question.

      – ahmadkhalifa
      Mar 23 at 2:35













      1














      Although the two if/else cases are equivalent, this seems to be what you are after:



      if arg is x:
      Do_1(x)
      else:
      Do_1(arg)
      Do_2
      Do_3





      share|improve this answer



























        1














        Although the two if/else cases are equivalent, this seems to be what you are after:



        if arg is x:
        Do_1(x)
        else:
        Do_1(arg)
        Do_2
        Do_3





        share|improve this answer

























          1












          1








          1







          Although the two if/else cases are equivalent, this seems to be what you are after:



          if arg is x:
          Do_1(x)
          else:
          Do_1(arg)
          Do_2
          Do_3





          share|improve this answer













          Although the two if/else cases are equivalent, this seems to be what you are after:



          if arg is x:
          Do_1(x)
          else:
          Do_1(arg)
          Do_2
          Do_3






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Mar 21 at 18:42









          NathanielNathaniel

          2,210214




          2,210214





















              0














              Or you could make the parameter value of Do_1 conditional:



              Do_1(x if arg is x else arg)
              Do_2()
              Do_3()





              share|improve this answer



























                0














                Or you could make the parameter value of Do_1 conditional:



                Do_1(x if arg is x else arg)
                Do_2()
                Do_3()





                share|improve this answer

























                  0












                  0








                  0







                  Or you could make the parameter value of Do_1 conditional:



                  Do_1(x if arg is x else arg)
                  Do_2()
                  Do_3()





                  share|improve this answer













                  Or you could make the parameter value of Do_1 conditional:



                  Do_1(x if arg is x else arg)
                  Do_2()
                  Do_3()






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Mar 21 at 22:05









                  Alain T.Alain T.

                  8,82811429




                  8,82811429



























                      draft saved

                      draft discarded
















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid


                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.

                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55287065%2fhow-to-avoid-repetitive-code-block-of-an-if-conditional%23new-answer', 'question_page');

                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

                      SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

                      은진 송씨 목차 역사 본관 분파 인물 조선 왕실과의 인척 관계 집성촌 항렬자 인구 같이 보기 각주 둘러보기 메뉴은진 송씨세종실록 149권, 지리지 충청도 공주목 은진현