-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathCamelTransformTool.py
More file actions
72 lines (57 loc) · 1.86 KB
/
Copy pathCamelTransformTool.py
File metadata and controls
72 lines (57 loc) · 1.86 KB
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
#!/usr/bin/env python
# coding=utf-8
import re
class CamelTransformTool(object):
"""
驼峰格式转换工具类
"""
def __init__(self):
pass
@staticmethod
def is_camel(field):
"""
判断是否为驼峰写法
:param field:
"""
pass
@staticmethod
def trans_underline_field_to_camel_field(field):
"""
将下划线写法转换为驼峰写法,步骤如下
0.转换为标准的以单个下划线分割的字符串,
1.单词转换为全小写
2.第一个单词首字母不变, 从第二个单词开始,首字母需大写
example:输入" OPEN ID "
0.转换为"OPEN_ID"
1.转换为"open_id"
2.转换为"openId"
:param field:
"""
for symbol in ' _\t\n':
field = field.strip(symbol)
word_list = re.sub(r'\s+|_+', '_', field).split('_')
word_len = len(word_list)
if word_len <= 1:
return field
camel_field = ""
for i in xrange(word_len):
if 0 == i:
camel_field += word_list[i].lower()
continue
camel_field += word_list[i].lower().capitalize()
return camel_field
@staticmethod
def trans_underline_field_to_camel_classname(field):
"""
类名输出为驼峰格式, 首字母需要大写
:param field: 类名
"""
field = CamelTransformTool.trans_underline_field_to_camel_field(field)
field = field[0].upper() + field[1:]
return field
"""
仅用于自测
"""
if __name__ == "__main__":
print CamelTransformTool.trans_underline_field_to_camel_field("OPEN_ID_HELLO_WORLD")
print CamelTransformTool.trans_underline_field_to_camel_classname("open_id_sheet")