Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 29 additions & 60 deletions src/file_utils/content_analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,82 +14,51 @@
def read_lines(file_path: str, encoding: str = 'utf-8') -> List[str]:
"""
Read all lines from a file and return as a list.

Args:
file_path (str): Path to the file to read
encoding (str): File encoding (default: 'utf-8')

Returns:
List[str]: List of lines from the file (with newline characters preserved)

TODO: Implement line-by-line file reading
"""
# TODO: Implement reading lines
pass
lines = []
with open(file_path, 'r', encoding=encoding) as file:
for line in file:
lines.append(line)
return lines


def count_lines(file_path: str, encoding: str = 'utf-8') -> int:
"""
Count the number of lines in a file.

Args:
file_path (str): Path to the file
encoding (str): File encoding (default: 'utf-8')

Returns:
int: Number of lines in the file

Examples:
>>> count_lines('data.txt')
150

TODO: Implement line counting
"""
# TODO: Implement line counting
pass
count = 0
with open(file_path, 'r', encoding=encoding) as file:
for line in file:
count += 1
return count


def count_words(file_path: str, encoding: str = 'utf-8') -> int:
"""
Count the number of words in a file.

Args:
file_path (str): Path to the file
encoding (str): File encoding (default: 'utf-8')

Returns:
int: Number of words in the file

Examples:
>>> count_words('essay.txt')
500

TODO: Implement word counting
Hint: Split the content by whitespace and count the resulting list
"""
# TODO: Implement word counting
pass
word_count = 0
with open(file_path, 'r', encoding=encoding) as file:
for line in file:
words = line.split()
word_count += len(words)
return word_count


def find_in_file(file_path: str, search_term: str, case_sensitive: bool = True, encoding: str = 'utf-8') -> List[Tuple[int, str]]:
"""
Find all occurrences of a search term in a file.

Args:
file_path (str): Path to the file to search
search_term (str): Term to search for
case_sensitive (bool): Whether search should be case sensitive (default: True)
encoding (str): File encoding (default: 'utf-8')

Returns:
List[Tuple[int, str]]: List of tuples containing (line_number, line_content)

Examples:
>>> matches = find_in_file('log.txt', 'ERROR')
>>> print(matches)
[(15, 'ERROR: Connection failed'), (23, 'ERROR: Invalid input')]

TODO: Implement text search in file
"""
# TODO: Implement file search
pass
results = []

with open(file_path, 'r', encoding=encoding) as file:
for line_number, line in enumerate(file, start=1):

if not case_sensitive:
if search_term.lower() in line.lower():
results.append((line_number, line.strip()))
else:
if search_term in line:
results.append((line_number, line.strip()))

return results