<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def patch():
 """ Patch startService and stopService so that they check the previous state first. (used for debugging only) """

    from twisted.application.service import Service
    old_startService = Service.startService
    old_stopService = Service.stopService

    def startService(self):
        assert not self.running, "%r already running" % (self,)
        return old_startService(self)

    def stopService(self):
        assert self.running, "%r already stopped" % (self,)
        return old_stopService(self)
    Service.startService = startService
    Service.stopService = stopService