def snake(s):
    """Convert from title or camelCase to snake_case."""
    if len(s) < 2:
        return s.lower()
    out = s[0].lower()
    for c in s[1:]:
        if c.isupper():
            out += '_'
            c = c.lower()
        out += c
    return out
