Any solution to duplication of function arguments in Python?

Viewed 46

I was wondering whether I can avoid using the same arguments when using a function within another one. I will give you a chunk of code to make it clear:

def calculateQC(self,dic_symbols:dict) -> float:

        str_quality = self.quality
        length_seq = len(str_quality)
        num_qc = 0

        for symbol in str_quality:
            num_value = dic_symbols[symbol]
            num_qc += num_value

        mean_qc = num_qc/length_seq
        return(mean_qc)    


def FastqtoFasta(self,min_qual:int,dic_symbols:dict) -> FastaFile:
        header,sequence,qualc = self.info,self.seq,self.quality
        qualc = self.calculateQC(dic_symbols)

        if qualc >= min_qual:
            print(">%s\n%s" %(header,sequence))

As you can see, the function FastqtoFasta includes another function called calculateQC, which requires a dictionary as an argument. As a consequence, I must also provide the FastqtoFasta with the dictionary in order to work it out or otherwise I would not be able to use calculateQC (error).

Is there a more straightforward solution to this or is this just the only way?

Thanks.

1 Answers

Suggest passing arguements via **kwargs (dictionary) if you want loose sharing of args across a stack of functions.

def calculateQC(self, **kwargs) -> float:
   dic_symbols = kwargs.get('symbols')
   ## do stuff

def FastqtoFasta(self, **kwargs) -> FastaFile:
    min_qual = kwargs['min']
    qualc = self.calculateQC(**kwargs)
    ## do stuff

To call the function:

 myObj.FastqtoFasta( min=#, symbols= {...})
Related