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;
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
add a comment |
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
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 theif else
block and unintended, such that they are always executed.
– Paritosh Singh
Mar 21 at 18:35
You only passx
if it's the same asarg
, so you don't need anif
, just always passarg
.
– Blorgbeard
Mar 21 at 18:35
add a comment |
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
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
python
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 theif else
block and unintended, such that they are always executed.
– Paritosh Singh
Mar 21 at 18:35
You only passx
if it's the same asarg
, so you don't need anif
, just always passarg
.
– Blorgbeard
Mar 21 at 18:35
add a comment |
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 theif else
block and unintended, such that they are always executed.
– Paritosh Singh
Mar 21 at 18:35
You only passx
if it's the same asarg
, so you don't need anif
, just always passarg
.
– 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
add a comment |
3 Answers
3
active
oldest
votes
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.
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
add a comment |
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
add a comment |
Or you could make the parameter value of Do_1 conditional:
Do_1(x if arg is x else arg)
Do_2()
Do_3()
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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.
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
add a comment |
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.
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
add a comment |
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.
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.
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
add a comment |
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
add a comment |
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
add a comment |
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
add a comment |
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
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
answered Mar 21 at 18:42
NathanielNathaniel
2,210214
2,210214
add a comment |
add a comment |
Or you could make the parameter value of Do_1 conditional:
Do_1(x if arg is x else arg)
Do_2()
Do_3()
add a comment |
Or you could make the parameter value of Do_1 conditional:
Do_1(x if arg is x else arg)
Do_2()
Do_3()
add a comment |
Or you could make the parameter value of Do_1 conditional:
Do_1(x if arg is x else arg)
Do_2()
Do_3()
Or you could make the parameter value of Do_1 conditional:
Do_1(x if arg is x else arg)
Do_2()
Do_3()
answered Mar 21 at 22:05
Alain T.Alain T.
8,82811429
8,82811429
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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 asarg
, so you don't need anif
, just always passarg
.– Blorgbeard
Mar 21 at 18:35