Advertisement
Guest User

Untitled

a guest
Feb 20th, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.70 KB | None | 0 0
  1. def valid_channel_name(name):
  2. """Channel names can only contain lowercase letters,
  3. numbers, hyphens, and underscores, and must be 21 characters or less.
  4.  
  5. Some sample conversions from Slack to determine some heuristics:
  6. 1..................20abc def => 1_20abc-def
  7. 1..................20abc..def => 1_20abc_def
  8.  
  9. Heuristics:
  10. 1. Encode input into ASCII
  11. 2. Replace non-alphanumeric groups of charaters by a single _
  12. 3. Replace all spaces by dashes
  13. 4. Truncate the resulting sring into first 21 characters
  14. """
  15. result = unicodedata.normalize('NFD', name).encode('ascii', 'ignore')
  16. result = re.sub("[^a-zA-Z\d\s-]+", '_', result)
  17. result = result.replace(' ', '-')
  18. result = result[:21].lower()
  19. return result
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement