import re
import keyword
import six


def _is_identifier(name):
    """Check if 'name' is a valid Python identifier.

    Source: https://stackoverflow.com/a/2545164
    """
    if name:
        if six.PY2:
            return re.match(r'^[a-z_][a-z0-9_]*$', name, re.I) and not keyword.iskeyword(name)
        else:
            return name.isidentifier()
    else:
        return False    # empty string or None