最終実装とテスト
ここで、必要なすべてのケースを扱う実装を完成させ、すべてのテストケースに合格することを確認しましょう。
snake_case.py ファイルを最終実装で更新します。
import re
def snake(s):
## Replace hyphens with spaces
s = s.replace('-', ' ')
## Handle PascalCase pattern
s = re.sub('([A-Z][a-z]+)', r' \1', s)
## Handle sequences of uppercase letters
s = re.sub('([A-Z]+)', r' \1', s)
## Split by whitespace and join with underscores
return '_'.join(s.split()).lower()
## Test with a complex example
if __name__ == "__main__":
test_string = "some-mixed_string With spaces_underscores-and-hyphens"
result = snake(test_string)
print(f"Original: {test_string}")
print(f"Snake case: {result}")
この最終実装では以下のことを行います。
- ハイフンを空白に置き換えます。
re.sub('([A-Z][a-z]+)', r' \1', s) を使って、「Word」のようなパターンの前に空白を追加します。
re.sub('([A-Z]+)', r' \1', s) を使って、連続する大文字の前に空白を追加します。
- 空白で分割し、アンダースコアで結合し、小文字に変換します。
ここで、セットアップステップで作成したテストセットに対して関数を実行しましょう。
python3 ~/project/test_snake.py
実装が正しければ、次のように表示されるはずです。
All tests passed! Your snake case function works correctly.
おめでとうございます!様々な入力形式を扱うことができる堅牢なスネークケース変換関数を成功させました。
元の問題の例を使って関数をテストすることで、関数が仕様を正確に守っていることを確認しましょう。
## Add this to the end of your snake_case.py file:
if __name__ == "__main__":
examples = [
'camelCase',
'some text',
'some-mixed_string With spaces_underscores-and-hyphens',
'AllThe-small Things'
]
for ex in examples:
result = snake(ex)
print(f"Original: {ex}")
print(f"Snake case: {result}")
print("-" * 20)
更新したスクリプトを実行します。
python3 ~/project/snake_case.py
すべての例が正しくスネークケースに変換されていることが確認できるはずです。
Original: some-mixed_string With spaces_underscores-and-hyphens
Snake case: some_mixed_string_with_spaces_underscores_and_hyphens
Original: camelCase
Snake case: camel_case
--------------------
Original: some text
Snake case: some_text
--------------------
Original: some-mixed_string With spaces_underscores-and-hyphens
Snake case: some_mixed_string_with_spaces_underscores_and_hyphens
--------------------
Original: AllThe-small Things
Snake case: all_the_small_things
--------------------