Skip to content

Instantly share code, notes, and snippets.

@cameronp98
Last active January 2, 2016 18:19
Show Gist options
  • Select an option

  • Save cameronp98/8342903 to your computer and use it in GitHub Desktop.

Select an option

Save cameronp98/8342903 to your computer and use it in GitHub Desktop.
convert strings to standard format
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