Owner: Vadim Rudakov, rudakow
This script formats a given input string by applying several transformations to make it URL-safe and filesystem-friendly. The transformations include converting to lowercase, replacing specific special characters, removing unwanted words, and truncating the string if necessary.
Synopsis¶
format_string.py 'Your Input String'Transformation Logic¶
Convert to Lowercase: All characters in the input string are converted to lowercase.
Replace & with and: The ampersand (
&) is replaced with the word “and”.Remove Special Symbols: Certain special symbols (e.g., "the ", “(”, “)”, "# ", “#”, “`”, “~”, “$”, “%”, “@”) are removed from the string.
Replace Special Symbols with Underscores: Other special symbols (e.g., “.”, “,”, “;”, “:”, “!”, “?”, “-”, “/”, “\”, “|”, “<”, “>”, “*”) are replaced with underscores (
_).Remove Multiple Underscores: Any sequence of multiple underscores is reduced to a single underscore.
Replace Spaces with Underscores: All spaces in the string are replaced with underscores.
Truncate Long Strings: If the resulting string exceeds 50 characters, it is truncated to 50 characters.
Remove Trailing Underscore: If the final character of the string is an underscore, it is removed.
Examples¶
format_string.py 'Agents4Science Conference Paper Digest: How Agents Are "Doing" Science Right Now'format_string.py '# Post-Mortem: Architectural Flaws in the `nbdiff`-Centric Jupyter Version Control Handbook'format_string.py 'Feedforward Neural Networks in Depth, Part 3 Cost Functions | I, Deep Learning.pdf'format_string.py 'From Concepts to Code: Introduction to Data Science (2024)'format_string.py 'Quiz - Special Applications: Face Recognition & Neural Style Transfer'Test Suite¶
The test suite covers all transformation rules:
| Test Area | Coverage |
|---|---|
| Case conversion | Lowercase transformation |
| Symbol replacement | &, punctuation, slashes, etc. |
| Symbol removal | Parentheses, quotes, special chars |
| Underscore handling | Collapsing, stripping |
| Truncation | 50 character limit |
Run tests with:
uv run pytest tools/tests/test_format_string.py -v