Home > Software engineering >  argument 2: <class 'TypeError'>: wrong type
argument 2: <class 'TypeError'>: wrong type

Time:10-18

I have a python Django application.

And I want to filter some sub text from extracted text.

So this is how my views.py looks like:

class ReadingFile(View):
    def get(self, request):
        form = ProfileForm()
        return render(request, "main/create_profile.html", {
            "form": form
        })

    def post(self, request):
        submitted_form = ProfileForm(request.POST, request.FILES)
        content = ''    

        if submitted_form.is_valid():
            uploadfile = UploadFile(image=request.FILES["upload_file"])

            name_of_file = str(request.FILES['upload_file'])
            uploadfile.save()

            with open(os.path.join(settings.MEDIA_ROOT,
                                   f"{uploadfile.image}"), 'r') as f:

                print("Now its type is ", type(name_of_file))
                print(uploadfile.image.path)

                # reading PDF file
                if uploadfile.image.path.endswith('.pdf'):
                    content = ExtractingTextFromFile.filterAnanas(self)
                # ENDING Reading pdf file

                else:
                    with open(os.path.join(settings.MEDIA_ROOT, uploadfile.image.path)) as f:
                        content = f.read()

            return render(request, "main/create_profile.html", {
                'form': ProfileForm(),
                "content": content
            })

        return render(request, "main/create_profile.html", {
            "form": submitted_form,
        })

and this is class: ExtractingTextFromFile.py

class ExtractingTextFromFile:

    def extract_text_from_image(filename):

        text_factuur_verdi = []
        pdf_file = wi(filename=filename, resolution=300)
        all_images = pdf_file.convert('jpeg')

        for image in all_images.sequence:
            image = wi(image=image)
            image = image.make_blob('jpeg')
            image = Image.open(io.BytesIO(image))

            text = pytesseract.image_to_string(image, lang='eng')
            text_factuur_verdi.append(text)

        return  text_factuur_verdi

    def __init__(self ):
        # class variables:
        self.apples_royal_gala = 'Appels Royal Gala 13kg 60/65 Generica PL Klasse I'
        self.ananas_crownless = 'Ananas Crownless 14kg 10 Sweet CR Klasse I'
        self.peen_waspeen = 'Peen Waspeen 14x1lkg 200-400 Generica BE Klasse I'
        self.tex_factuur_verdi = []

    def make_pattern(substr):
        return r"(?<="   substr   r").*?(?P<number>[0-9,.]*)\n"

    def filterAnanas(self):
        self.get_text_from_image()
        return re.findall(self.make_pattern(self.ananas_crownless), self.text_factuur_verdi[0])

and now I just try to call the method:

 def filterAnanas(self):

from the views.py file. So that is this line in views.py:

  content = ExtractingTextFromFile.filterAnanas(self)

But then I get this error:

'ReadingFile' object has no attribute 'get_text_from_image'

My question: how can I call the method: filterAnanas from the views.py file?

Thank you

So I have it now like this:

def filterAnanas(self):
        self.extract_text_from_image()
        return re.findall(self.make_pattern(self.ananas_crownless), self.text_factuur_verdi[0])

and in views.py:

extract_instance = ExtractingTextFromFile()
 content = extract_instance.filterAnanas()

Then I get this error:

ArgumentError at /

argument 2: <class 'TypeError'>: wrong type

I have it:


def filterAnanas(self, file_name):
        self.extract_text_from_image(file_name)
        return re.findall(self.make_pattern(self.ananas_crownless), self.text_factuur_verdi[0])

then I get this:

ExtractingTextFromFile.extract_text_from_image() takes 1 positional argument but 2 were given

CodePudding user response:

You need to initialize a class before calling its methods.

import ExtractingTextFromFile # You need to make sure the class properly imported

class ReadingFile(View):
    def get(self, request):
        ... your code...

    def post(self, request):
        extract_instance = ExtractingTextFromFile() # Initialise the class here
        ... your code...

        return_value = extract_instance.filterAnanas(file_name) # Call method here

Since you are not not doing any inheritance for ExtractingTextFromFile class make sure that there will be a get_text_from_image method.

class ExtractingTextFromFile:

    def extract_text_from_image(self, filename):
        self.text_factuur_verdi = []
        ... your code ...
        # no need to return from this method
  
    def filterAnanas(self, file_name):
        self.extract_text_from_image(file_name)
        return re.findall(self.make_pattern(self.ananas_crownless), self.text_factuur_verdi[0])

I can't see the declaration of get_text_from_image() method, make sure that there will be method.

CodePudding user response:

When you want to call method from class you should not use self

self using inside a class to pass the object as an instance you can call it in this way

content = ExtractingTextFromFile.filterAnanas()
  • Related