2a437760c9344a20a5785f5b4706950c911534b6,conceptnet5/formats/sql.py,EdgeIndexReader,random,#EdgeIndexReader#,270

Before Change



    def random(self):
        filenum = random.randrange(0, self.nshards)
        fileobj = self.get_file(filenum)
        byte_offset = random.randrange(0, self.file_sizes[filenum])
        fileobj.seek(byte_offset)

        // This is kind of an evil hack. We"ve seeked to a random place in the
        // file, which is probably in the middle of an edge. We"ll find the
        // start of the edge by seeking backwards until we find a byte 0x8D,
        // then try to decode the dictionary that begins there.
        //
        // We have to allow the result to fail sometimes, as Unicode strings
        // will also contain byte 8D, and it turns out we have a lot of those.

        while True:
            try:
                curbyte = fileobj.read(1)
                while curbyte != b"\x8d":
                    byte_offset -= 1
                    fileobj.seek(byte_offset)
                    curbyte = fileobj.read(1)
                fileobj.seek(byte_offset)

                unpacker = Unpacker(fileobj, encoding=encoding)
                result = unpacker.unpack()
                if len(result) == 13 and "uri" in result:
                    return result
                else:
                    byte_offset -= 2
            except (ValueError, TypeError):
                byte_offset -= 2

After Change


        offset = random.randrange(0, 100)
        rows = []
        while not rows:
            c.execute(
                "SELECT filenum, offset from text_index "
                "WHERE queryhash >= ? "
                "ORDER BY queryhash LIMIT 1 OFFSET ?",
                (hashval, offset)
            )
            rows = c.fetchall()

        filenum, offset = rows[0]
        return self.get_edge(filenum, offset)
Italian Trulli
In pattern: SUPERPATTERN

Frequency: 3

Non-data size: 5

Instances


Project Name: commonsense/conceptnet5
Commit Name: 2a437760c9344a20a5785f5b4706950c911534b6
Time: 2015-04-08
Author: rob@luminoso.com
File Name: conceptnet5/formats/sql.py
Class Name: EdgeIndexReader
Method Name: random


Project Name: pantsbuild/pants
Commit Name: dbf744f0af02efeab9fefc708292786385aec030
Time: 2020-10-08
Author: greg.shuflin@toolchain.com
File Name: src/python/pants/bin/remote_pants_runner.py
Class Name: RemotePantsRunner
Method Name: _connect_and_execute


Project Name: recipy/recipy
Commit Name: 27aebd9429cd624a0dada25b9685ffc7af444919
Time: 2016-09-22
Author: michaelj@epcc.ed.ac.uk
File Name: integration_test/process.py
Class Name:
Method Name: execute_and_capture