RoelandKeuren1989
RoelandKeuren1989

Reputation: 1

How can I get a decreasing index from a python object when called?

For a program that searches the content of the latest report (exact report defined in program) I was looking to get a way to walk through the reports until I got a report that matches the scenario it was run on. Using -1 would only help in specific cases. The reports are grouped together in a folder, and transferred to a list in my program. As there are many reports (up to 99) I would like to automate

So far I've tried a few thing, but the most "fruitful" results where:

class IndexGenerator1:
    def __init__(self):
        self.index = -1
        
    def __call__(self):
        return self.index
    
    def reduceIndex(self):
        self.index -= 1
        
    def returnIndex(self):
        return(self.index)

Calling the class:

    def getReportPath(self):
        if self.machineOrModule == 'machine':
            mostRecentDir = None
            mostRecentTime = 0
            for entry in os.scandir(self.machineDirectory):
                if entry.is_dir():
                    mod_time = entry.stat().st_mtime_ns
                    if mod_time > mostRecentTime:
                        mostRecentDir = entry.name
            intermediatePath = (not allowed to share this string :))
            
        scenario = self.scenario                
        file = glob.glob(intermediatePath)
        files = file.sort(key=os.path.getmtime)
        oIndexGenerator = IndexGenerator1  
        self.index = oIndexGenerator.returnIndex()
 
        with open(files[self.index]) as f:
            if self.scenario not in f.read: 
                self.index = oIndexGenerator.reduceIndex(self)  
            else:
                with open(files[index]) as f:
                    for line in f.read():
                        reportPath = os.path.abspath(f.name)
                        return reportPath

With this code I got:

Traceback (most recent call last):
  File "...\python\MIRT\OOP\reportpathasclass\testReportPath.py", line 53, in <module>
    searchInfo()
  File "...\python\MIRT\OOP\reportpathasclass\testReportPath.py", line 34, in searchInfo
    print(oTestReportFile.getReportPath())
  File "...\python\MIRT\OOP\reportpathasclass\TestNameReportFile.py", line 96, in getReportPath
    self.index = oIndexGenerator.returnIndex(self)
  File "....\python\MIRT\OOP\reportpathasclass\IndexGenerator1.py", line 12, in returnIndex
    return(self.index)
AttributeError: 'TestNameReportFile' object has no attribute 'index'

When I run the program for a report that has only a single scenario, the searchInfo and getReportPath work (the code as displayed differs). My assumption is that the IndexGenerator in combination with the TestNameReportFile parts are not working together as intended.

As I am relatively new in python and programming overall, I am open for suggestions on either solving my issue or a clear lead on how to find it workable.

Upvotes: 0

Views: 36

Answers (1)

Spatz
Spatz

Reputation: 20118

Instead of

oIndexGenerator = IndexGenerator1

write

oIndexGenerator = IndexGenerator1()

self related to instance, not to class itself.

Upvotes: 1

Related Questions