pyxllib 0.3.197__py3-none-any.whl → 0.3.200__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- pyxllib/__init__.py +21 -21
- pyxllib/algo/__init__.py +8 -8
- pyxllib/algo/disjoint.py +54 -54
- pyxllib/algo/geo.py +541 -541
- pyxllib/algo/intervals.py +964 -964
- pyxllib/algo/matcher.py +389 -389
- pyxllib/algo/newbie.py +166 -166
- pyxllib/algo/pupil.py +629 -629
- pyxllib/algo/shapelylib.py +67 -67
- pyxllib/algo/specialist.py +241 -241
- pyxllib/algo/stat.py +494 -494
- pyxllib/algo/treelib.py +149 -149
- pyxllib/algo/unitlib.py +66 -66
- pyxllib/autogui/__init__.py +5 -5
- pyxllib/autogui/activewin.py +246 -246
- pyxllib/autogui/all.py +9 -9
- pyxllib/autogui/autogui.py +852 -852
- pyxllib/autogui/uiautolib.py +362 -362
- pyxllib/autogui/virtualkey.py +102 -102
- pyxllib/autogui/wechat.py +827 -827
- pyxllib/autogui/wechat_msg.py +421 -421
- pyxllib/autogui/wxautolib.py +84 -84
- pyxllib/cv/__init__.py +5 -5
- pyxllib/cv/expert.py +267 -267
- pyxllib/cv/imfile.py +159 -159
- pyxllib/cv/imhash.py +39 -39
- pyxllib/cv/pupil.py +9 -9
- pyxllib/cv/rgbfmt.py +1525 -1525
- pyxllib/cv/slidercaptcha.py +137 -137
- pyxllib/cv/trackbartools.py +251 -251
- pyxllib/cv/xlcvlib.py +1040 -1040
- pyxllib/cv/xlpillib.py +423 -423
- pyxllib/data/echarts.py +240 -240
- pyxllib/data/jsonlib.py +89 -89
- pyxllib/data/oss.py +72 -72
- pyxllib/data/pglib.py +1127 -1127
- pyxllib/data/sqlite.py +568 -568
- pyxllib/data/sqllib.py +297 -297
- pyxllib/ext/JLineViewer.py +505 -505
- pyxllib/ext/__init__.py +6 -6
- pyxllib/ext/demolib.py +246 -246
- pyxllib/ext/drissionlib.py +277 -277
- pyxllib/ext/kq5034lib.py +12 -12
- pyxllib/ext/old.py +663 -663
- pyxllib/ext/qt.py +449 -449
- pyxllib/ext/robustprocfile.py +497 -497
- pyxllib/ext/seleniumlib.py +76 -76
- pyxllib/ext/tk.py +173 -173
- pyxllib/ext/unixlib.py +827 -827
- pyxllib/ext/utools.py +351 -351
- pyxllib/ext/webhook.py +124 -119
- pyxllib/ext/win32lib.py +40 -40
- pyxllib/ext/wjxlib.py +88 -88
- pyxllib/ext/wpsapi.py +124 -124
- pyxllib/ext/xlwork.py +9 -9
- pyxllib/ext/yuquelib.py +1105 -1105
- pyxllib/file/__init__.py +17 -17
- pyxllib/file/docxlib.py +761 -761
- pyxllib/file/gitlib.py +309 -309
- pyxllib/file/libreoffice.py +165 -165
- pyxllib/file/movielib.py +148 -148
- pyxllib/file/newbie.py +10 -10
- pyxllib/file/onenotelib.py +1469 -1469
- pyxllib/file/packlib/__init__.py +330 -330
- pyxllib/file/packlib/zipfile.py +2441 -2441
- pyxllib/file/pdflib.py +426 -426
- pyxllib/file/pupil.py +185 -185
- pyxllib/file/specialist/__init__.py +685 -685
- pyxllib/file/specialist/dirlib.py +799 -799
- pyxllib/file/specialist/download.py +193 -193
- pyxllib/file/specialist/filelib.py +2829 -2829
- pyxllib/file/xlsxlib.py +3131 -3131
- pyxllib/file/xlsyncfile.py +341 -341
- pyxllib/prog/__init__.py +5 -5
- pyxllib/prog/cachetools.py +64 -64
- pyxllib/prog/deprecatedlib.py +233 -233
- pyxllib/prog/filelock.py +42 -42
- pyxllib/prog/ipyexec.py +253 -253
- pyxllib/prog/multiprogs.py +940 -940
- pyxllib/prog/newbie.py +451 -451
- pyxllib/prog/pupil.py +1197 -1197
- pyxllib/prog/sitepackages.py +33 -33
- pyxllib/prog/specialist/__init__.py +391 -391
- pyxllib/prog/specialist/bc.py +203 -203
- pyxllib/prog/specialist/browser.py +497 -497
- pyxllib/prog/specialist/common.py +347 -347
- pyxllib/prog/specialist/datetime.py +198 -198
- pyxllib/prog/specialist/tictoc.py +240 -240
- pyxllib/prog/specialist/xllog.py +180 -180
- pyxllib/prog/xlosenv.py +108 -108
- pyxllib/stdlib/__init__.py +17 -17
- pyxllib/stdlib/tablepyxl/__init__.py +10 -10
- pyxllib/stdlib/tablepyxl/style.py +303 -303
- pyxllib/stdlib/tablepyxl/tablepyxl.py +130 -130
- pyxllib/text/__init__.py +8 -8
- pyxllib/text/ahocorasick.py +39 -39
- pyxllib/text/airscript.js +744 -744
- pyxllib/text/charclasslib.py +121 -121
- pyxllib/text/jiebalib.py +267 -267
- pyxllib/text/jinjalib.py +32 -32
- pyxllib/text/jsa_ai_prompt.md +271 -271
- pyxllib/text/jscode.py +922 -922
- pyxllib/text/latex/__init__.py +158 -158
- pyxllib/text/levenshtein.py +303 -303
- pyxllib/text/nestenv.py +1215 -1215
- pyxllib/text/newbie.py +300 -300
- pyxllib/text/pupil/__init__.py +8 -8
- pyxllib/text/pupil/common.py +1121 -1121
- pyxllib/text/pupil/xlalign.py +326 -326
- pyxllib/text/pycode.py +47 -47
- pyxllib/text/specialist/__init__.py +8 -8
- pyxllib/text/specialist/common.py +112 -112
- pyxllib/text/specialist/ptag.py +186 -186
- pyxllib/text/spellchecker.py +172 -172
- pyxllib/text/templates/echart_base.html +10 -10
- pyxllib/text/templates/highlight_code.html +16 -16
- pyxllib/text/templates/latex_editor.html +102 -102
- pyxllib/text/vbacode.py +17 -17
- pyxllib/text/xmllib.py +747 -747
- pyxllib/xl.py +42 -39
- pyxllib/xlcv.py +17 -17
- {pyxllib-0.3.197.dist-info → pyxllib-0.3.200.dist-info}/METADATA +1 -1
- pyxllib-0.3.200.dist-info/RECORD +126 -0
- {pyxllib-0.3.197.dist-info → pyxllib-0.3.200.dist-info}/licenses/LICENSE +190 -190
- pyxllib-0.3.197.dist-info/RECORD +0 -126
- {pyxllib-0.3.197.dist-info → pyxllib-0.3.200.dist-info}/WHEEL +0 -0
pyxllib/text/pupil/xlalign.py
CHANGED
@@ -1,326 +1,326 @@
|
|
1
|
-
#!/usr/bin/env python3
|
2
|
-
# -*- coding: utf-8 -*-
|
3
|
-
# @Author : 陈坤泽
|
4
|
-
# @Email : 877362867@qq.com
|
5
|
-
# @Date : 2020/06/01
|
6
|
-
|
7
|
-
|
8
|
-
"""很久以前搞得一个,按照中文域宽对齐相关操作
|
9
|
-
也是 pyxllib v0.0.xx 最底层基础的__str.py 文件
|
10
|
-
"""
|
11
|
-
|
12
|
-
import copy
|
13
|
-
import re
|
14
|
-
import textwrap
|
15
|
-
|
16
|
-
from pyxllib.prog.newbie import len_in_dim2, GrowingList
|
17
|
-
from pyxllib.prog.pupil import run_once
|
18
|
-
|
19
|
-
|
20
|
-
def strwidth(s):
|
21
|
-
""" string width
|
22
|
-
|
23
|
-
中英字符串实际宽度
|
24
|
-
>>> strwidth('ab')
|
25
|
-
2
|
26
|
-
>>> strwidth('a⑪中⑩')
|
27
|
-
7
|
28
|
-
|
29
|
-
⑩等字符的宽度还是跟字体有关的,不过在大部分地方好像都是域宽2,目前算法问题不大
|
30
|
-
"""
|
31
|
-
try:
|
32
|
-
res = len(s.encode('gbk'))
|
33
|
-
except UnicodeEncodeError:
|
34
|
-
count = len(s)
|
35
|
-
for x in s:
|
36
|
-
if ord(x) > 127:
|
37
|
-
count += 1
|
38
|
-
res = count
|
39
|
-
return res
|
40
|
-
|
41
|
-
|
42
|
-
@run_once('str')
|
43
|
-
def get_strwidth(s):
|
44
|
-
""" 带缓存的版本 """
|
45
|
-
return strwidth(s)
|
46
|
-
|
47
|
-
|
48
|
-
def strwidth_proc(s, fmt='r', chinese_char_width=1.8):
|
49
|
-
""" 此函数可以用于每个汉字域宽是w=1.8等奇怪的情况
|
50
|
-
|
51
|
-
为了让字符串域宽为一个整数,需要补充中文空格,会对原始字符串进行修改。
|
52
|
-
故返回值有2个,第1个是修正后的字符串s,第2个是实际宽度w。
|
53
|
-
|
54
|
-
:param s: 一个字符串
|
55
|
-
:param fmt: 目标对齐格式
|
56
|
-
:param chinese_char_width: 每个汉字字符宽度
|
57
|
-
:return: (s, w)
|
58
|
-
s: 修正后的字符串值s
|
59
|
-
w: 修正后字符串的实际宽度
|
60
|
-
|
61
|
-
>>> strwidth_proc('哈哈a', chinese_char_width=1.8)
|
62
|
-
(' 哈哈a', 10)
|
63
|
-
"""
|
64
|
-
# 1 计算一些参数值
|
65
|
-
s = str(s) # 确保是字符串类型
|
66
|
-
l1 = len(s)
|
67
|
-
l2 = strwidth(s)
|
68
|
-
y = l2 - l1 # 中文字符数
|
69
|
-
x = l1 - y # 英文字符数
|
70
|
-
ch = chr(12288) # 中文空格
|
71
|
-
w = x + y * chinese_char_width # 当前字符串宽度
|
72
|
-
# 2 计算需要补充t个中文空格
|
73
|
-
error = 0.05 # 允许误差范围
|
74
|
-
t = 0 # 需要补充中文字符数
|
75
|
-
while error < w % 1 < 1 - error: # 小数部分超过误差
|
76
|
-
t += 1
|
77
|
-
w += chinese_char_width
|
78
|
-
# 3 补充中文字符
|
79
|
-
if t:
|
80
|
-
if fmt == 'r':
|
81
|
-
s = ch * t + s
|
82
|
-
elif fmt == 'l':
|
83
|
-
s = s + ch * t
|
84
|
-
else:
|
85
|
-
s = ch * (t - t // 2) + s + ch * (t // 2)
|
86
|
-
return s, int(w)
|
87
|
-
|
88
|
-
|
89
|
-
def realign(text, least_blank=4, tab2blank=4, support_chinese=False, sep=None):
|
90
|
-
r""" 一列文本的对齐
|
91
|
-
:param text: 一段文本
|
92
|
-
支持每行列数不同
|
93
|
-
:param least_blank: 每列最少间距空格数
|
94
|
-
:param tab2blank:
|
95
|
-
:param support_chinese: 支持中文域宽计算
|
96
|
-
:param sep: 每列分隔符,默认为least_blank个空格
|
97
|
-
:return: 对齐美化的一段文本
|
98
|
-
|
99
|
-
>>> realign(' Aget keep hold show\nmaking selling giving collecting')
|
100
|
-
'Aget keep hold show\nmaking selling giving collecting'
|
101
|
-
"""
|
102
|
-
# 1 预处理
|
103
|
-
s = text.replace('\t', ' ' * tab2blank)
|
104
|
-
s = re.sub(' {' + str(least_blank) + ',}', r'\t', s) # 统一用\t作为分隔符
|
105
|
-
lenfunc = strwidth if support_chinese else len
|
106
|
-
if sep is None: sep = ' ' * least_blank
|
107
|
-
|
108
|
-
# 2 计算出每一列的最大宽度
|
109
|
-
lines = s.splitlines()
|
110
|
-
n = len(lines)
|
111
|
-
max_width = GrowingList() # 因为不知道有多少列,用自增长的list来存储每一列的最大宽度
|
112
|
-
for i, line in enumerate(lines):
|
113
|
-
line = line.strip().split('\t')
|
114
|
-
m = len(line)
|
115
|
-
for j in range(m): max_width[j] = max(max_width[j] or 0, lenfunc(line[j]))
|
116
|
-
lines[i] = line
|
117
|
-
if len(max_width) == 1: return '\n'.join(map(lambda x: x[0], lines))
|
118
|
-
|
119
|
-
# 3 重组内容
|
120
|
-
for i, line in enumerate(lines):
|
121
|
-
for j in range(len(line) - 1): line[j] += ' ' * (max_width[j] - lenfunc(line[j])) # 注意最后一列就不用加空格了
|
122
|
-
lines[i] = sep.join(line)
|
123
|
-
return '\n'.join(lines)
|
124
|
-
|
125
|
-
|
126
|
-
def listalign(ls, fmt='r', *, width=None, fillchar=' ', prefix='', suffix='', chinese_char_width=2):
|
127
|
-
"""文档: https://blog.csdn.net/code4101/article/details/80985218(不过文档有些过时了)
|
128
|
-
|
129
|
-
listalign列表对齐
|
130
|
-
py3中str的len是计算字符数量,例如len('ab') --> 2, len('a中b') --> 3。
|
131
|
-
但在对齐等操作中,是需要将每个汉字当成宽度2来处理,计算字符串实际宽度的。
|
132
|
-
所以我们需要开发一个strwidth函数,效果: strwidth('ab') --> 2,strwidth('a中b') --> 4。
|
133
|
-
|
134
|
-
:param ls:
|
135
|
-
要处理的列表,会对所有元素调用str处理,确保全部转为string类型
|
136
|
-
且会将换行符转为\n显示
|
137
|
-
:param fmt: (format)
|
138
|
-
l: left,左对齐
|
139
|
-
c: center,居中
|
140
|
-
r: right,右对齐
|
141
|
-
多个字符: 扩展fmt长度跟ls一样,每一个元素单独设置对齐格式。如果fmt长度小于ls,则扩展的格式按照fmt[-1]设置
|
142
|
-
:param width:
|
143
|
-
None或者设置值小于最长字符串: 不设域宽,直接按照最长的字符串为准
|
144
|
-
:param fillchar: 填充字符
|
145
|
-
:param prefix: 添加前缀
|
146
|
-
:param suffix: 添加后缀
|
147
|
-
:param chinese_char_width: 每个汉字字符宽度
|
148
|
-
|
149
|
-
:return:
|
150
|
-
对齐后的数组ls,每个元素会转为str类型
|
151
|
-
|
152
|
-
>>> listalign(['a', '哈哈', 'ccd'])
|
153
|
-
[' a', '哈哈', ' ccd']
|
154
|
-
>>> listalign(['a', '哈哈', 'ccd'], chinese_char_width=1.8)
|
155
|
-
[' a', ' 哈哈', ' ccd']
|
156
|
-
"""
|
157
|
-
# 1 处理fmt数组
|
158
|
-
if len(fmt) == 1:
|
159
|
-
fmt = [fmt] * len(ls)
|
160
|
-
elif len(fmt) < len(ls):
|
161
|
-
fmt = list(fmt) + [fmt[-1]] * (len(ls) - len(fmt))
|
162
|
-
|
163
|
-
# 2 算出需要域宽
|
164
|
-
if chinese_char_width == 2:
|
165
|
-
strs = [str(x).replace('\n', r'\n') for x in ls] # 存储转成字符串的元素
|
166
|
-
lens = [strwidth(x) for x in strs] # 存储每个元素的实际域宽
|
167
|
-
else:
|
168
|
-
strs = [] # 存储转成字符串的元素
|
169
|
-
lens = [] # 存储每个元素的实际域宽
|
170
|
-
for i, t in enumerate(ls):
|
171
|
-
t, n = strwidth_proc(t, fmt[i], chinese_char_width)
|
172
|
-
strs.append(t)
|
173
|
-
lens.append(n)
|
174
|
-
w = max(lens)
|
175
|
-
if width and isinstance(width, int) and width > w:
|
176
|
-
w = width
|
177
|
-
|
178
|
-
# 3 对齐操作
|
179
|
-
for i, s in enumerate(strs):
|
180
|
-
if fmt[i] == 'r':
|
181
|
-
strs[i] = fillchar * (w - lens[i]) + strs[i]
|
182
|
-
elif fmt[i] == 'l':
|
183
|
-
strs[i] = strs[i] + fillchar * (w - lens[i])
|
184
|
-
elif fmt[i] == 'c':
|
185
|
-
t = w - lens[i]
|
186
|
-
strs[i] = fillchar * (t - t // 2) + strs[i] + fillchar * (t // 2)
|
187
|
-
strs[i] = prefix + strs[i] + suffix
|
188
|
-
return strs
|
189
|
-
|
190
|
-
|
191
|
-
def arr_hangclear(arr, depth=None):
|
192
|
-
""" 清除连续相同值,简化表格内容
|
193
|
-
>> arr_hangclear(arr, depth=2)
|
194
|
-
原表格:
|
195
|
-
A B D
|
196
|
-
A B E
|
197
|
-
A C E
|
198
|
-
A C E
|
199
|
-
新表格:
|
200
|
-
A B D
|
201
|
-
E
|
202
|
-
C E
|
203
|
-
E
|
204
|
-
|
205
|
-
:param arr: 二维数组
|
206
|
-
:param depth: 处理列上限
|
207
|
-
例如depth=1,则只处理第一层
|
208
|
-
depth=None,则处理所有列
|
209
|
-
|
210
|
-
>>> arr_hangclear([[1, 2, 4], [1, 2, 5], [1, 3, 5], [1, 3, 5]])
|
211
|
-
[[1, 2, 4], ['', '', 5], ['', 3, 5], ['', '', 5]]
|
212
|
-
>>> arr_hangclear([[1, 2, 4], [1, 2, 5], [2, 2, 5], [1, 2, 5]])
|
213
|
-
[[1, 2, 4], ['', '', 5], [2, 2, 5], [1, 2, 5]]
|
214
|
-
"""
|
215
|
-
m = depth or len_in_dim2(arr) - 1
|
216
|
-
a = copy.deepcopy(arr)
|
217
|
-
|
218
|
-
# 算法原理:从下到上,从右到左判断与上一行重叠了几列数据
|
219
|
-
for i in range(len(arr) - 1, 0, -1):
|
220
|
-
for j in range(m):
|
221
|
-
if a[i][j] == a[i - 1][j]:
|
222
|
-
a[i][j] = ''
|
223
|
-
else:
|
224
|
-
break
|
225
|
-
return a
|
226
|
-
|
227
|
-
|
228
|
-
def arr2table(arr, rowmerge=False):
|
229
|
-
""" 数组转html表格代码
|
230
|
-
|
231
|
-
:param arr: 需要处理的数组
|
232
|
-
:param rowmerge: 行单元格合并
|
233
|
-
:return: html文本格式的<table>
|
234
|
-
|
235
|
-
这个arr2table是用来画合并单元格的
|
236
|
-
>> browser(arr2table([['A', 1, 'a'], ['', 2, 'b'], ['B', 3, 'c'], ['', '', 'd'], ['', 5, 'e']], True), 'a.html')
|
237
|
-
效果图:http://i1.fuimg.com/582188/c452f40b5a072f8d.png
|
238
|
-
"""
|
239
|
-
n = len(arr)
|
240
|
-
m = len_in_dim2(arr)
|
241
|
-
res = ['<table border="1"><tbody>']
|
242
|
-
for i, line in enumerate(arr):
|
243
|
-
res.append('<tr>')
|
244
|
-
for j, ele in enumerate(line):
|
245
|
-
if rowmerge:
|
246
|
-
if ele != '':
|
247
|
-
cnt = 1
|
248
|
-
while i + cnt < n and arr[i + cnt][j] == '':
|
249
|
-
for k in range(j - 1, -1, -1):
|
250
|
-
if arr[i + cnt][k] != '':
|
251
|
-
break
|
252
|
-
else:
|
253
|
-
cnt += 1
|
254
|
-
continue
|
255
|
-
break
|
256
|
-
if cnt > 1:
|
257
|
-
res.append(f'<td rowspan="{cnt}">{ele}</td>')
|
258
|
-
else:
|
259
|
-
res.append(f'<td>{ele}</td>')
|
260
|
-
elif j == m - 1:
|
261
|
-
res.append(f'<td>{ele}</td>')
|
262
|
-
else:
|
263
|
-
res.append(f'<td>{ele}</td>')
|
264
|
-
res.append('</tr>')
|
265
|
-
res.append('</tbody></table>')
|
266
|
-
return ''.join(res)
|
267
|
-
|
268
|
-
|
269
|
-
def east_asian_len(s, ambiguous_width=None):
|
270
|
-
import pandas.io.formats.format as fmt
|
271
|
-
return fmt.EastAsianTextAdjustment().len(s)
|
272
|
-
|
273
|
-
|
274
|
-
def east_asian_shorten(s, width=50, placeholder='...'):
|
275
|
-
"""考虑中文情况下的域宽截断
|
276
|
-
|
277
|
-
:param s: 要处理的字符串
|
278
|
-
:param width: 宽度上限,仅能达到width-1的宽度
|
279
|
-
:param placeholder: 如果做了截断,末尾补足字符
|
280
|
-
|
281
|
-
# width比placeholder还小
|
282
|
-
>>> east_asian_shorten('a', 2)
|
283
|
-
'a'
|
284
|
-
>>> east_asian_shorten('a啊b'*4, 3)
|
285
|
-
'..'
|
286
|
-
>>> east_asian_shorten('a啊b'*4, 4)
|
287
|
-
'...'
|
288
|
-
|
289
|
-
>>> east_asian_shorten('a啊b'*4, 5, '...')
|
290
|
-
'a...'
|
291
|
-
>>> east_asian_shorten('a啊b'*4, 11)
|
292
|
-
'a啊ba啊...'
|
293
|
-
>>> east_asian_shorten('a啊b'*4, 16, '...')
|
294
|
-
'a啊ba啊ba啊b...'
|
295
|
-
>>> east_asian_shorten('a啊b'*4, 18, '...')
|
296
|
-
'a啊ba啊ba啊ba啊b'
|
297
|
-
"""
|
298
|
-
# 一、如果字符串本身不到width设限,返回原值
|
299
|
-
s = textwrap.shorten(s, width * 3, placeholder='') # 用textwrap的折行功能,尽量不删除文本
|
300
|
-
n = east_asian_len(s)
|
301
|
-
if n < width: return s
|
302
|
-
|
303
|
-
# 二、如果输入的width比placeholder还短
|
304
|
-
width -= 1
|
305
|
-
m = east_asian_len(placeholder)
|
306
|
-
if width <= m:
|
307
|
-
return placeholder[:width]
|
308
|
-
|
309
|
-
# 三、需要添加 placeholder
|
310
|
-
# 1 计算长度
|
311
|
-
width -= m
|
312
|
-
|
313
|
-
# 2 截取s
|
314
|
-
try:
|
315
|
-
s = s.encode('gbk')[:width].decode('gbk', errors='ignore')
|
316
|
-
except UnicodeEncodeError:
|
317
|
-
i, count = 0, m
|
318
|
-
while i < n and count <= width:
|
319
|
-
if ord(s[i]) > 127:
|
320
|
-
count += 2
|
321
|
-
else:
|
322
|
-
count += 1
|
323
|
-
i += 1
|
324
|
-
s = s[:i]
|
325
|
-
|
326
|
-
return s + placeholder
|
1
|
+
#!/usr/bin/env python3
|
2
|
+
# -*- coding: utf-8 -*-
|
3
|
+
# @Author : 陈坤泽
|
4
|
+
# @Email : 877362867@qq.com
|
5
|
+
# @Date : 2020/06/01
|
6
|
+
|
7
|
+
|
8
|
+
"""很久以前搞得一个,按照中文域宽对齐相关操作
|
9
|
+
也是 pyxllib v0.0.xx 最底层基础的__str.py 文件
|
10
|
+
"""
|
11
|
+
|
12
|
+
import copy
|
13
|
+
import re
|
14
|
+
import textwrap
|
15
|
+
|
16
|
+
from pyxllib.prog.newbie import len_in_dim2, GrowingList
|
17
|
+
from pyxllib.prog.pupil import run_once
|
18
|
+
|
19
|
+
|
20
|
+
def strwidth(s):
|
21
|
+
""" string width
|
22
|
+
|
23
|
+
中英字符串实际宽度
|
24
|
+
>>> strwidth('ab')
|
25
|
+
2
|
26
|
+
>>> strwidth('a⑪中⑩')
|
27
|
+
7
|
28
|
+
|
29
|
+
⑩等字符的宽度还是跟字体有关的,不过在大部分地方好像都是域宽2,目前算法问题不大
|
30
|
+
"""
|
31
|
+
try:
|
32
|
+
res = len(s.encode('gbk'))
|
33
|
+
except UnicodeEncodeError:
|
34
|
+
count = len(s)
|
35
|
+
for x in s:
|
36
|
+
if ord(x) > 127:
|
37
|
+
count += 1
|
38
|
+
res = count
|
39
|
+
return res
|
40
|
+
|
41
|
+
|
42
|
+
@run_once('str')
|
43
|
+
def get_strwidth(s):
|
44
|
+
""" 带缓存的版本 """
|
45
|
+
return strwidth(s)
|
46
|
+
|
47
|
+
|
48
|
+
def strwidth_proc(s, fmt='r', chinese_char_width=1.8):
|
49
|
+
""" 此函数可以用于每个汉字域宽是w=1.8等奇怪的情况
|
50
|
+
|
51
|
+
为了让字符串域宽为一个整数,需要补充中文空格,会对原始字符串进行修改。
|
52
|
+
故返回值有2个,第1个是修正后的字符串s,第2个是实际宽度w。
|
53
|
+
|
54
|
+
:param s: 一个字符串
|
55
|
+
:param fmt: 目标对齐格式
|
56
|
+
:param chinese_char_width: 每个汉字字符宽度
|
57
|
+
:return: (s, w)
|
58
|
+
s: 修正后的字符串值s
|
59
|
+
w: 修正后字符串的实际宽度
|
60
|
+
|
61
|
+
>>> strwidth_proc('哈哈a', chinese_char_width=1.8)
|
62
|
+
(' 哈哈a', 10)
|
63
|
+
"""
|
64
|
+
# 1 计算一些参数值
|
65
|
+
s = str(s) # 确保是字符串类型
|
66
|
+
l1 = len(s)
|
67
|
+
l2 = strwidth(s)
|
68
|
+
y = l2 - l1 # 中文字符数
|
69
|
+
x = l1 - y # 英文字符数
|
70
|
+
ch = chr(12288) # 中文空格
|
71
|
+
w = x + y * chinese_char_width # 当前字符串宽度
|
72
|
+
# 2 计算需要补充t个中文空格
|
73
|
+
error = 0.05 # 允许误差范围
|
74
|
+
t = 0 # 需要补充中文字符数
|
75
|
+
while error < w % 1 < 1 - error: # 小数部分超过误差
|
76
|
+
t += 1
|
77
|
+
w += chinese_char_width
|
78
|
+
# 3 补充中文字符
|
79
|
+
if t:
|
80
|
+
if fmt == 'r':
|
81
|
+
s = ch * t + s
|
82
|
+
elif fmt == 'l':
|
83
|
+
s = s + ch * t
|
84
|
+
else:
|
85
|
+
s = ch * (t - t // 2) + s + ch * (t // 2)
|
86
|
+
return s, int(w)
|
87
|
+
|
88
|
+
|
89
|
+
def realign(text, least_blank=4, tab2blank=4, support_chinese=False, sep=None):
|
90
|
+
r""" 一列文本的对齐
|
91
|
+
:param text: 一段文本
|
92
|
+
支持每行列数不同
|
93
|
+
:param least_blank: 每列最少间距空格数
|
94
|
+
:param tab2blank:
|
95
|
+
:param support_chinese: 支持中文域宽计算
|
96
|
+
:param sep: 每列分隔符,默认为least_blank个空格
|
97
|
+
:return: 对齐美化的一段文本
|
98
|
+
|
99
|
+
>>> realign(' Aget keep hold show\nmaking selling giving collecting')
|
100
|
+
'Aget keep hold show\nmaking selling giving collecting'
|
101
|
+
"""
|
102
|
+
# 1 预处理
|
103
|
+
s = text.replace('\t', ' ' * tab2blank)
|
104
|
+
s = re.sub(' {' + str(least_blank) + ',}', r'\t', s) # 统一用\t作为分隔符
|
105
|
+
lenfunc = strwidth if support_chinese else len
|
106
|
+
if sep is None: sep = ' ' * least_blank
|
107
|
+
|
108
|
+
# 2 计算出每一列的最大宽度
|
109
|
+
lines = s.splitlines()
|
110
|
+
n = len(lines)
|
111
|
+
max_width = GrowingList() # 因为不知道有多少列,用自增长的list来存储每一列的最大宽度
|
112
|
+
for i, line in enumerate(lines):
|
113
|
+
line = line.strip().split('\t')
|
114
|
+
m = len(line)
|
115
|
+
for j in range(m): max_width[j] = max(max_width[j] or 0, lenfunc(line[j]))
|
116
|
+
lines[i] = line
|
117
|
+
if len(max_width) == 1: return '\n'.join(map(lambda x: x[0], lines))
|
118
|
+
|
119
|
+
# 3 重组内容
|
120
|
+
for i, line in enumerate(lines):
|
121
|
+
for j in range(len(line) - 1): line[j] += ' ' * (max_width[j] - lenfunc(line[j])) # 注意最后一列就不用加空格了
|
122
|
+
lines[i] = sep.join(line)
|
123
|
+
return '\n'.join(lines)
|
124
|
+
|
125
|
+
|
126
|
+
def listalign(ls, fmt='r', *, width=None, fillchar=' ', prefix='', suffix='', chinese_char_width=2):
|
127
|
+
"""文档: https://blog.csdn.net/code4101/article/details/80985218(不过文档有些过时了)
|
128
|
+
|
129
|
+
listalign列表对齐
|
130
|
+
py3中str的len是计算字符数量,例如len('ab') --> 2, len('a中b') --> 3。
|
131
|
+
但在对齐等操作中,是需要将每个汉字当成宽度2来处理,计算字符串实际宽度的。
|
132
|
+
所以我们需要开发一个strwidth函数,效果: strwidth('ab') --> 2,strwidth('a中b') --> 4。
|
133
|
+
|
134
|
+
:param ls:
|
135
|
+
要处理的列表,会对所有元素调用str处理,确保全部转为string类型
|
136
|
+
且会将换行符转为\n显示
|
137
|
+
:param fmt: (format)
|
138
|
+
l: left,左对齐
|
139
|
+
c: center,居中
|
140
|
+
r: right,右对齐
|
141
|
+
多个字符: 扩展fmt长度跟ls一样,每一个元素单独设置对齐格式。如果fmt长度小于ls,则扩展的格式按照fmt[-1]设置
|
142
|
+
:param width:
|
143
|
+
None或者设置值小于最长字符串: 不设域宽,直接按照最长的字符串为准
|
144
|
+
:param fillchar: 填充字符
|
145
|
+
:param prefix: 添加前缀
|
146
|
+
:param suffix: 添加后缀
|
147
|
+
:param chinese_char_width: 每个汉字字符宽度
|
148
|
+
|
149
|
+
:return:
|
150
|
+
对齐后的数组ls,每个元素会转为str类型
|
151
|
+
|
152
|
+
>>> listalign(['a', '哈哈', 'ccd'])
|
153
|
+
[' a', '哈哈', ' ccd']
|
154
|
+
>>> listalign(['a', '哈哈', 'ccd'], chinese_char_width=1.8)
|
155
|
+
[' a', ' 哈哈', ' ccd']
|
156
|
+
"""
|
157
|
+
# 1 处理fmt数组
|
158
|
+
if len(fmt) == 1:
|
159
|
+
fmt = [fmt] * len(ls)
|
160
|
+
elif len(fmt) < len(ls):
|
161
|
+
fmt = list(fmt) + [fmt[-1]] * (len(ls) - len(fmt))
|
162
|
+
|
163
|
+
# 2 算出需要域宽
|
164
|
+
if chinese_char_width == 2:
|
165
|
+
strs = [str(x).replace('\n', r'\n') for x in ls] # 存储转成字符串的元素
|
166
|
+
lens = [strwidth(x) for x in strs] # 存储每个元素的实际域宽
|
167
|
+
else:
|
168
|
+
strs = [] # 存储转成字符串的元素
|
169
|
+
lens = [] # 存储每个元素的实际域宽
|
170
|
+
for i, t in enumerate(ls):
|
171
|
+
t, n = strwidth_proc(t, fmt[i], chinese_char_width)
|
172
|
+
strs.append(t)
|
173
|
+
lens.append(n)
|
174
|
+
w = max(lens)
|
175
|
+
if width and isinstance(width, int) and width > w:
|
176
|
+
w = width
|
177
|
+
|
178
|
+
# 3 对齐操作
|
179
|
+
for i, s in enumerate(strs):
|
180
|
+
if fmt[i] == 'r':
|
181
|
+
strs[i] = fillchar * (w - lens[i]) + strs[i]
|
182
|
+
elif fmt[i] == 'l':
|
183
|
+
strs[i] = strs[i] + fillchar * (w - lens[i])
|
184
|
+
elif fmt[i] == 'c':
|
185
|
+
t = w - lens[i]
|
186
|
+
strs[i] = fillchar * (t - t // 2) + strs[i] + fillchar * (t // 2)
|
187
|
+
strs[i] = prefix + strs[i] + suffix
|
188
|
+
return strs
|
189
|
+
|
190
|
+
|
191
|
+
def arr_hangclear(arr, depth=None):
|
192
|
+
""" 清除连续相同值,简化表格内容
|
193
|
+
>> arr_hangclear(arr, depth=2)
|
194
|
+
原表格:
|
195
|
+
A B D
|
196
|
+
A B E
|
197
|
+
A C E
|
198
|
+
A C E
|
199
|
+
新表格:
|
200
|
+
A B D
|
201
|
+
E
|
202
|
+
C E
|
203
|
+
E
|
204
|
+
|
205
|
+
:param arr: 二维数组
|
206
|
+
:param depth: 处理列上限
|
207
|
+
例如depth=1,则只处理第一层
|
208
|
+
depth=None,则处理所有列
|
209
|
+
|
210
|
+
>>> arr_hangclear([[1, 2, 4], [1, 2, 5], [1, 3, 5], [1, 3, 5]])
|
211
|
+
[[1, 2, 4], ['', '', 5], ['', 3, 5], ['', '', 5]]
|
212
|
+
>>> arr_hangclear([[1, 2, 4], [1, 2, 5], [2, 2, 5], [1, 2, 5]])
|
213
|
+
[[1, 2, 4], ['', '', 5], [2, 2, 5], [1, 2, 5]]
|
214
|
+
"""
|
215
|
+
m = depth or len_in_dim2(arr) - 1
|
216
|
+
a = copy.deepcopy(arr)
|
217
|
+
|
218
|
+
# 算法原理:从下到上,从右到左判断与上一行重叠了几列数据
|
219
|
+
for i in range(len(arr) - 1, 0, -1):
|
220
|
+
for j in range(m):
|
221
|
+
if a[i][j] == a[i - 1][j]:
|
222
|
+
a[i][j] = ''
|
223
|
+
else:
|
224
|
+
break
|
225
|
+
return a
|
226
|
+
|
227
|
+
|
228
|
+
def arr2table(arr, rowmerge=False):
|
229
|
+
""" 数组转html表格代码
|
230
|
+
|
231
|
+
:param arr: 需要处理的数组
|
232
|
+
:param rowmerge: 行单元格合并
|
233
|
+
:return: html文本格式的<table>
|
234
|
+
|
235
|
+
这个arr2table是用来画合并单元格的
|
236
|
+
>> browser(arr2table([['A', 1, 'a'], ['', 2, 'b'], ['B', 3, 'c'], ['', '', 'd'], ['', 5, 'e']], True), 'a.html')
|
237
|
+
效果图:http://i1.fuimg.com/582188/c452f40b5a072f8d.png
|
238
|
+
"""
|
239
|
+
n = len(arr)
|
240
|
+
m = len_in_dim2(arr)
|
241
|
+
res = ['<table border="1"><tbody>']
|
242
|
+
for i, line in enumerate(arr):
|
243
|
+
res.append('<tr>')
|
244
|
+
for j, ele in enumerate(line):
|
245
|
+
if rowmerge:
|
246
|
+
if ele != '':
|
247
|
+
cnt = 1
|
248
|
+
while i + cnt < n and arr[i + cnt][j] == '':
|
249
|
+
for k in range(j - 1, -1, -1):
|
250
|
+
if arr[i + cnt][k] != '':
|
251
|
+
break
|
252
|
+
else:
|
253
|
+
cnt += 1
|
254
|
+
continue
|
255
|
+
break
|
256
|
+
if cnt > 1:
|
257
|
+
res.append(f'<td rowspan="{cnt}">{ele}</td>')
|
258
|
+
else:
|
259
|
+
res.append(f'<td>{ele}</td>')
|
260
|
+
elif j == m - 1:
|
261
|
+
res.append(f'<td>{ele}</td>')
|
262
|
+
else:
|
263
|
+
res.append(f'<td>{ele}</td>')
|
264
|
+
res.append('</tr>')
|
265
|
+
res.append('</tbody></table>')
|
266
|
+
return ''.join(res)
|
267
|
+
|
268
|
+
|
269
|
+
def east_asian_len(s, ambiguous_width=None):
|
270
|
+
import pandas.io.formats.format as fmt
|
271
|
+
return fmt.EastAsianTextAdjustment().len(s)
|
272
|
+
|
273
|
+
|
274
|
+
def east_asian_shorten(s, width=50, placeholder='...'):
|
275
|
+
"""考虑中文情况下的域宽截断
|
276
|
+
|
277
|
+
:param s: 要处理的字符串
|
278
|
+
:param width: 宽度上限,仅能达到width-1的宽度
|
279
|
+
:param placeholder: 如果做了截断,末尾补足字符
|
280
|
+
|
281
|
+
# width比placeholder还小
|
282
|
+
>>> east_asian_shorten('a', 2)
|
283
|
+
'a'
|
284
|
+
>>> east_asian_shorten('a啊b'*4, 3)
|
285
|
+
'..'
|
286
|
+
>>> east_asian_shorten('a啊b'*4, 4)
|
287
|
+
'...'
|
288
|
+
|
289
|
+
>>> east_asian_shorten('a啊b'*4, 5, '...')
|
290
|
+
'a...'
|
291
|
+
>>> east_asian_shorten('a啊b'*4, 11)
|
292
|
+
'a啊ba啊...'
|
293
|
+
>>> east_asian_shorten('a啊b'*4, 16, '...')
|
294
|
+
'a啊ba啊ba啊b...'
|
295
|
+
>>> east_asian_shorten('a啊b'*4, 18, '...')
|
296
|
+
'a啊ba啊ba啊ba啊b'
|
297
|
+
"""
|
298
|
+
# 一、如果字符串本身不到width设限,返回原值
|
299
|
+
s = textwrap.shorten(s, width * 3, placeholder='') # 用textwrap的折行功能,尽量不删除文本
|
300
|
+
n = east_asian_len(s)
|
301
|
+
if n < width: return s
|
302
|
+
|
303
|
+
# 二、如果输入的width比placeholder还短
|
304
|
+
width -= 1
|
305
|
+
m = east_asian_len(placeholder)
|
306
|
+
if width <= m:
|
307
|
+
return placeholder[:width]
|
308
|
+
|
309
|
+
# 三、需要添加 placeholder
|
310
|
+
# 1 计算长度
|
311
|
+
width -= m
|
312
|
+
|
313
|
+
# 2 截取s
|
314
|
+
try:
|
315
|
+
s = s.encode('gbk')[:width].decode('gbk', errors='ignore')
|
316
|
+
except UnicodeEncodeError:
|
317
|
+
i, count = 0, m
|
318
|
+
while i < n and count <= width:
|
319
|
+
if ord(s[i]) > 127:
|
320
|
+
count += 2
|
321
|
+
else:
|
322
|
+
count += 1
|
323
|
+
i += 1
|
324
|
+
s = s[:i]
|
325
|
+
|
326
|
+
return s + placeholder
|