def METHOD_NAME(self, text_name, data):
    texts = bpy.data.texts.items()
    exists = False
    for t in texts:
        if bpy.data.texts[t[0]].name == text_name:
            exists = True
            break
    if not exists:
        bpy.data.texts.new(text_name)
    bpy.data.texts[text_name].clear()
    bpy.data.texts[text_name].write(data)