-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutil.py
More file actions
36 lines (28 loc) · 806 Bytes
/
util.py
File metadata and controls
36 lines (28 loc) · 806 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# Utility class
import string
import re
import logging
def debug(msg):
logger = logging.getLogger(__name__)
logger.debug(msg)
def log(msg):
logger = logging.getLogger(__name__)
logger.info(msg)
def to_snake_case(label):
"""
Convert a standard label to snake case variable name.
>>> to_snake_case("My Label")
'my_label'
>>> to_snake_case("State/Country")
'state_country'
>>> to_snake_case("my-label")
'my_label'
"""
filterpunct = re.compile("[%s\ ]" % string.punctuation)
alphafilter = re.compile("[%s%s_-]" % (string.digits,
string.ascii_letters))
name = filterpunct.sub("_", label.lower())
return "".join(alphafilter.findall(name))
if __name__ == '__main__':
import doctest
doctest.testmod()