One alternative is to use a Driver: https://www.blender.org/manual/animation/drivers.html .
If you are using nodes for your lamp then you probably want to put the driver on your strength property.

When you configure a driver to use a script, you have to tell blender you trust the script. This usually means you have to save the file and reload it with driver scripts enabled (this often shows up as a button in the top bar when blender notices an untrusted script in a driver).
If the script is very deterministic, you might be able to replace it with keyframes. It is possible for a python script to compute keyframes.
Consider this excerpt from http://web.purplefrog.com/~thoth/blender/python-cookbook/animate-cycles-lamp-strength.html:
import bpy def mission1(obj): lamp = obj.data e_node = lamp.node_tree.nodes["Emission"] data_path = 'nodes["Emission"].inputs[1].default_value' for fr in [100, 130, 150, 200, 300]: fr2 = fr+10 e_node.inputs[1].default_value = 20 lamp.node_tree.keyframe_insert(data_path=data_path, frame=fr) e_node.inputs[1].default_value = 100 lamp.node_tree.keyframe_insert(data_path=data_path, frame=fr2) for fc in lamp.node_tree.animation_data.action.fcurves: for kp in fc.keyframe_points: kp.interpolation = 'CONSTANT' mission1(bpy.context.active_object)