def test_nonempty_strings(self): self.assertRaises(TypeError, helper.is_empty_string, None) self.assertRaises(TypeError, helper.is_empty_string, {}) self.assertRaises(TypeError, helper.is_empty_string, 42) self.assertRaises(TypeError, helper.is_empty_string, []) self.assertFalse(helper.is_empty_string("Hello, World"), "Non-empty string.") self.assertFalse(helper.is_empty_string(" *"), "String with leading whitespace and asterisk.") self.assertFalse(helper.is_empty_string("trailblazer "), "String with trailing whitespace.") self.assertFalse(helper.is_empty_string(" Hello, World "), "String with leading and trailing whitespace.") self.assertFalse(helper.is_empty_string("\r\n\t\\"), "Escape sequences with backslash character.")
def _read_config_params_from_file(self, config_file): config_params = {} with (open(config_file, "r")) as infile: for line in infile: if not is_whole_string_comment(line) and not is_empty_string(line): cleaned_line = clean_string(line) field_name = cleaned_line.split()[0] if field_name == "torsion": config_params = self._handle_torsion_field( cleaned_line, config_params ) elif field_name == "namd_energy": config_params = self._handle_namd_energy_field( cleaned_line, config_params ) elif field_name == "ring_pucker": config_params = self._handle_ring_pucker_field( cleaned_line, config_params ) elif field_name == "block_average": config_params[field_name] = self._handle_block_average_field( cleaned_line, config_params ) elif field_name == "atom_distance": config_params = self._handle_atom_distance_field( cleaned_line, config_params ) elif field_name == "frames_per_ns": config_params[field_name] = eval(line.split()[1]) else: config_params[field_name] = cleaned_line.split()[1] return config_params
def __is_geotagging_input(question_input, _): """Validates the specified geotagging input configuration. A geotagging input configuration contains the following optional fields: - location: a string that specifies the input's initial location. Args: question_input (dict): An input configuration to validate. Returns: <bool, str|None>: A pair containing the value True if the specified configuration is valid, False otherwise; as well as an error message in case it is invalid. """ unexpected_keys = find_unexpected_keys(question_input, __is_geotagging_input.FIELDS) if unexpected_keys: message = "The geotagging input configuration contains the following unrecognized fields: '{}'." return (False, message.format("', '".join(unexpected_keys))) location = question_input.get("location") if location is not None: message = "A geotagging input's 'location' field must be a non-empty string." try: if is_empty_string(location): return (False, message) except TypeError: return (False, message) return (True, None)
def __is_multiple_option_input(question_input, languages=None): unexpected_keys = find_unexpected_keys(question_input, __is_multiple_option_input.FIELDS) if unexpected_keys: message = "The multiple-option input configuration contains the following unrecognized fields: '{}'." return (False, message.format("', '".join(unexpected_keys))) for key in ["enable-multiple-choices", "enable-other-option", "enable-illustrations"]: field = question_input.get(key) if field is not None and not isinstance(field, bool): return (False, "The '{}' field must be a boolean value.".format(key)) options = question_input.get("options") if options is not None: enable_illustrations = question_input.get("enable-illustrations", False) if not isinstance(options, list) or len(options) < 1: return (False, "The 'options' field must be a non-empty list.") else: for option in options: label = option.get("label") error = (False, "An option label must be a non-empty or normalized string.") try: if label is None or not is_configuration_string(label, languages): return error except TypeError: return error value = option.get("value") if value is None or not isinstance(value, basestring): return (False, "An option value must be a string.") # If the 'enable-illustrations' flag is set to True, validate illustrations. illustration = option.get("illustration") if enable_illustrations else None if illustration is not None: missing = [k for k in illustration.keys() if k not in __is_multiple_option_input.ILLUSTRATION_FIELDS or illustration[k] is None] if missing: missing = "', '".join(missing) return (False, "The illustration is missing the following fields: '{}'.".format(missing)) for key in __is_multiple_option_input.ILLUSTRATION_FIELDS: try: field = illustration.get(key) if is_empty_string(field): return (False, "An illustration's '{}' field must be a non-empty string.".format(key)) except: return (False, "An illustration's '{}' field must be a string.".format(key)) else: return (False, "The 'options' field must be a non-empty list.") return (True, None)
def is_project_description(description): """Validates the specified project description. A valid description is simply a non-empty string. Args: description (str): A project description to validate. Returns: <bool, str|None>: A pair containing the value True if the specified description is valid, False otherwise; and an error message in case the description is invalid. """ try: return (False, "A project description must be a non-empty string.") if is_empty_string(description) else (True, None) except TypeError: return (False, "The 'description' argument must be a string.")
def is_project_name(name): """Validates the specified project name. A valid name is simply a non-empty string. Args: name (str): A project name to validate. Returns: <bool, str|None>: A pair containing the value True if the specified name is valid, False otherwise; and an error message in case the name is invalid. """ try: return (False, "A project name must be a non-empty string.") if is_empty_string(name) else (True, None) except TypeError: return (False, "The 'name' argument must be a string.")
def is_project_name(name): """Validates the specified project name. A valid name is simply a non-empty string. Args: name (str): A project name to validate. Returns: <bool, str|None>: A pair containing the value True if the specified name is valid, False otherwise; and an error message in case the name is invalid. """ try: return (False, "A project name must be a non-empty string." ) if is_empty_string(name) else (True, None) except TypeError: return (False, "The 'name' argument must be a string.")
def is_project_description(description): """Validates the specified project description. A valid description is simply a non-empty string. Args: description (str): A project description to validate. Returns: <bool, str|None>: A pair containing the value True if the specified description is valid, False otherwise; and an error message in case the description is invalid. """ try: return (False, "A project description must be a non-empty string." ) if is_empty_string(description) else (True, None) except TypeError: return (False, "The 'description' argument must be a string.")
def is_subject_type(subject_type): """Validates the specified subject type. Args: subject_type (str): A subject type to validate. Returns: <bool, str|None>: A pair containing the value True if the specified subject type is valid, False otherwise; as well as an error message in case it is invalid. Raises: TypeError: If the 'subject_type' argument is not a string. """ if is_empty_string(subject_type): return (False, "A subject type must be a non-empty string.") elif subject_type not in is_subject_type.SUPPORTED_TYPES: return (False, "The subject type '{}' is not recognized.".format(subject_type)) return (True, None)
def is_question_input_type(input_type): """Validates the specified question input type. Args: input_type (str): An input type to validate. Returns: <bool, str|None>: A pair containing the value True if the specified input type is valid, False otherwise; as well as an error message in case it is invalid. Raises: TypeError: If the input_type argument is not a string. """ if is_empty_string(input_type): return (False, "An input type must be a non-empty string.") elif input_type not in is_question_input_type.INPUT_TYPES: return (False, "The input type '{}' is not recognized.".format(input_type)) return (True, None)
def is_subject_type(subject_type): """Validates the specified subject type. Args: subject_type (str): A subject type to validate. Returns: <bool, str|None>: A pair containing the value True if the specified subject type is valid, False otherwise; as well as an error message in case it is invalid. Raises: TypeError: If the 'subject_type' argument is not a string. """ if is_empty_string(subject_type): return (False, "A subject type must be a non-empty string.") elif subject_type not in is_subject_type.SUPPORTED_TYPES: return ( False, "The subject type '{}' is not recognized.".format(subject_type)) return (True, None)
def __is_key(key): """Validates the specified key. A key is a non-empty string that is strictly composed of alphanumeric characters (a-Z, A-Z, 0-9), hyphens (-) or underscores (_). Args: key (str): A key to validate. Returns: bool: True if the specified key is valid, False otherwise. Raises: TypeError: If the key argument is not a string. """ if is_empty_string(key): return False else: from re import match matches = match("[a-zA-Z0-9-_]+", key) return matches and matches.group() == key
def is_project_short_name(short_name): """Validates the specified project short name. A valid short name is a non-empty string that contains only alphanumeric characters (a-z, A-Z, 0-9), hyphens (-) and underscores (_). Args: short_name (str): A project short name to validate. Returns: <bool, str|None>: A pair containing the value True if the specified short name is valid, False otherwise; and an error message in case the short name is invalid. """ ERROR_MESSAGE = "A short name must be a non-empty string containing only of alphanumeric characters (a-z, A-Z, 0-9), hyphens (-) and underscores (_)." try: if is_empty_string(short_name): return (False, ERROR_MESSAGE) else: from re import match matches = match(r"[a-zA-Z0-9-_]+", short_name) matched = matches and (matches.group() == short_name) return (True, None) if matched else (False, ERROR_MESSAGE) except TypeError: return (False, "The 'short_name' argument must be a string.")
def is_expects(assertion_expects): message = "A tutorial subject assertion's 'expects' field must be a non-empty string." return (False, message) if is_empty_string(assertion_expects) else (True, None)
def is_page(subject_page): message = "A tutorial subject's 'page' field must be a non-empty string." return (False, message) if is_empty_string(subject_page) else (True, None)
def test_empty_strings(self): self.assertTrue(helper.is_empty_string(""), "Empty string.") self.assertTrue(helper.is_empty_string(" "), "Empty string (whitespace only).") self.assertTrue(helper.is_empty_string("\r\n\t"), "Empty string (escape sequences).")