0

I've a small problem, I have a method in another class that scans a folder and for each file in that folder I need to create new folders according to the date of the files to organize and initiate a new instance, but I'm having trouble on the second part because whatever I try to return it dont seems to work since no new instances are created.

class CPImages:

    def __init__(self, filename):
        self.filename = filename
        self.exif = {}
        self.metadata = {}

    @staticmethod
    def makeCPImage(filename):
        image = CPImages(filename)
        image.loadExif()
        date = photo.getDate()
        if not (os.path.exists(date)):
        #if the folder dont exist, create one and then copy the file
            os.makedirs(date)
            CPImages.copyToFolder(filename, date) 
        else:
        #if the folder exists just copy
            CPImages.copyToFolder(filename, date)
        return CPImages(filename)

Just a bit of more context, the method loadExif() extracts the Exif of the image, the method getDate() turns the date into an usefull format. Here you have the code for searching in the folders

class ImageCollection:

    #Here I search in all folders to extract all files
    def allFiles(folder, fileslist=[], extension='*.jpg*'):
    for obj in os.scandir(folder):
        if obj.is_dir():
            ImageCollection.allFiles(obj)
        if obj.is_file:
            if fnmatch.fnmatch(obj, extension):
                fileslist.append(os.path.join(obj))
            else:
                pass
    return fileslist
    
    #Here I use the files to pass to makeCPImages
    def scanFolder(folder):
    for i in ImageCollection.allFiles(folder):
        CPImages.makeCPImage(i)

ImageCollection.scanFolder('somePath') 
  • _"whatever I try to return it dont seems to work since no new instances are created"_ what happens? The code looks like it returns a new instance here `return CPImages(filename)`. Is there an error? – Anentropic May 30 '22 at 10:42
  • @Anentropic no, it runs with no problem, simply no instance is created at all – Pedro Almeida May 30 '22 at 10:45
  • that makes no sense... show the code you are executing – Anentropic May 30 '22 at 10:46
  • @Anentropic just edited and added the rest of the code – Pedro Almeida May 30 '22 at 11:00
  • what do you expect to happen? you are not capturing the return value of the staticmethod when you call it – Anentropic May 30 '22 at 11:01
  • I intend to have a new instance created for each file, I've tried both capturing the return in another method and calling directly the method in the makeCPImage return but none works. Could you guide me a little bit on how to create an instance from this point ? – Pedro Almeida May 30 '22 at 11:12
  • you are creating a new instance, you're just not doing anything with it – Anentropic May 30 '22 at 11:13
  • But why does when I try to test shows me none ? I run `for obj in gc.get_objects(): if isinstance(obj, CPImages): print(obj.__dict__)` – Pedro Almeida May 30 '22 at 11:20
  • why are you using `gc.get_objects()`? but I expect the reason will be that the instance is immediately garbage collected because you don't assign it to a variable – Anentropic May 30 '22 at 11:24
  • Cause later I need to save all instances to a .json, the print is only there to help me see if there is actually any instance. Okay, I see, and is there any way to automatically assign these new instances to variables, cause I really need to keep them – Pedro Almeida May 30 '22 at 11:41
  • think it through... what are you going to put into the json file? A list of instances? So you need to store your instances into a list that you can later covert to json. So before the for loop you could initialise an empty list `images = []` and then in the loop you can `images.append(CPImages.makeCPImage(i))` – Anentropic May 30 '22 at 11:50

1 Answers1

0

Use @classmethod, instead of @staticmethod.

you can refer to this answer. for further info.

Jay Dee
  • 51
  • 3
  • I've tried this and I still cant create an instance, but I appreciate the help! – Pedro Almeida May 30 '22 at 11:15
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community May 30 '22 at 12:15