mardi 31 janvier 2017

Redundant instructions in two conditions

I want to optimize the following code:

 for myFile in myFiles:
        file = open(filename, 'rt')
        try:
            if CLIENT == "C1":
                head = rows[:7]
                tail = rows[7:]
                for row in rows:
                    if "".join(row)!= "":
                        if not u_pass:
                            header = [ row.strip().replace(" ", "_") for row in row[3:] ]
                            u_pass = True
                        else:
                            self.usecases(row, data, index)

            elif CLIENT == 'C2':
                reader = csv.reader(file)
                firstline = next(reader)
                secondline = next(reader)
            else:
                for row in rows:
                    if "".join(row)!= "":
                        if not u_pass:
                            header = [ row.strip().replace(" ", "_") for row in row[3:] ]
                            u_pass = True
                            # Recuperation des donnees
                        else:
                            self.usecases(row, data, index)

The code below is repeated twice in the previous code, meaning there are some common instructions between these conditions "

for row in rows:

            if "".join(row)!= "":
                if not u_pass:
                    header = [ row.strip().replace(" ", "_") for row in row[3:] ]
                    u_pass = True
                else:
                    self.usecases(row, data, index)

Aucun commentaire:

Enregistrer un commentaire