def _check_valid_key ( self , key ) : if not isinstance ( key , key_type ) : raise ValueError ( '%r is not a valid key type' % key ) if not VALID_KEY_RE . match ( key ) : raise ValueError ( '%r contains illegal characters' % key )
check valid dictionary key python
Checks if a key is valid and raises a ValueError if its not .
def _check_valid_key(self, key):
        """Checks if a key is valid and raises a ValueError if its not.

        When in need of checking a key for validity, always use this
        method if possible.

        :param key: The key to be checked
        """
        if not isinstance(key, key_type):
            raise ValueError('%r is not a valid key type' % key)
        if not VALID_KEY_RE.match(key):
            raise ValueError('%r contains illegal characters' % key)