def fix_length(row) : """Takes in a row and return it with the event name, first name and last name columns truncated after 30 characters. Parameters: row (str): String of data with comma separators. Return: str: The modified row is returned. Preconditions: row != None """ eventname = get_column(row, 0) #Get event name column from 'row'. firstname = get_column(row, 1) lastname = get_column(row, 2) if len(firstname) > 30 : firstname = truncate_string(firstname, 30) #truncate firstname if it's over 30 characters long if len(lastname) > 30 : lastname = truncate_string(lastname, 30) #truncate lastname if it's over 30 characters long if len(eventname) > 30 : eventname = truncate_string(eventname, 30) #truncate eventname if it's over 30 characters long row = replace_column(row, eventname, 0) #replace first name column with the truncated firstname row = replace_column(row, firstname, 1) #replace first name column with the truncated firstname row = replace_column(row, lastname, 2) #replace last name column with the truncated lastname return row
def check_name(row) : """Checks a row to see whether the first name and last name columns contains valid data. Parameters: row (str): String of data with comma separators. Return: bool: Return True if data is corrupt, otherwise return False. Preconditions: row != None """ first_name = get_column(row, 1) #Get the first name column from 'row'. last_name = get_column(row, 2) with open("athlete_names.csv", "r") as names: #Imports the athlete_names.csv master file. for row in names : row = row[:-1] #Removes the new line characters (\n) from the end of each row. first_master = get_column(row, 0) #First name extracted from the master file. first_master = truncate_string(first_master, 30) #We are comparing between truncated versions of the strings. last_master = get_column(row, 1) last_master = truncate_string(last_master, 30) if (first_name == first_master) and (last_name == last_master) : return False #If a match is found, return False. return True
def truncate_names_in_row(row): """Return the row with columns 2, 3, & 4 truncated Parameters row (list): list of columns in the row Return row (list): list of columns in the row now with column 2, 3, & 4 truncated Preconditions row != None """ row[1] = truncate_string(row[1], MAX_CHARACTERS_COLUMN_2) row[2] = truncate_string(row[2], MAX_CHARACTERS_COLUMN_3) row[3] = truncate_string(row[3], MAX_CHARACTERS_COLUMN_4) return row
def max_char_30(row): # Truncates sport, atheletes' first and family name to 30 characters """ Truncate data in indicated column to 30 characters. Parameters: row (str): String of data with comma separators (CSV format). Return: str: Updated row with truncated data in columns 1 ~ 4. """ for i in range(4): row_update = get_column(row, i) # row_update now contain data from row and in the 'i' value of column row_updated = truncate_string(row_update, 30) # Truncates string to 30 characters and stores in row_updated row = replace_column(row, row_updated, i) # Updated data replaces the exisiting data at indicated column in the row return row
def check_event_name(row) : """Checks a row to see whether the event name column contains valid data. Parameters: row (str): String of data with comma separators. Return: bool: return True if data is corrupt, otherwise return false Preconditions: row != None """ event_name = get_column(row, 0) #Get the event name column from 'row'. with open("event_names.csv", "r") as event_names: #Imports the event_names.csv master file. for row in event_names : row = row[:-1] #Removes the new line characters (\n) from the end of each row. row = truncate_string(row, 30) #We are comparing between truncated versions of the strings. if event_name == row : return False #If a match is found between 'event_name' and one of the event names in the master file, set invalid_event to False. return True #No match is found, so return True.
def truncate_name_length(row_to_process): """Returns a row with first three columns truncated to size 30. Parameters: row_to_process (str): Row whose column values are to be truncated. Returns: str: Row with event and athlete names truncated to size 30. Preconditions: maximum character length of the columns is specified as constant variable. functions get_column, replace_column and truncate_string have been \ imported from assign1_utilities. """ column_position = 0 row_length_check = row_to_process while column_position < 3: value = get_column(row_to_process, column_position) if len(value) > NAME_MAX_CHARACTER_LENGTH: value = truncate_string(value, NAME_MAX_CHARACTER_LENGTH) row_length_check = replace_column(row_length_check, value, column_position) column_position += 1 return (row_length_check)
def check_valid_rules(row_to_process, corrupt): """Returns a string with value True if values are invalid or \ row doesn't follow rules checked earlier Parameters: row_to_process (str): Row whose values are to be validated. corrupt (boolean) : status of row Returns: boolean: corrupt value as input or set as True if columns doesn't have valid values Preconditions: corrupt value is already True if the row doesn't abide with rules checked before. functions get_column and replace_column have been imported from assign1_utilities. column position specified as constant variable. """ corrupt_after_check = corrupt """Check for invalid event name""" event_name_value = get_column(row_to_process, EVENT_NAME_POSITION) is_event_name_valid = False # For the event name matches with any valid name in file it is valid with open("event_names.csv", "r") as valid_event_name_file: for row in valid_event_name_file: valid_event_name = row[:-1] # Remove /n valid_event_name = truncate_string(valid_event_name, NAME_MAX_CHARACTER_LENGTH) if valid_event_name == event_name_value: is_event_name_valid = True if is_event_name_valid == False: corrupt_after_check = True """Check for invalid country code""" country_code_value = get_column(row_to_process, COUNTRY_CODE_POSITION) is_country_code_valid = False # If the country code matches with any valid name in file it is valid with open("country_codes.csv", "r") as valid_country_code_file: for row in valid_country_code_file: valid_country_code = row[:-1] # Remove /n if valid_country_code == country_code_value: is_country_code_valid = True if is_country_code_valid == False: corrupt_after_check = True """Check for invalid athlete name""" athlete_first_name_value = get_column(row_to_process, ATHLETE_FIRST_NAME_POSITION) athlete_surname_value = get_column(row_to_process, ATHLETE_SURNAME_POSITION) is_athlete_name_valid = False # If the event name matches with any valid name in file it is valid with open("athlete_names.csv", "r") as valid_athlete_names_file: for row in valid_athlete_names_file: valid_athlete_first_name = get_column(row, 0) valid_athlete_surname = get_column(row, 1) valid_athlete_surname = valid_athlete_surname[:-1] # Remove /n valid_athlete_first_name = truncate_string( valid_athlete_first_name, NAME_MAX_CHARACTER_LENGTH) valid_athlete_surname = truncate_string(valid_athlete_surname, NAME_MAX_CHARACTER_LENGTH) if valid_athlete_first_name == athlete_first_name_value \ and valid_athlete_surname == athlete_surname_value: is_athlete_name_valid = True if is_athlete_name_valid == False: corrupt_after_check = True # Return input corrupt value or set corrupt value as True if names are invalid return (corrupt_after_check)