Skip to content

Commit

Permalink
improve is_hashtag
Browse files Browse the repository at this point in the history
  • Loading branch information
Alejandro Gómez committed Apr 17, 2012
1 parent a465b30 commit 35cade8
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
7 changes: 5 additions & 2 deletions tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,10 +156,13 @@ def test_is_username(self):
self.failIf(is_username(user))

def test_is_hashtag(self):
valid = ['#turses', '#cúrcuma', '#4n_4Wfu1_US3RN4M3']
valid = ['#turses', '#cúrcuma', '#4n_4Wfu1_H45hT46']
for hashtag in valid:
self.failUnless(is_hashtag(hashtag))
# TODO: test invalid hashtags

invalid = ['s#turses', '#']
for hashtag in invalid:
self.failIf(is_hashtag(hashtag))

def test_sanitize_username(self):
dirty_and_clean = [
Expand Down
13 changes: 11 additions & 2 deletions turses/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

username_regex = re.compile(r'[A-Za-z0-9_]+')

hashtag_regex = re.compile(r'#.+')

prepend_at = lambda username: '@%s' % username

def is_DM(status):
Expand Down Expand Up @@ -92,8 +94,15 @@ def is_username(username):
return match.start() == 0 and match.end() == len(username)
return False

def is_hashtag(string):
return len(string) > 1 and string.startswith('#')
def is_hashtag(hashtag):
"""
Return `True` if `hashtag` is a valid Twitter hashtag, `False`
otherwise.
"""
match = hashtag_regex.match(hashtag)
if match:
return match.start() == 0 and match.end() == len(hashtag)
return False

def sanitize_username(username):
"""
Expand Down

0 comments on commit 35cade8

Please sign in to comment.