lundi 26 juin 2017

Merge code of same operation with two different conditions

I have a question. I'm trying to tidy up some coding. I have this part of code

        if (self.type == self.BETAECM):
        for line in contentInfo:
            if line.startswith("caid:"):
                caid = self.readEcmInfo(line)
                if "x" in caid:
                    idx = caid.index("x")
                    caid = caid[(idx + 1):]
                    caid = caid[:4]
                    caid = caid.upper()
                    if (caid >= "1700") and (caid <= "17FF"):
                        return True
            elif line.startswith("====="):
                caid = self.readCaid(line)                        
                if "x" in caid:
                    idx = caid.index("x")
                    caid = caid[(idx + 1):]
                    caid = caid[:4]
                    caid = caid.upper()
                    if (caid >= "1700") and (caid <= "17FF"):
                        return True
        return False

As you can see there are two different conditions that are handled with the same operation I want to tidy up everything so that the identical parts of the code (what starts with if "x" in caid) are only written once. Is this possible? Thanks in advance!

Aucun commentaire:

Enregistrer un commentaire