括号内的表达式是什么意思?

时间:2019-05-27 21:41:08

标签: python

我不明白这段代码在说什么。

expectedContent = content[offset + readOffset : offset + readOffset + readSize]

这部分代码是测试人员的一部分,它可以自动更正我的作业,并且在此功能之内

 def test_read_section(self, fname):
        score = 0

        subprocess.call(["ipcrm", "shm", self.data["shm_key"]], stderr=open(os.devnull, "w"))
        self.writeString("CREATE_SHM")
        self.writeNumber(int(self.data["shm_size"]))
        r = self.readString()
        if r != "CREATE_SHM":
            return score
        r = self.readString()
        if r != "SUCCESS":
            return score
        # check if the shm actually exists
        shm = self.shmget(int(self.data["shm_key"]), int(self.data["shm_size"]), 0)
        if shm < 0:
            if VERBOSE:
                print "[TESTER] shm with key %s not found" % self.data["shm_key"]
            return score
        shAddr = self.shmat(shm, 0, 0)
        score = 1

        self.writeString("MAP_FILE")
        self.writeString(fname)
        r = self.readString()
        if r != "MAP_FILE":
            return score
        r = self.readString()
        if r != "SUCCESS":
            return score
        score = 2

        sections = getSectionsTable(self.data, fname)
        self.writeString("READ_FROM_FILE_SECTION")
        self.writeNumber(len(sections)+2)
        self.writeNumber(0)
        self.writeNumber(100)
        r = self.readString()
        if r != "READ_FROM_FILE_SECTION":
            return score
        r = self.readString()
        if r != "ERROR":
            return score
        score = 4                                   

        fin = open(fname, "rb")
        content = fin.read()
        fin.close()

        sectIds = random.sample(range(len(sections)), 3)
        for sectId in sectIds:
            _name, _type, offset, size = sections[sectId]
            readOffset = random.randint(0, size/2)
            readSize = random.randint(5, size/2)
            expectedContent = content[offset + readOffset : offset + readOffset + readSize]
            self.writeString("READ_FROM_FILE_SECTION")
            self.writeNumber(sectId+1)
            self.writeNumber(readOffset)
            self.writeNumber(readSize)
            r = self.readString()
            if r != "READ_FROM_FILE_SECTION":
                print "!!!!!%d " % (sectId+1)
                return score
            r = self.readString()
            if r != "SUCCESS":
                print "[ANDREI] sectid:%d, readOffset: %d, readSize: %d" % (sectId+1,readOffset,readSize)
                return score
            readContent = ctypes.string_at(shAddr, readSize)
            if readContent != expectedContent:
                if VERBOSE:
                    print "[TESTER] read content incorrect"
            else:
                score += 2
        return score

2 个答案:

答案 0 :(得分:0)

array[x : y]是一个子数组,其中包含索引x和索引y - 1之间的所有元素。

答案 1 :(得分:0)

实际上这不是一个操作,而是列表'content'的截断,从索引:(offset + readOffset)开始,以inex:(offset + readOffset + readSize)结尾