聚档结果解析样例(Python)

import struct
import ArchiveResult_pb2 as ArchiveResMsg

class Archives:
    def __init__(self):
        self.archives = []
        self.unarchived_features = []

    def report(self):
        print("{} clusters archived, top 10 are :".format(len(self.archives)))
        for k, i in enumerate(self.archives[:10]):
             print("    NUM {} -- {}".format(k+1, i))
        print("{} features are classified as isolated points".format(len(self.unarchived_features)))

    def parse(self, filepath):
        r = ArchiveResMsg.ArchiveResultMessage()

        # Parse from protobuf serialized result
        filestream = open(filepath,  "rb")
        r.ParseFromString(filestream.read())
        filestream.close()

        size_of_cluster = struct.unpack(str(r.numClusters)+'I', bytearray(r.sizeOfClusters))
        unarchive_idx = struct.unpack(str(r.numUnarchivedIdx)+'Q', bytearray(r.unarchivedIdx))
        archive_idx = struct.unpack(str(r.numArchivedIdx)+'Q', bytearray(r.archivedIdx))
        for i in unarchive_idx:
            self.unarchived_features.append(i)
        offset = 0
        for i in size_of_cluster:
            self.archives.append(archive_idx[offset:offset+i])
            offset+=i