def apply_attribute(string, hashtag='hashtag', attag='attag', url='url'): """ Apply an attribute to `string` dependending on wether it is a hashtag, a Twitter username or an URL. >>> apply_attribute('#Python') ('hashtag', u'#Python') >>> apply_attribute('@dialelo') ('attag', u'@dialelo') >>> apply_attribute('@dialelo', attag='username') ('username', u'@dialelo') >>> apply_attribute('http://www.dialelo.com') ('url', u'http://www.dialelo.com') >>> apply_attribute('turses') u'turses' """ string = str(string) if is_hashtag(string): return (hashtag, string) elif string.startswith('@') and is_username(string[1:]): return (attag, string) elif is_url(string): return (url, string) else: return string
def apply_attribute(string, hashtag='hashtag', attag='attag', url='url'): """ Apply an attribute to `string` dependending on wether it is a hashtag, a Twitter username or an URL. >>> apply_attribute('#Python') ('hashtag', u'#Python') >>> apply_attribute('@dialelo') ('attag', u'@dialelo') >>> apply_attribute('@dialelo', attag='username') ('username', u'@dialelo') >>> apply_attribute('http://www.dialelo.com') ('url', u'http://www.dialelo.com') >>> apply_attribute('turses') u'turses' """ string = unicode(string) if is_hashtag(string): return (hashtag, string) elif string.startswith('@') and is_username(string[1:]): return (attag, string) elif is_url(string): return (url, string) else: return string
def apply_attribute(string): if is_hashtag(string): return ('hashtag', string) elif string.startswith('@') and is_username(string[1:-1]): # FIXME: we can lose some characters here.. username = sanitize_username(string) return ('attag', '@' + username) elif is_url(string): return ('url', string) else: return string