如何在调用时从 python 对象中获取递减索引?

问题描述 投票:0回答:1

对于搜索最新报告内容的程序(程序中定义的精确报告),我希望找到一种方法来浏览报告,直到获得与其运行场景相匹配的报告。 使用 -1 仅在特定情况下有帮助。这些报告被分组在一个文件夹中,并传输到我的程序中的列表中。 由于有很多报告(最多 99 个),我想自动化

到目前为止,我已经尝试了一些方法,但最“富有成效”的结果是:

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)

呼叫班级:

    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

用这个代码我得到:

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'

当我为只有一个场景的报告运行程序时,searchInfo 和 getReportPath 可以工作(显示的代码不同)。我的假设是 IndexGenerator 与 TestNameReportFile 部分的结合没有按预期一起工作。

由于我在 python 和编程方面相对较新,因此我愿意寻求有关解决我的问题的建议或关于如何找到可行的明确线索。

python indexing search-engine
1个回答
0
投票

而不是

oIndexGenerator = IndexGenerator1

oIndexGenerator = IndexGenerator1()

self
与实例相关,与类本身无关/

最新问题
© www.soinside.com 2019 - 2024. All rights reserved.