Last active
January 2, 2016 18:19
-
-
Save cameronp98/8342903 to your computer and use it in GitHub Desktop.
convert strings to standard format
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import re | |
| import unittest | |
| from unicodedata import normalize | |
| # http://en.wikipedia.org/wiki/Clean_URL#Slug | |
| def slugify(string, delim="-"): | |
| string = normalize("NFKD", string).lower() | |
| return re.sub(r"\W+", delim, string).strip(delim) | |
| class TestSlugify(unittest.TestCase): | |
| def test_capitals(self): | |
| string = "This is a TEST" | |
| slug = slugify(string) | |
| self.assertEqual(slug, "this-is-a-test") | |
| def test_punctuation(self): | |
| string = "This: a SLUG! T#E*S;T" | |
| slug = slugify(string) | |
| self.assertEqual(slug, "this-a-slug-t-e-s-t") | |
| if __name__ == '__main__': | |
| unittest.main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment