Home > OS >  How to combine two methods for uploading file?
How to combine two methods for uploading file?

Time:10-26

I have a Django application

And I have a upload functionality. And I have two methods that shows the extracted text:

  def filter_verdi_total_number_fruit(self, file_name):
        self.extractingText.extract_text_from_image(file_name)
        regex = r"(\d*(?:\.\d )*)\s*\W (?:"   '|'.join(re.escape(word)
                                                       for word in self.extractingText.list_fruit)   ')'
        return re.findall(regex, self.extractingText.text_factuur_verdi[0])

    def filter_verdi_fruit_name(self, file_name):
        self.extractingText.extract_text_from_image(file_name)
        regex = r"(?:\d*(?:\.\d )*)\s*\W ("   '|'.join(re.escape(word)
                                                       for word in self.extractingText.list_fruit)   ')'
        return re.findall(regex, self.extractingText.text_factuur_verdi[0])

But as you can see. There are some duplicate code. Like:

file_name 

and:

re.findall(regex, self.extractingText.text_factuur_verdi[0])

So I try to combine this two methods in one method:

 def combine_methods(self,  file_name):
        self.filter_verdi_total_number_fruit(file_name) 
        self.filter_verdi_fruit_name(file_name)

and then I try to call the combined method in the views.py:

  if uploadfile.image.path.endswith('.pdf'):
                    content ='\n'.join(filter_text.combine_methods(uploadfile.image.path))  

But then I get this error:

can only join an iterable
Exception Location:     C:\Users\engel\Documents\NVWA\software\blockchainfruit\main\views.py, line 50, in post
Raised during:  main.views.ReadingFile

Question: how can I change this?

CodePudding user response:

I think you have forgotten return statement in combine_methods function, so it returns None, which cannot be "joined" in content='\n'.join(filter_text.combine_methods(uploadfile.image.path))

  • Related