开发者

How to rewrite the code more elegantly [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.

This question does not appear to be about programming within the s开发者_JS百科cope defined in the help center.

Closed 6 years ago.

Improve this question

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.


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])


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)]


'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])


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.


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


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])
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜