コード例 #1
0
def read_record():
    from google.protobuf.json_format import MessageToJson
    #filename, need_new_file = find_latest_file()
    filename = FILE_PREFIX + '0000' + FILE_EXT
    need_new_file = False
    records = records_pb2.Records()
    print('Decompressing, opening gzip file.')
    with gzip.open(filename, 'rb') as f:
        print('Parsing from gzip file.')
        records.ParseFromString(f.read())
    record_counter = 0
    while record_counter < 3:
        station_counter = 0
        while station_counter < 3:
            print(records.record[record_counter].stations.
                  station[station_counter])
            station_counter += 1
        record_counter += 1
    """print('Converting protocol buffer message to JSON.')
    data_raw = MessageToJson(records)
    print('Loading to string.')
    data_json = json.loads(data_raw)
    with open('test.json', 'w+') as f:
        print('Writing to file.')
        f.write(json.dumps(data_json['record']))"""
    return ('Done')
コード例 #2
0
def add_record():
    filename, need_new_file = find_latest_file()
    records = records_pb2.Records()
    if not (need_new_file):
        with gzip.open(filename, 'rb') as f:
            records.ParseFromString(f.read())
    record = records.record.add()
    gen_record(record)
    return records, filename
コード例 #3
0
def read_record():
    from google.protobuf.json_format import MessageToJson
    filename, need_new_file = find_latest_file()
    records = records_pb2.Records()
    with gzip.open(filename, 'rb') as f:
        records.ParseFromString(f.read())
    data_raw = MessageToJson(records)
    data_json = json.loads(data_raw)
    with open('test.json', 'w+') as f:
        f.write(json.dumps(data_json['record']))
    return ('Done')