Skip to content

Commit c85c328

Browse files
Refactor code for improved clarity and consistency.
Remove unnecessary parentheses in conditional expressions and list comprehensions. Add docstrings to test functions in `test_utils.py` for better documentation. Simplify error handling messages in `cli.py` to reduce redundancy and enhance readability.
1 parent b0b268e commit c85c328

File tree

5 files changed

+23
-12
lines changed

5 files changed

+23
-12
lines changed

gsppy/cli.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,9 @@ def read_transactions_from_json(file_path: str) -> List[List]:
5656
raise ValueError("File should contain a JSON array of transaction lists.")
5757
return transactions
5858
except Exception as e:
59-
logging.error(f"Error reading transaction data from JSON file '{file_path}': {e}")
60-
raise ValueError(f"Error reading transaction data from JSON file: {e}")
59+
msg = f"Error reading transaction data from JSON file '{file_path}': {e}"
60+
logging.error(msg)
61+
raise ValueError(msg)
6162

6263

6364
def read_transactions_from_csv(file_path: str) -> List[List]:
@@ -85,8 +86,9 @@ def read_transactions_from_csv(file_path: str) -> List[List]:
8586
transactions.append([item.strip() for item in row if item.strip()])
8687
return transactions
8788
except Exception as e:
88-
logging.error(f"Error reading transaction data from CSV file '{file_path}': {e}")
89-
raise ValueError(f"Error reading transaction data from CSV file: {e}")
89+
msg = f"Error reading transaction data from CSV file '{file_path}': {e}"
90+
logging.error(msg)
91+
raise ValueError(msg)
9092

9193

9294
def detect_and_read_file(file_path: str) -> List[List]:
@@ -110,10 +112,11 @@ def detect_and_read_file(file_path: str) -> List[List]:
110112

111113
if file_extension == ".json":
112114
return read_transactions_from_json(file_path)
113-
elif file_extension == ".csv":
115+
116+
if file_extension == ".csv":
114117
return read_transactions_from_csv(file_path)
115-
else:
116-
raise ValueError("Unsupported file format. Please provide a JSON or CSV file.")
118+
119+
raise ValueError("Unsupported file format. Please provide a JSON or CSV file.")
117120

118121

119122
def main():

gsppy/gsp.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ def _pre_processing(self, raw_transactions: List[List]):
173173
raise ValueError(msg)
174174

175175
logger.info("Pre-processing transactions...")
176-
self.max_size = max([len(item) for item in raw_transactions])
176+
self.max_size = max(len(item) for item in raw_transactions)
177177
self.transactions = [tuple(transaction) for transaction in raw_transactions]
178178
counts = Counter(chain.from_iterable(raw_transactions))
179179
self.unique_candidates = [(item,) for item in counts.keys()]
@@ -273,7 +273,7 @@ def search(self, min_support: float = 0.2) -> List[Dict[Tuple, int]]:
273273
and completion.
274274
- Status updates for each iteration until the algorithm terminates.
275275
"""
276-
if not (0.0 < min_support <= 1.0):
276+
if not 0.0 < min_support <= 1.0:
277277
raise ValueError("Minimum support must be in the range (0.0, 1.0]")
278278

279279
min_support = len(self.transactions) * min_support

gsppy/tests/test_gsp.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ def test_no_frequent_items(supermarket_transactions):
8383
"""
8484
gsp = GSP(supermarket_transactions)
8585
result = gsp.search(min_support=0.9) # High minimum support
86-
assert result == [], "High minimum support should filter out all items."
86+
assert not result, "High minimum support should filter out all items."
8787

8888

8989
def test_frequent_patterns(supermarket_transactions):

gsppy/tests/test_utils.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212

1313

1414
def test_split_into_batches():
15+
"""
16+
Test the `split_into_batches` utility function.
17+
"""
1518
# Test with exact batches
1619
items = [(1,), (2,), (3,), (4,), (5,)]
1720
batch_size = 2
@@ -32,10 +35,13 @@ def test_split_into_batches():
3235
items = []
3336
batch_size = 3
3437
result = list(split_into_batches(items, batch_size))
35-
assert result == [], "Failed empty input"
38+
assert not result, "Failed empty input"
3639

3740

3841
def test_is_subsequence_in_list():
42+
"""
43+
Test the `is_subsequence_in_list` utility function.
44+
"""
3945
# Test when the subsequence is present
4046
assert is_subsequence_in_list((1, 2), (0, 1, 2, 3)), "Failed to find subsequence"
4147
assert is_subsequence_in_list((3,), (0, 1, 2, 3)), "Failed single-element subsequence"
@@ -53,6 +59,9 @@ def test_is_subsequence_in_list():
5359

5460

5561
def test_generate_candidates_from_previous():
62+
"""
63+
Test the `generate_candidates_from_previous` utility function.
64+
"""
5665
# Test if candidates are generated correctly
5766
prev_patterns = {
5867
(1, 2): 3,

gsppy/utils.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,4 +83,3 @@ def generate_candidates_from_previous(prev_patterns: Dict[Tuple, int]) -> List[T
8383
for pattern1, pattern2 in product(keys, repeat=2)
8484
if pattern1[1:] == pattern2[:-1] and not (len(pattern1) == 1 and pattern1 == pattern2)
8585
]
86-

0 commit comments

Comments
 (0)