-1

I don't understand what this code is trying to say.

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

This portion of code is part of a tester, that automatically corrects my homework assignment, and it's inside this function

 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
motiondev
  • 41
  • 5
  • It's a list slice. Your start-point comes from the end of the previous slice, and you're reading the chunk size on each iteration. – roganjosh May 27 '19 at 21:43
  • If `x` is a list, then `x[a:b]` is a list consisting of elements `x[a]` through `x[b-1]`. Note that `x[b]` is excluded. – Tom Karzes May 27 '19 at 21:45

2 Answers2

0

array[x : y] is a sub array which contains all elements between index x and index y - 1.

goodvibration
  • 5,980
  • 4
  • 28
  • 61
0

Actually this is not an operation, but a truncation of the list 'content', starting from index: (offset + readOffset), and ending at inex: (offset + readOffset + readSize)

Nadhem Maaloul
  • 433
  • 5
  • 11