-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathnaming_conventions.py
98 lines (88 loc) · 3.1 KB
/
naming_conventions.py
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import re
# precompiled regular expressions, used in name_parts
_RE_NAME_SEP = re.compile('[_\W]+')
"""Matches seperators for splitting names."""
_RE_NAME_DIGITS = re.compile('([0-9]+)|([a-zA-Z]+)')
"""Matches digits or characters for splitting names."""
_RE_NAME_CAMEL = re.compile('([A-Z][a-z]*)|([a-z]+)')
"""Finds components of camelCase and CamelCase names."""
_RE_NAME_LC = re.compile('[a-z]')
"""Matches a lower case character."""
_RE_NAME_UC = re.compile('[A-Z]')
"""Matches an upper case character."""
def name_parts(name):
"""Intelligently split a name into parts:
* first, split at non-alphanumeric characters
* next, seperate digits from characters
* finally, if some part has mixed case, it must be
camel case so split it further at upper case characters
>>> name_parts("hello_world")
['hello', 'world']
>>> name_parts("HELLO_WORLD")
['HELLO', 'WORLD']
>>> name_parts("HelloWorld")
['Hello', 'World']
>>> name_parts("helloWorld")
['hello', 'World']
>>> name_parts("xs:NMTOKEN")
['xs', 'NMTOKEN']
>>> name_parts("xs:NCName")
['xs', 'N', 'C', 'Name']
>>> name_parts('this IS a sillyNAME')
['this', 'IS', 'a', 'silly', 'N', 'A', 'M', 'E']
>>> name_parts('tHis is A Silly naME')
['t', 'His', 'is', 'A', 'Silly', 'na', 'M', 'E']
"""
# str(name) converts name to string in case it is a py2k
# unicode string
name = str(name)
# separate at symbols
parts = _RE_NAME_SEP.split(name)
# seperate digits
newparts = []
for part in parts:
for part_groups in _RE_NAME_DIGITS.findall(part):
for group in part_groups:
if group:
newparts.append(group)
break
parts = newparts
# separate at upper case characters for CamelCase and camelCase words
newparts = []
for part in parts:
if _RE_NAME_LC.search(part) and _RE_NAME_UC.search(part):
# find the camel bumps
for part_groups in _RE_NAME_CAMEL.findall(part):
for group in part_groups:
if group:
newparts.append(group)
break
else:
newparts.append(part)
parts = newparts
# return result
return parts
def name_attribute(name):
"""Converts an attribute name, as in the description file,
into a name usable by python.
:param name: The attribute name.
:type name: ``str``
:return: Reformatted attribute name, useable by python.
>>> name_attribute('tHis is A Silly naME')
't_his_is_a_silly_na_m_e'
>>> name_attribute('Test:Something')
'test_something'
>>> name_attribute('unknown?')
'unknown'
"""
return '_'.join(part.lower() for part in name_parts(name))
def name_class(name):
"""Converts a class name, as in the xsd file, into a name usable
by python.
:param name: The class name.
:type name: str
:return: Reformatted class name, useable by python.
>>> name_class('this IS a sillyNAME')
'ThisIsASillyNAME'
"""
return ''.join(part.capitalize() for part in name_parts(name))