From 8e0e1c5b157f573dbee156961e8d03ba935bf615 Mon Sep 17 00:00:00 2001 From: Derek Karungani Date: Sat, 11 Jan 2025 16:44:50 -0600 Subject: [PATCH] Documented assumptions and completed docstrings --- solutions/get_unique_values.py | 30 ++++++++++++++++++++++-------- 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/solutions/get_unique_values.py b/solutions/get_unique_values.py index 94a6a590f..2eab9956b 100644 --- a/solutions/get_unique_values.py +++ b/solutions/get_unique_values.py @@ -1,7 +1,10 @@ -# Module for extracting unique values from CSV file. """ -This module provides the get_unique_values function to extract -unique values from a specified column in a CSV file. +This module provides functionality to extract unique values from a CSV file. + +It contains the `get_unique_values` function, which takes a file path and +column name as input and returns a list of unique values from that column. +The module handles potential errors like invalid file paths or missing column +names. """ import csv @@ -12,15 +15,20 @@ def get_unique_values(file_path: str, column_name: str) -> list: Get unique values from a specified column in a CSV file. Parameters: - file_path (str): Path to the CSV file. - column_name (str): Name of the column to extract unique values. + file_path (str): Path to the CSV file. It should be a valid path to an existing CSV file. + The function assumes the file is encoded in UTF-8. Both relative and absolute + paths are acceptable. The file must exist, or a FileNotFoundError will be raised. + column_name (str): Name of the column to extract unique values. It should correspond to a + header in the CSV file. Returns: - list: Unique values in the specified column. + list: Unique values in the specified column. The order of the unique values is not guaranteed to be + the same as their order of appearance in the CSV file. Raises: - FileNotFoundError: If the file path is invalid. + FileNotFoundError: If the file path is invalid or the file does not exist. KeyError: If the column name doesn't exist in the CSV. + ValueError: If the `file_path` is an empty string. Examples: >>> with open("test.csv", "w", newline="") as f: @@ -43,9 +51,15 @@ def get_unique_values(file_path: str, column_name: str) -> list: Traceback (most recent call last): ... FileNotFoundError: File not found: nonexistent_file.csv + >>> get_unique_values("", "Name") + Traceback (most recent call last): + ... + ValueError: The file_path cannot be an empty string. """ + if not file_path: + raise ValueError("The file_path cannot be an empty string.") try: - with open(file_path, "r") as file: + with open(file_path, "r", encoding="utf-8") as file: reader = csv.DictReader(file) # Check if the column name exists in the CSV header if column_name not in reader.fieldnames: