0

The code below reads lines from a file, then it executes the custom function (My_Function) and return values to the variables(e.g. condition_A)

  for line in input_file:
     if condition_A:
         condition_A = My_Function(A_tuple[0], B_tuple[0])

    if condition_B:
        condition_B = My_Function(A_tuple[1], B_tuple[1])

    if condition_C:
        condition_C = My_Function(A_tuple[2], B_tuple[2])

    if condition_D:
        condition_D = My_Function(A_tuple[3], B_tuple[3])

    if condition_E:
        condition_E = My_Function(A_tuple[4], B_tuple[4])

...

My question is: can the code be modified to more elegant version? After all, many code is similar(I don't want to define another function to simplify it because the code is still similar after the new function is defined). thanks.

phooji
  • 10,086
  • 2
  • 38
  • 45
Matt Elson
  • 4,177
  • 8
  • 31
  • 46
  • 2
    Could you be more specific about what the conditions are? Are you testing the line for equality to various things, or is it more complicated than that? – Zhehao Mao Jun 21 '11 at 17:09
  • 2
    I'm voting to close this question as off-topic because code refactoring should be asked about on Code Review, not Stack Overflow. – TylerH Feb 10 '17 at 15:08

6 Answers6

8

Instead of having 5 variables condition_*, use a list, conditions:

conditions=[1]*5 # initialize conditions as you wish
for line in input_file:
    for i,condition in enumerate(conditions):
        if condition:
            conditions[i]=My_Function(A_tuple[i],B_tuple[i])
unutbu
  • 842,883
  • 184
  • 1,785
  • 1,677
1

What about something like

conditions = [condition_A, condition_B, condition_C, condition_D, condition_E]
condition_test = lambda c, i: My_Function(A_tuple[i], B_tuple[i]) if c else c
for line in input_file:
    conditions = [condition_test(c, i) for i, c in enumerate(conditions)]
senderle
  • 145,869
  • 36
  • 209
  • 233
0

'line' is not referenced in teh loop, is that an error in simplifying it for posting?

How about

condition=1 #or 2 or...
for line in input_file:
   My_Function(A_tuple[condition],B_tuple[condition])
Pete
  • 1,241
  • 2
  • 9
  • 21
0

Before refactoring your code on a purely syntactic level (which is covered in examples above), it might be useful to evaluate what you're doing with the code on a functional level

Check out your condition_x variables. I think you might be using the same variable for two different things (both type-wise and logically) - usually a bad idea in a weakly typed language. It looks to me as if the user sets a condition to true or false, and then that condition is assigned the output - is the output boolean? is it related to the original value of that variable? Rethinking this might lead to more understandable code.

It is also difficult to evaluate how this can be refactored without seeing what goes in to condition_x - since these might have commonalities.

dfb
  • 13,133
  • 2
  • 31
  • 52
0

One more sample(not solution) based on unutbu's:

data = [1,2,3,'',4,5,6, '', 0]
for i in (i for i in xrange(len(data)) if data[i] not in ['',0]):
    data[i] += 1

Sorry if duplicate

Artsiom Rudzenka
  • 27,895
  • 4
  • 34
  • 52
0

Here is a generic solution where you can have custom index and you can also access conditions by name if need be and it can be easily extended to add any new complexities

class Condition(object):
    def __init__(self, active, index1, index2):
        self.active = active
        self.index1 = index1
        self.index2 = index2

conditions = {
    'A': Condition(True,0,0),
    'B': Condition(True,1,1),
    'C': Condition(True,2,2),
    'D': Condition(True,3,3),
    'E': Condition(True,4,4),
}

for line in input_file:
    for condition in conditions.itervalues():
        if condition.active:
            condition.active = My_Function(A_tuple[condition.active.index1], B_tuple[condition.active.index2])
Anurag Uniyal
  • 85,954
  • 40
  • 175
  • 219