Skip to content

Commit eea1bac

Browse files
alisatwat3cclausspre-commit-ci[bot]
authored
fix: raise ValueError in encode() for non-lowercase input (#14936)
* fix: raise ValueError in encode() for non-lowercase input encode() previously accepted uppercase letters, digits, and other non-lowercase characters silently, producing incorrect/out-of-range values (e.g. negative numbers for uppercase letters) instead of failing. Add input validation using str.islower() and str.isalpha() to raise a ValueError when the input isn't purely lowercase a-z. Added a doctest covering the new error case. * resolved doctest * Fix Ruff 0.16 lint failures * Enhance encode function error handling examples Update error handling in encode function to include examples for mixed case and invalid characters. * Fix indentation in test_cancer_data function * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --------- Co-authored-by: Christian Clauss <cclauss@me.com> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 758d487 commit eea1bac

2 files changed

Lines changed: 15 additions & 1 deletion

File tree

ciphers/a1z26.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,21 @@ def encode(plain: str) -> list[int]:
1313
"""
1414
>>> encode("myname")
1515
[13, 25, 14, 1, 13, 5]
16+
>>> encode("abCd")
17+
Traceback (most recent call last):
18+
...
19+
ValueError: plain must contain only lowercase letters (a-z)
20+
>>> encode("n0w")
21+
Traceback (most recent call last):
22+
...
23+
ValueError: plain must contain only lowercase letters (a-z)
24+
>>> encode("later!")
25+
Traceback (most recent call last):
26+
...
27+
ValueError: plain must contain only lowercase letters (a-z)
1628
"""
29+
if not plain.islower() or not plain.isalpha():
30+
raise ValueError("plain must contain only lowercase letters (a-z)")
1731
return [ord(elem) - 96 for elem in plain]
1832

1933

machine_learning/sequential_minimum_optimization.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -451,7 +451,7 @@ def test_cancer_data():
451451
print("Hello!\nStart test SVM using the SMO algorithm!")
452452
# 0: download dataset and load into pandas' dataframe
453453
if not os.path.exists(r"cancer_data.csv"):
454-
request = urllib.request.Request( # noqa: S310
454+
request = urllib.request.Request(
455455
CANCER_DATASET_URL,
456456
headers={"User-Agent": "Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)"},
457457
)

0 commit comments

Comments
 (0)