def direction(dir):
    if isinstance(dir, String):
        dir = str(dir)
    cls = type(dir)
    if cls == Direction:
        return dir
    elif cls == int:
        return [
            Direction.right, Direction.up_right, Direction.up,
            Direction.up_left, Direction.left, Direction.down_left,
            Direction.down, Direction.down_right
        ][dir % 8]
    elif cls == str:
        cleaned = re.sub("[^a-z]", "", dir.lower()[:5])
        lookup = {
            "r": Direction.right,
            "ri": Direction.right,
            "rig": Direction.right,
            "righ": Direction.right,
            "right": Direction.right,
            "ur": Direction.up_right,
            "upr": Direction.up_right,
            "upri": Direction.up_right,
            "uprig": Direction.up_right,
            "u": Direction.up,
            "up": Direction.up,
            "ul": Direction.up_left,
            "upl": Direction.up_left,
            "uple": Direction.up_left,
            "uplef": Direction.up_left,
            "l": Direction.left,
            "le": Direction.left,
            "lef": Direction.left,
            "left": Direction.left,
            "dl": Direction.down_left,
            "downl": Direction.down_left,
            "d": Direction.down,
            "do": Direction.down,
            "dow": Direction.down,
            "down": Direction.down,
            "dr": Direction.down_right,
            "downr": Direction.down_right
        }
        if cleaned in lookup:
            return lookup[cleaned]
        elif any(c in dir for c in "0123456789"):
            return [
                Direction.right, Direction.up_right, Direction.up,
                Direction.up_left, Direction.left, Direction.down_left,
                Direction.down, Direction.down_right
            ][int(re.search("\d+", dir).group()) % 8]
        else:
            return 0