mirror of https://github.com/mahmoud/boltons.git
add basic docs and doctests to strutils
This commit is contained in:
parent
d64f944b3e
commit
8f8104daf3
|
@ -14,19 +14,41 @@ def slugify(text, delim='_'):
|
||||||
lowercased string separated only by the delimiter specified
|
lowercased string separated only by the delimiter specified
|
||||||
by 'delim', which defaults to '_'.
|
by 'delim', which defaults to '_'.
|
||||||
|
|
||||||
>>> 'First post! Hi!!!!~1 '
|
>>> slugify('First post! Hi!!!!~1 ')
|
||||||
'first_post_hi_1'
|
'first_post_hi_1'
|
||||||
"""
|
"""
|
||||||
return delim.join(split_punct_ws(text)).lower()
|
return delim.join(split_punct_ws(text)).lower()
|
||||||
|
|
||||||
|
|
||||||
def split_punct_ws(text):
|
def split_punct_ws(text):
|
||||||
|
"""
|
||||||
|
str.split() will split on whitespace, split_punct_ws will
|
||||||
|
split on punctuation and whitespace. This is mostly here
|
||||||
|
for use by slugify(), above.
|
||||||
|
|
||||||
|
>>> split_punct_ws('First post! Hi!!!!~1 ')
|
||||||
|
['First', 'post', 'Hi', '1']
|
||||||
|
"""
|
||||||
return [w for w in _punct_re.split(text) if w]
|
return [w for w in _punct_re.split(text) if w]
|
||||||
|
|
||||||
|
|
||||||
def camel2under(string):
|
def camel2under(camel_string):
|
||||||
return _camel2under_re.sub(r'_\1', string).lower()
|
"""
|
||||||
|
Converts a camelcased string to underscores. Useful for
|
||||||
|
turning a class name into a function name.
|
||||||
|
|
||||||
|
>>> camel2under('BasicParseTest')
|
||||||
|
'basic_parse_test'
|
||||||
|
"""
|
||||||
|
return _camel2under_re.sub(r'_\1', camel_string).lower()
|
||||||
|
|
||||||
|
|
||||||
def under2camel(string):
|
def under2camel(under_string):
|
||||||
return ''.join(w.capitalize() or '_' for w in string.split('_'))
|
"""
|
||||||
|
Converts an underscored string to camelcased. Useful for
|
||||||
|
turning a function name into a class name.
|
||||||
|
|
||||||
|
>>> under2camel('complex_tokenizer')
|
||||||
|
'ComplexTokenizer'
|
||||||
|
"""
|
||||||
|
return ''.join(w.capitalize() or '_' for w in under_string.split('_'))
|
||||||
|
|
Loading…
Reference in New Issue