最终实现与测试
现在,让我们完成实现,以处理所有需要的情况,并验证它能通过所有测试用例。
使用最终实现更新你的 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)
在连续大写字母序列前添加一个空格。
- 按空格分割字符串,用下划线连接,并转换为小写。
现在,让我们使用在设置步骤中创建的测试套件来运行我们的函数:
cd /tmp && python3 test_snake.py
如果你的实现正确,你应该会看到:
All tests passed! Your snake case function works correctly.
恭喜!你已成功实现了一个强大的蛇形命名法(snake case)转换函数,它可以处理各种输入格式。
让我们通过使用原始问题中的示例进行测试,确保我们的函数准确遵循规范:
## 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
--------------------