Skip to content

Commit

Permalink
Documented assumptions and completed docstrings
Browse files Browse the repository at this point in the history
  • Loading branch information
Derekkarungani committed Jan 11, 2025
1 parent 5a40384 commit 8e0e1c5
Showing 1 changed file with 22 additions and 8 deletions.
30 changes: 22 additions & 8 deletions solutions/get_unique_values.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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:
Expand All @@ -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:
Expand Down

0 comments on commit 8e0e1c5

Please sign in to comment.