mirror of https://github.com/mahmoud/boltons.git
add under2camel, camel2under, and slugify (and split_punct_ws, which is much faster than re-based punctuation splitting)
This commit is contained in:
parent
14333377e2
commit
e73e4ca02d
|
@ -0,0 +1,22 @@
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
import re
|
||||||
|
import string
|
||||||
|
|
||||||
|
_camel2under_re = re.compile('((?<=[a-z0-9])[A-Z]|(?!^)[A-Z](?=[a-z]))')
|
||||||
|
_punct_ws_str = string.punctuation + string.whitespace
|
||||||
|
|
||||||
|
|
||||||
|
def slugify(text, delim='_'):
|
||||||
|
return delim.join(split_punct_ws(text).lower())
|
||||||
|
|
||||||
|
|
||||||
|
def split_punct_ws(text, _punct=_punct_ws_str):
|
||||||
|
return [w for w in text.split(_punct) if w]
|
||||||
|
|
||||||
|
|
||||||
|
def camel2under(string):
|
||||||
|
return _camel2under_re.sub(r'_\1', string).lower()
|
||||||
|
|
||||||
|
|
||||||
|
def under2camel(string):
|
||||||
|
return ''.join(w.capitalize() or '_' for w in string.split('_'))
|
Loading…
Reference in New Issue