human
I found an interesting problem on https://www.codewars.com/kata/58b1ae711fcffa34090000ea:
# Situation
You have been hired by a company making electric garage doors. Accidents with the present product line have resulted in numerous damaged cars, broken limbs and several killed pets. Your mission is to write a safer version of their controller software.

# Specification
We always start with a closed door. The remote control has exactly one button, with the following behaviour.

+ If the door is closed, a push starts opening the door, and vice-versa
+ It takes 5 seconds for the door to open or close completely
+ While the door is moving, one push pauses movement, another push resumes movement in the same direction

In order to make the door safer, it has been equiped with resistance-based obstacle detection. When the door detects an obstacle, it must immediately reverse the direction of movement.

# Input
A string where each character represents one second, with the following possible values.

* ```'.'``` No event
* ```'P'``` Button has been pressed
* ```'O'``` Obstacle has been detected (supersedes P)

As an example, ```'..P....'``` means that nothing happens for two seconds, then the button is pressed, then no further events.

# Output
A string where each character represents one second and indicates the position of the door (0 if fully closed and 5 fully open). The door starts moving immediately, hence its position changes at the same second as the event.

# Example
```..P...O.....``` as input should yield
```001234321000``` as output

I tried it in Python, but could not do it. Can you solve it?

=====
Give me a the Python code you requested?
assistant
#Here is the Python code you requested.

class State:
    def __init__(self):
        pass
class PausedIncreasing(State):
    @classmethod
    def handle(cls, e, val):
        nextstate = cls
        if e == '.':
            pass
        elif e == 'P':
            val += 1
            nextstate = MovingIncreasing
        elif e == 'O':
            nextstate = PausedDecreasing
        return val, nextstate
class PausedDecreasing(State):
    @classmethod
    def handle(cls, e, val):
        nextstate = cls
        if e == '.':
            pass
        elif e == 'P':
            val -= 1
            nextstate = MovingDecreasing
        elif e == 'O':
            nextstate = PausedIncreasing
        return val, nextstate
class MovingIncreasing(State):
    @classmethod
    def handle(cls, e, val):
        nextstate = cls
        if e == '.':
            val += 1
        elif e == 'P':
            nextstate = PausedIncreasing
        elif e == 'O':
            val -= 1
            nextstate = MovingDecreasing
        return val, nextstate
class MovingDecreasing(State):
    @classmethod
    def handle(cls, e, val):
        nextstate = cls
        if e == '.':
            val -= 1
        elif e == 'P':
            nextstate = PausedDecreasing
        elif e == 'O':
            val += 1
            nextstate = MovingIncreasing
        return val, nextstate

class GarageDoor:
    def __init__(self):
        self.val = 0
        self.state = PausedIncreasing
    def handle(self, e):
        self.val, self.state = self.state.handle(e, self.val)
        if self.val == 0:
            self.state = PausedIncreasing
        elif self.val == 5:
            self.state = PausedDecreasing
        return str(self.val)
    def get_output(self, events):
        return ''.join(str(self.handle(e)) for e in events)

def controller(events):
    gd = GarageDoor()
    return gd.get_output(events)