This hardly warrants anything more than a few quick lines.
This line was bombing out:
bpy.ops.object.modifier_apply(modifier = 'Bla')
Well, the “wrong context” error reared its ugly head. That’s because there was nothing selected in the scene, even though there was an active object.
So, when calling the above operator, ensure that there is a selected, active object in the scene.
bpy.data.scenes.objects.active = some_obj
can save lives.