Python Forum
NameError when calling a class method
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
NameError when calling a class method
#1
Hi,

I'm currently starting to learn python (have already programming experience with c#).

For my first project i wanted to write a script, that sorts my camera images. The idea is to create a task list, that contains a list of image file paths, that either have or don't have exif tags.

For the first part, i have written a class, that creates a csv file with the file path and a boolean flag (has exif or not). Somehow i'm not able to successfully execute my method called 'create_tasklist'. I'm getting an error, that another method 'get_filtered_images' in that class cannot be found.

From my point of view it looks good, but still i'm getting this error. Can you take a look at the following class?:

from os import listdir, path, rename
import csv
import exifread
import datetime
from Settings import Settings

class TaskGenerator:

    def __init__(self, settings: Settings):
        self.settings = settings

    def get_task_line(self, img, image):
        tags = exifread.process_file(img)
        if tags:
            line = image + ";True"
        else:
            line = image + ";False"
        return line


    def import_exif_images(self, taskfile_relpath):
        isExif = "True"
        return import_taskfile(taskfile_relpath, isExif)


    def import_nonexif_images(self, taskfile_relpath):
        isExif = "False"
        return import_taskfile(taskfile_relpath, isExif)


    def import_taskfile(self, taskfile_relpath, isExifValue):
        current_base_path = path.dirname(path.abspath(__file__))
        taskfile_abspath = path.join(current_base_path, taskfile_relpath)

        images = []
        with open(taskfile_abspath, "r") as tasklist:
            csv_reader = csv.reader(tasklist, delimiter=';')
            # skip header line
            next(csv_reader)
            for line in csv_reader:
                if not line or len(line) < 2:
                    continue #  skip line if empty

                image_path = line[0]
                isExif = str(line[1].strip('\''))
                if isExif == isExifValue:
                    images.append(image_path.strip())

        return images

    def get_filtered_images(self):
        files = listdir(self.settings.base_path)
        images = []
        for file in files:
            abspath = path.join(self.settings.base_path, file)
            extension_array = path.splitext(abspath)
            
            extension = ""
            if extension_array and len(extension_array) > 0:
                extension = extension_array[1]
            
            if extension != "" and extension in self.settings.image_types:
                images.append(abspath)
        return images

    def create_tasklist(self, tasklist_filename):
        images = get_filtered_images(self)
        numberOfImages = len(images)

        taskfile_content = ["file;hasexif"]
        for image in images:
            with open(image, "rb") as img:
                imageIndex = images.index(image, 0, numberOfImages)
                print("processing image " + str(imageIndex) + "/" + str(numberOfImages) + "(" + image + ")")
                
                line = get_task_line(self, img, image)
                
                taskfile_content.append(line)

        current_base_path = path.dirname(path.abspath(__file__))
        taskfile_abspath = path.join(current_base_path, tasklist_filename)

        with open(taskfile_abspath, "w") as taskfile:
            print("writing task file: " + taskfile_abspath)
            taskfile_content = "\n".join(taskfile_content)
            taskfile.write(taskfile_content)
This is how i instantiate the class, and call the method:

from TaskGenerator import TaskGenerator
from Settings import Settings

base_path = r"C:\path\to\my\pictures"
copy_only = False
datetime_format_string = "%Y-%m-%d_%H-%M-%S"
exif_tag_name = "EXIF DateTimeOriginal"
image_types = [".jpg", ".png", ".JPG", ".PNG"]
settings = Settings(base_path, copy_only, datetime_format_string, exif_tag_name, image_types)

taskGenerator = TaskGenerator(settings)
taskGenerator.create_tasklist("task-list.csv")
Thats the error I'm getting:

Error:
Exception has occurred: NameError name 'get_filtered_images' is not defined File "D:\python-stuff\exifreader\TaskGenerator.py", line 67, in create_tasklist images = get_filtered_images(self) File "D:\python-stuff\exifreader\do-work.py", line 14, in <module> taskGenerator.create_tasklist("task-list.csv")
I'm not sure if i made an indentation error, therefore i posted the whole class. Otherwise I'd posted an simplified example.

Do you have an idea, what could be wrong?

Thanks in advance,

Michael
Reply
#2
The method is in the class hierarchy, so you have to call it that way, and you can use self to do so:

# not 
images = get_filtered_images(self)

# but instead
images = self.get_filtered_images()
Reply
#3
Oh boy, i would never think of that. I haven't seen this in any tutorials. Well thanks a lot!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  super() and order of running method in class inheritance akbarza 7 594 Feb-04-2024, 09:35 AM
Last Post: Gribouillis
  Using one child class method in another child class garynewport 5 1,484 Jan-11-2023, 06:07 PM
Last Post: garynewport
  PyRun_SimpleFile calling multiprocessing Python Class cause endless init loop Xeno 2 988 Sep-19-2022, 02:32 AM
Last Post: Xeno
  [Solved] Novice question to OOP: can a method of class A access attributes of class B BigMan 1 1,267 Mar-14-2022, 11:21 PM
Last Post: deanhystad
  class, attribute and method Frankduc 9 2,380 Feb-27-2022, 09:07 PM
Last Post: deanhystad
  Subclass initialized property used in parent class method. Is it bad coding practice? saavedra29 5 1,679 Feb-07-2022, 07:29 PM
Last Post: saavedra29
  Calling a base class variable from an inherited class CompleteNewb 3 1,591 Jan-20-2022, 04:50 AM
Last Post: CompleteNewb
  Calling a class from a function jc4d 5 1,754 Dec-17-2021, 09:04 PM
Last Post: ndc85430
  Calling functions from within a class: PYQT6 Anon_Brown 4 3,643 Dec-09-2021, 12:40 PM
Last Post: deanhystad
  Class Method to Calculate Age Doesn't Work gdbengo 1 1,657 Oct-30-2021, 11:20 PM
Last Post: Yoriz

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020