Skip to content

Instantly share code, notes, and snippets.

@chrickso
Created July 3, 2017 16:17
Show Gist options
  • Save chrickso/cbc351bd0d308d8a1d65a9d78c1e1d08 to your computer and use it in GitHub Desktop.
Save chrickso/cbc351bd0d308d8a1d65a9d78c1e1d08 to your computer and use it in GitHub Desktop.
python 3 filter()
def sanitize_username(self, s):
"""Takes a string and returns it with only allowed characters."""
# http://stackoverflow.com/questions/12756156/with-python-what-is-the-most-efficient-way-to-remove-all-char-from-string-excep#comment17236154_12756156
# a-z, A-Z, 0-9, _, -, .
keep_set = set(string.ascii_letters + string.digits + '_-.')
return filter(keep_set.__contains__, s)
sanitize_username('chrickso') returns <filter object at 0x10c53aa58>
def sanitize_username(self, s):
"""Takes a string and returns it with only allowed characters."""
# http://stackoverflow.com/questions/12756156/with-python-what-is-the-most-efficient-way-to-remove-all-char-from-string-excep#comment17236154_12756156
# a-z, A-Z, 0-9, _, -, .
keep_set = set(string.ascii_letters + string.digits + '_-.')
return list(filter(keep_set.__contains__, s))
sanitize_username('chrickso') returns ['c', 'h', 'r', 'i', 'c', 'k', 's', 'o']
how can i get it to return 'chrickso' ?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment