def format_script(sensor_graph):
    records = []
    records.append(SetGraphOnlineRecord(False, address=8))
    records.append(ClearDataRecord(address=8))
    records.append(ResetGraphRecord(address=8))
    for node in sensor_graph.nodes:
        records.append(AddNodeRecord(str(node), address=8))
    for streamer in sensor_graph.streamers:
        records.append(AddStreamerRecord(streamer, address=8))
    for stream, value in sorted(sensor_graph.constant_database.items(), key=lambda x: x[0].encode()):
        records.append(SetConstantRecord(stream, value, address=8))
    records.append(PersistGraphRecord(address=8))
    records.append(ClearConfigVariablesRecord())
    for slot in sorted(sensor_graph.config_database, key=lambda x: x.encode()):
        for config_id in sorted(sensor_graph.config_database[slot]):
            config_type, value = sensor_graph.config_database[slot][config_id]
            byte_value = _convert_to_bytes(config_type, value)
            records.append(SetConfigRecord(slot, config_id, byte_value))
    app_tag = sensor_graph.metadata_database.get('app_tag')
    app_version = sensor_graph.metadata_database.get('app_version')
    if app_tag is not None:
        records.append(SetDeviceTagRecord(app_tag=app_tag, app_version=app_version))
    script = UpdateScript(records)
    return script.encode()