Skip to content

Commit 6c4912c

Browse files
committed
add
1 parent 64b16e2 commit 6c4912c

File tree

2 files changed

+72
-10
lines changed

2 files changed

+72
-10
lines changed

Test.py

+16-10
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
# @Author : bajins https://www.bajins.com
55
# @File : Test.py
66
# @Version: 1.0.0
7-
# @Time : 2020/9/30 13:00
7+
# @Time : 5050/9/30 13:00
88
# @Project: scripts_python
99
# @Package:
1010
# @Software: PyCharm
@@ -18,20 +18,20 @@
1818
class Test(unittest.TestCase):
1919
@classmethod
2020
def setUpClass(cls): # 启动执行一次
21-
print("execute setUpClass")
21+
print("execute setUpClass".center(50, "="))
2222

2323
@classmethod
2424
def tearDownClass(cls): # 结束执行一次
25-
print("execute tearDownClass")
25+
print("execute tearDownClass".center(50, "="))
2626

2727
def setUp(self): # 每条用例执行前都要执行
28-
print("execute setUp")
28+
print("execute setUp".center(50, "="))
2929

3030
def tearDown(self): # 每条用例执行后都要执行
31-
print("execute tearDown")
31+
print("execute tearDown".center(50, "="))
3232

3333
def test_one(self): # 注意所有测试方法都需要以test开头
34-
print('execute test_one')
34+
print('execute test_one'.center(50, "="))
3535
self.assertTrue('FOO'.isupper())
3636

3737
def testReptileUtil(self):
@@ -52,10 +52,16 @@ def testSystemUtil(self):
5252
restart_process(os.path.abspath(__file__))
5353

5454
def testStringUtil(self):
55-
print(random.randint(1, 10))
56-
print(random.randint(2000, 2017))
57-
from utils.StringUtil import is_empty
58-
is_empty("")
55+
# print(random.randint(1, 10))
56+
# print(random.randint(5000, 5017))
57+
# from utils.StringUtil import is_empty
58+
# is_empty("")
59+
from utils import StringUtil
60+
print(StringUtil.hump_case_underline_lower("oneHTTPRequest12AaureBBBXu"))
61+
print(StringUtil.underline2hump("hello_word"))
62+
print(StringUtil.hump2underline("URLCamelCaseHello12Word"))
63+
json_str = """{"userName":"hi"}"""
64+
print(StringUtil.json_hump2underline(json_str))
5965

6066
def testFileUtil(self):
6167
from utils.FileUtil import count_dir_size

utils/StringUtil.py

+56
Original file line numberDiff line numberDiff line change
@@ -128,3 +128,59 @@ def random_string(length=16):
128128
"""
129129
str_list = [random.choice(string.digits + string.ascii_letters) for i in range(length)]
130130
return ''.join(str_list)
131+
132+
133+
def hump_case_underline_lower(text):
134+
"""
135+
驼峰(hump/camel)转下划线小写,已考虑到连续大写,不拆分数字
136+
:param text: 驼峰形式字符串
137+
:return: 字母全小写的下划线形式字符串
138+
"""
139+
lists = text[0]
140+
for i in range(1, len(text)):
141+
if text[i].isupper() and (not text[i - 1].isupper() or (text[i - 1].isupper() and text[i + 1].islower())):
142+
# 加'_',当前为大写,前一个字母为小写
143+
lists += '_'
144+
lists += text[i]
145+
else:
146+
lists += text[i]
147+
return lists.lower()
148+
149+
150+
def hump2underline(hump_str):
151+
"""
152+
驼峰形式字符串转成下划线形式,已考虑到连续大写,拆分数字
153+
:param hump_str: 驼峰形式字符串
154+
:return: 字母全小写并拆分数字的下划线形式字符串
155+
"""
156+
# 第一个参数匹配正则,匹配小写字母和大写字母的分界位置
157+
# 第二个参数使用了正则分组的后向引用
158+
hump_str = re.sub('(.)([A-Z][a-z]+)', r'\1_\2', hump_str)
159+
# 匹配字母与数字分界位置
160+
hump_str = re.sub('(.)([0-9]+)', r'\1_\2', hump_str)
161+
# 匹配小写字母数字与大写字母分界位置
162+
return re.sub('([a-z0-9])([A-Z])', r'\1_\2', hump_str).lower()
163+
164+
165+
def underline2hump(underline_str):
166+
"""
167+
下划线形式字符串转成驼峰形式
168+
:param underline_str: 下划线形式字符串
169+
:return: 驼峰形式字符串
170+
"""
171+
# 这里re.sub()函数第二个替换参数用到了一个匿名回调函数,回调函数的参数x为一个匹配对象,返回值为一个处理后的字符串
172+
return re.sub(r'(_\w)', lambda x: x.group(1)[1].upper(), underline_str)
173+
174+
175+
def json_hump2underline(hump_json_str):
176+
"""
177+
把一个json字符串中的所有字段名都从驼峰形式替换成下划线形式。
178+
注意:因为考虑到json可能具有多层嵌套的复杂结构,所以这里直接采用正则文本替换的方式进行处理,而不是采用把json转成字典再进行处理的方式
179+
:param hump_json_str: 字段名为驼峰形式的json字符串
180+
:return: 字段名为下划线形式的json字符串
181+
"""
182+
# 从json字符串中匹配字段名的正则
183+
# 注:这里的字段名只考虑由英文字母、数字、下划线组成
184+
attr_ptn = re.compile(r'"\s*(\w+)\s*"\s*:')
185+
# 使用hump2underline函数作为re.sub函数第二个参数的回调函数
186+
return re.sub(attr_ptn, lambda x: '"' + hump2underline(x.group(1)) + '" :', hump_json_str)

0 commit comments

Comments
 (0)