Repository navigation
Expand file tree
/
Copy pathutils.py
More file actions
100 lines (82 loc) · 3.14 KB
/
Copy pathutils.py
File metadata and controls
100 lines (82 loc) · 3.14 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
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
99
100
import re
def generate_result(column_name=None, row=None, value=None, message=""):
return dict(
value=value,
row=row,
message=message,
column_name=column_name
)
def handle_xls_type(func):
""" 处理表内数据格式"""
def wrapper(self, *args, **kwargs):
new_list = []
xls_list = func(self, *args, **kwargs)
for value in xls_list:
if isinstance(value, (float, int)):
# 处理xlrd在转换数字时,会把int转换为float
new_list.append(xls_float_correct(value))
elif isinstance(value, str) and len(value.strip()) == 0:
# 判断数据为空
new_list.append(None)
else:
new_list.append(value)
return new_list
return wrapper
def xls_float_correct(num):
""" 用于处理xlrd在转换数字时,会把int转换为float"""
s = str(num)
s_split = s.split('.')
if len(s_split) == 1: # s = '1'
return int(num)
elif len(s_split) == 2: # s = '1.0'
if int(s_split[1]) == 0:
return int(s_split[0])
else:
return float(num)
else:
raise ValueError('传入值错误 num={}'.format(num))
def ncol_2_column(num):
""" 转换数字行数为表格中的字母列数"""
num = num - 1 # 本框架里所有表格相关的数值均从1开始计算
m = num // 26
r = num % 26
if m >= 1:
left = chr(m + 64)
right = chr(r + 65)
return '{}{}'.format(left, right)
else:
return chr(num + 65)
def get_type(value):
s = re.findall(r'<class \'(.+?)\'>', str(type(value)))
if s:
return s[0]
else:
raise ValueError('unknown error,can not get type: value={}, type={}'.format(value, type(value)))
def get_space(SpaceNum=1):
return '\t'*SpaceNum
def pprint(*args):
_str = []
for index, value in enumerate(args):
if isinstance(value, (dict, tuple, list)):
_str.append('[{index}]({type}) = {value}\n'.format(index=index, value=_print(value),
type=get_type(value)))
else:
_str.append('[{index}]({type}) = {value}\n'.format(index=index, value=value,
type=get_type(value)))
print(''.join(_str))
def _print(args, SpaceNum=1):
_str = []
SpaceNum += 1
if isinstance(args, (tuple, list)):
_str.append('')
for index, value in enumerate(args):
_str.append('{space}[{index}]({type}) = {value}'.format(index=index, value=_print(value, SpaceNum),
type=get_type(value), space=get_space(SpaceNum)))
elif isinstance(args, dict):
_str.append('')
for key, value in args.items():
_str.append('{space}[{key}]({type}) = {value}'.format(key=key, value=_print(value,SpaceNum),
type=get_type(value), space=get_space(SpaceNum)))
else:
_str.append(str(args))
return '\n'.join(_str)