pyxllib 0.0.43__py3-none-any.whl → 0.3.197__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 +9 -2
- pyxllib/algo/__init__.py +8 -0
- pyxllib/algo/disjoint.py +54 -0
- pyxllib/algo/geo.py +541 -0
- pyxllib/{util/mathlib.py → algo/intervals.py} +172 -36
- pyxllib/algo/matcher.py +389 -0
- pyxllib/algo/newbie.py +166 -0
- pyxllib/algo/pupil.py +629 -0
- pyxllib/algo/shapelylib.py +67 -0
- pyxllib/algo/specialist.py +241 -0
- pyxllib/algo/stat.py +494 -0
- pyxllib/algo/treelib.py +149 -0
- pyxllib/algo/unitlib.py +66 -0
- pyxllib/autogui/__init__.py +5 -0
- pyxllib/autogui/activewin.py +246 -0
- pyxllib/autogui/all.py +9 -0
- pyxllib/autogui/autogui.py +852 -0
- pyxllib/autogui/uiautolib.py +362 -0
- pyxllib/autogui/virtualkey.py +102 -0
- pyxllib/autogui/wechat.py +827 -0
- pyxllib/autogui/wechat_msg.py +421 -0
- pyxllib/autogui/wxautolib.py +84 -0
- pyxllib/cv/__init__.py +1 -11
- pyxllib/cv/expert.py +267 -0
- pyxllib/cv/{imlib.py → imfile.py} +18 -83
- pyxllib/cv/imhash.py +39 -0
- pyxllib/cv/pupil.py +9 -0
- pyxllib/cv/rgbfmt.py +1525 -0
- pyxllib/cv/slidercaptcha.py +137 -0
- pyxllib/cv/trackbartools.py +163 -49
- pyxllib/cv/xlcvlib.py +1040 -0
- pyxllib/cv/xlpillib.py +423 -0
- pyxllib/data/__init__.py +0 -0
- pyxllib/data/echarts.py +240 -0
- pyxllib/data/jsonlib.py +89 -0
- pyxllib/{util/oss2_.py → data/oss.py} +11 -9
- pyxllib/data/pglib.py +1127 -0
- pyxllib/data/sqlite.py +568 -0
- pyxllib/{util → data}/sqllib.py +13 -31
- pyxllib/ext/JLineViewer.py +505 -0
- pyxllib/ext/__init__.py +6 -0
- pyxllib/{util → ext}/demolib.py +119 -35
- pyxllib/ext/drissionlib.py +277 -0
- pyxllib/ext/kq5034lib.py +12 -0
- pyxllib/{util/main.py → ext/old.py} +122 -284
- pyxllib/ext/qt.py +449 -0
- pyxllib/ext/robustprocfile.py +497 -0
- pyxllib/ext/seleniumlib.py +76 -0
- pyxllib/{util/tklib.py → ext/tk.py} +10 -11
- pyxllib/ext/unixlib.py +827 -0
- pyxllib/ext/utools.py +351 -0
- pyxllib/{util/webhooklib.py → ext/webhook.py} +45 -17
- pyxllib/ext/win32lib.py +40 -0
- pyxllib/ext/wjxlib.py +88 -0
- pyxllib/ext/wpsapi.py +124 -0
- pyxllib/ext/xlwork.py +9 -0
- pyxllib/ext/yuquelib.py +1105 -0
- pyxllib/file/__init__.py +17 -0
- pyxllib/file/docxlib.py +761 -0
- pyxllib/{util → file}/gitlib.py +40 -27
- pyxllib/file/libreoffice.py +165 -0
- pyxllib/file/movielib.py +148 -0
- pyxllib/file/newbie.py +10 -0
- pyxllib/file/onenotelib.py +1469 -0
- pyxllib/file/packlib/__init__.py +330 -0
- pyxllib/{util → file/packlib}/zipfile.py +598 -195
- pyxllib/file/pdflib.py +426 -0
- pyxllib/file/pupil.py +185 -0
- pyxllib/file/specialist/__init__.py +685 -0
- pyxllib/{basic/_5_dirlib.py → file/specialist/dirlib.py} +364 -93
- pyxllib/file/specialist/download.py +193 -0
- pyxllib/file/specialist/filelib.py +2829 -0
- pyxllib/file/xlsxlib.py +3131 -0
- pyxllib/file/xlsyncfile.py +341 -0
- pyxllib/prog/__init__.py +5 -0
- pyxllib/prog/cachetools.py +64 -0
- pyxllib/prog/deprecatedlib.py +233 -0
- pyxllib/prog/filelock.py +42 -0
- pyxllib/prog/ipyexec.py +253 -0
- pyxllib/prog/multiprogs.py +940 -0
- pyxllib/prog/newbie.py +451 -0
- pyxllib/prog/pupil.py +1197 -0
- pyxllib/{sitepackages.py → prog/sitepackages.py} +5 -3
- pyxllib/prog/specialist/__init__.py +391 -0
- pyxllib/prog/specialist/bc.py +203 -0
- pyxllib/prog/specialist/browser.py +497 -0
- pyxllib/prog/specialist/common.py +347 -0
- pyxllib/prog/specialist/datetime.py +199 -0
- pyxllib/prog/specialist/tictoc.py +240 -0
- pyxllib/prog/specialist/xllog.py +180 -0
- pyxllib/prog/xlosenv.py +108 -0
- pyxllib/stdlib/__init__.py +17 -0
- pyxllib/{util → stdlib}/tablepyxl/__init__.py +1 -3
- pyxllib/{util → stdlib}/tablepyxl/style.py +1 -1
- pyxllib/{util → stdlib}/tablepyxl/tablepyxl.py +2 -4
- pyxllib/text/__init__.py +8 -0
- pyxllib/text/ahocorasick.py +39 -0
- pyxllib/text/airscript.js +744 -0
- pyxllib/text/charclasslib.py +121 -0
- pyxllib/text/jiebalib.py +267 -0
- pyxllib/text/jinjalib.py +32 -0
- pyxllib/text/jsa_ai_prompt.md +271 -0
- pyxllib/text/jscode.py +922 -0
- pyxllib/text/latex/__init__.py +158 -0
- pyxllib/text/levenshtein.py +303 -0
- pyxllib/text/nestenv.py +1215 -0
- pyxllib/text/newbie.py +300 -0
- pyxllib/text/pupil/__init__.py +8 -0
- pyxllib/text/pupil/common.py +1121 -0
- pyxllib/text/pupil/xlalign.py +326 -0
- pyxllib/text/pycode.py +47 -0
- pyxllib/text/specialist/__init__.py +8 -0
- pyxllib/text/specialist/common.py +112 -0
- pyxllib/text/specialist/ptag.py +186 -0
- pyxllib/text/spellchecker.py +172 -0
- pyxllib/text/templates/echart_base.html +11 -0
- pyxllib/text/templates/highlight_code.html +17 -0
- pyxllib/text/templates/latex_editor.html +103 -0
- pyxllib/text/vbacode.py +17 -0
- pyxllib/text/xmllib.py +747 -0
- pyxllib/xl.py +39 -0
- pyxllib/xlcv.py +17 -0
- pyxllib-0.3.197.dist-info/METADATA +48 -0
- pyxllib-0.3.197.dist-info/RECORD +126 -0
- {pyxllib-0.0.43.dist-info → pyxllib-0.3.197.dist-info}/WHEEL +4 -5
- pyxllib/basic/_1_strlib.py +0 -945
- pyxllib/basic/_2_timelib.py +0 -488
- pyxllib/basic/_3_pathlib.py +0 -916
- pyxllib/basic/_4_loglib.py +0 -419
- pyxllib/basic/__init__.py +0 -54
- pyxllib/basic/arrow_.py +0 -250
- pyxllib/basic/chardet_.py +0 -66
- pyxllib/basic/dirlib.py +0 -529
- pyxllib/basic/dprint.py +0 -202
- pyxllib/basic/extension.py +0 -12
- pyxllib/basic/judge.py +0 -31
- pyxllib/basic/log.py +0 -204
- pyxllib/basic/pathlib_.py +0 -705
- pyxllib/basic/pytictoc.py +0 -102
- pyxllib/basic/qiniu_.py +0 -61
- pyxllib/basic/strlib.py +0 -761
- pyxllib/basic/timer.py +0 -132
- pyxllib/cv/cv.py +0 -834
- pyxllib/cv/cvlib/_1_geo.py +0 -543
- pyxllib/cv/cvlib/_2_cvprcs.py +0 -309
- pyxllib/cv/cvlib/_2_imgproc.py +0 -594
- pyxllib/cv/cvlib/_3_pilprcs.py +0 -80
- pyxllib/cv/cvlib/_4_cvimg.py +0 -211
- pyxllib/cv/cvlib/__init__.py +0 -10
- pyxllib/cv/debugtools.py +0 -82
- pyxllib/cv/fitz_.py +0 -300
- pyxllib/cv/installer.py +0 -42
- pyxllib/debug/_0_installer.py +0 -38
- pyxllib/debug/_1_typelib.py +0 -277
- pyxllib/debug/_2_chrome.py +0 -198
- pyxllib/debug/_3_showdir.py +0 -161
- pyxllib/debug/_4_bcompare.py +0 -140
- pyxllib/debug/__init__.py +0 -49
- pyxllib/debug/bcompare.py +0 -132
- pyxllib/debug/chrome.py +0 -198
- pyxllib/debug/installer.py +0 -38
- pyxllib/debug/showdir.py +0 -158
- pyxllib/debug/typelib.py +0 -278
- pyxllib/image/__init__.py +0 -12
- pyxllib/torch/__init__.py +0 -20
- pyxllib/torch/modellib.py +0 -37
- pyxllib/torch/trainlib.py +0 -344
- pyxllib/util/__init__.py +0 -20
- pyxllib/util/aip_.py +0 -141
- pyxllib/util/casiadb.py +0 -59
- pyxllib/util/excellib.py +0 -495
- pyxllib/util/filelib.py +0 -612
- pyxllib/util/jsondata.py +0 -27
- pyxllib/util/jsondata2.py +0 -92
- pyxllib/util/labelmelib.py +0 -139
- pyxllib/util/onepy/__init__.py +0 -29
- pyxllib/util/onepy/onepy.py +0 -574
- pyxllib/util/onepy/onmanager.py +0 -170
- pyxllib/util/pyautogui_.py +0 -219
- pyxllib/util/textlib.py +0 -1305
- pyxllib/util/unorder.py +0 -22
- pyxllib/util/xmllib.py +0 -639
- pyxllib-0.0.43.dist-info/METADATA +0 -39
- pyxllib-0.0.43.dist-info/RECORD +0 -80
- pyxllib-0.0.43.dist-info/top_level.txt +0 -1
- {pyxllib-0.0.43.dist-info → pyxllib-0.3.197.dist-info/licenses}/LICENSE +0 -0
@@ -0,0 +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
|
pyxllib/text/pycode.py
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
#!/usr/bin/env python3
|
2
|
+
# -*- coding: utf-8 -*-
|
3
|
+
# @Author : 陈坤泽
|
4
|
+
# @Email : 877362867@qq.com
|
5
|
+
# @Date : 2021/08/20 11:46
|
6
|
+
|
7
|
+
|
8
|
+
import re
|
9
|
+
|
10
|
+
|
11
|
+
def py_remove_interaction_chars(s):
|
12
|
+
""" 去掉复制的一段代码中,前导的“>>>”标记 """
|
13
|
+
# 这个算法可能还不够严谨,实际应用中再逐步写鲁棒
|
14
|
+
# ">>> "、"... "
|
15
|
+
lines = [line[4:] for line in s.splitlines()]
|
16
|
+
return '\n'.join(lines)
|
17
|
+
|
18
|
+
|
19
|
+
def pycode_sort_import(s):
|
20
|
+
from pyxllib.text.nestenv import PyNestEnv
|
21
|
+
|
22
|
+
def cmp(line):
|
23
|
+
""" 将任意一句import映射为一个可比较的list对象
|
24
|
+
|
25
|
+
:return: 2个数值
|
26
|
+
1、模块优先级
|
27
|
+
2、import在前,from在后
|
28
|
+
"""
|
29
|
+
name = re.search(r'(?:import|from)\s+(\S+)', line).group(1)
|
30
|
+
for i, x in enumerate('stdlib prog algo text file cv data extend'.split()):
|
31
|
+
name = name.replace('pyxllib.' + x, f'{i:02}')
|
32
|
+
for i, x in enumerate('pyxllib pyxlpr xlproject'.split()):
|
33
|
+
name = name.replace(x, f'~{i:02}')
|
34
|
+
for i, x in enumerate('newbie pupil specialist expert'.split()):
|
35
|
+
name = name.replace('.' + x, f'{i:02}')
|
36
|
+
|
37
|
+
# 忽略大小写
|
38
|
+
return [name.lower(), line.startswith('import')]
|
39
|
+
|
40
|
+
def sort_part(m):
|
41
|
+
parts = PyNestEnv(m.group()).imports().strings()
|
42
|
+
parts = [p.rstrip() + '\n' for p in parts]
|
43
|
+
parts.sort(key=cmp)
|
44
|
+
return ''.join(parts)
|
45
|
+
|
46
|
+
res = PyNestEnv(s).imports().sub(sort_part, adjacent=True) # 需要邻接,分块处理
|
47
|
+
return res
|
@@ -0,0 +1,112 @@
|
|
1
|
+
#!/usr/bin/env python3
|
2
|
+
# -*- coding: utf-8 -*-
|
3
|
+
# @Author : 陈坤泽
|
4
|
+
# @Email : 877362867@qq.com
|
5
|
+
# @Date : 2021/06/06 17:34
|
6
|
+
|
7
|
+
import re
|
8
|
+
import sys
|
9
|
+
import textwrap
|
10
|
+
|
11
|
+
from bs4 import BeautifulSoup
|
12
|
+
import pandas as pd
|
13
|
+
import requests
|
14
|
+
|
15
|
+
from pyxllib.prog.newbie import len_in_dim2
|
16
|
+
from pyxllib.prog.pupil import check_install_package
|
17
|
+
from pyxllib.prog.specialist import dataframe_str
|
18
|
+
from pyxllib.text.pupil import ContentLine
|
19
|
+
from pyxllib.file.specialist import get_encoding, File
|
20
|
+
|
21
|
+
|
22
|
+
def regularcheck(pattern, string, flags=0):
|
23
|
+
arr = []
|
24
|
+
cl = ContentLine(string)
|
25
|
+
for i, m in enumerate(re.finditer(pattern, string, flags)):
|
26
|
+
ss = map(lambda x: textwrap.shorten(x, 200), m.groups())
|
27
|
+
arr.append([i + 1, cl.in_line(m.start(0)), *ss])
|
28
|
+
tablehead = ['行号'] + list(map(lambda x: f'第{x}组', range(len_in_dim2(arr) - 2)))
|
29
|
+
df = pd.DataFrame.from_records(arr, columns=tablehead)
|
30
|
+
res = f'正则模式:{pattern},匹配结果:\n' + dataframe_str(df)
|
31
|
+
return res
|
32
|
+
|
33
|
+
|
34
|
+
def readtext(filename, encoding=None):
|
35
|
+
"""读取普通的文本文件
|
36
|
+
会根据tex、py文件情况指定默认编码
|
37
|
+
"""
|
38
|
+
try:
|
39
|
+
with open(filename, 'rb') as f: # 以二进制读取文件,注意二进制没有\r\n参数
|
40
|
+
bstr = f.read()
|
41
|
+
except FileNotFoundError:
|
42
|
+
return None
|
43
|
+
|
44
|
+
if not encoding:
|
45
|
+
encoding = get_encoding(bstr)
|
46
|
+
s = bstr.decode(encoding=encoding, errors='ignore')
|
47
|
+
if '\r' in s: # 注意这个问题跟gb2312和gbk是独立的,用gbk编码也要做这个处理
|
48
|
+
s = s.replace('\r\n', '\n') # 如果用\r\n作为换行符会有一些意外不好处理
|
49
|
+
return s
|
50
|
+
|
51
|
+
|
52
|
+
def ensure_content(ob=None, encoding=None):
|
53
|
+
"""
|
54
|
+
:param ob:
|
55
|
+
未输入:从控制台获取文本
|
56
|
+
存在的文件名:读取文件的内容返回
|
57
|
+
tex、py、
|
58
|
+
docx、doc
|
59
|
+
pdf
|
60
|
+
有read可调用成员方法:返回f.read()
|
61
|
+
其他字符串:返回原值
|
62
|
+
:param encoding: 强制指定编码
|
63
|
+
"""
|
64
|
+
# TODO: 如果输入的是一个文件指针,也能调用f.read()返回所有内容
|
65
|
+
# TODO: 增加鲁棒性判断,如果输入的不是字符串类型也要有出错判断
|
66
|
+
if ob is None:
|
67
|
+
return sys.stdin.read() # 注意输入是按 Ctrl + D 结束
|
68
|
+
elif File(ob): # 如果存在这样的文件,那就读取文件内容(bug点:如果输入是目录名会PermissionError)
|
69
|
+
if ob.endswith('.docx'): # 这里还要再扩展pdf、doc文件的读取
|
70
|
+
# 安装详见: https://blog.csdn.net/code4101/article/details/79328636
|
71
|
+
check_install_package('textract')
|
72
|
+
text = textract.process(ob)
|
73
|
+
return text.decode('utf8', errors='ignore')
|
74
|
+
elif ob.endswith('.doc'):
|
75
|
+
raise NotImplementedError
|
76
|
+
elif ob.endswith('.pdf'):
|
77
|
+
raise NotImplementedError
|
78
|
+
else: # 按照普通的文本文件读取内容
|
79
|
+
return readtext(ob, encoding)
|
80
|
+
else: # 判断不了的情况,也认为是字符串
|
81
|
+
return ob
|
82
|
+
|
83
|
+
|
84
|
+
def file_lastlines(fn, n):
|
85
|
+
"""获得一个文件最后的几行内容
|
86
|
+
参考资料: https://stackoverflow.com/questions/136168/get-last-n-lines-of-a-file-with-python-similar-to-tail
|
87
|
+
|
88
|
+
>> s = FileLastLine('book.log', 1)
|
89
|
+
'Output written on book.dvi (2 pages, 7812 bytes).'
|
90
|
+
"""
|
91
|
+
f = ensure_content(fn)
|
92
|
+
assert n >= 0
|
93
|
+
pos, lines = n + 1, []
|
94
|
+
while len(lines) <= n:
|
95
|
+
try:
|
96
|
+
f.seek(-pos, 2)
|
97
|
+
except IOError:
|
98
|
+
f.seek(0)
|
99
|
+
break
|
100
|
+
finally:
|
101
|
+
lines = list(f)
|
102
|
+
pos *= 2
|
103
|
+
f.close()
|
104
|
+
return ''.join(lines[-n:])
|
105
|
+
|
106
|
+
|
107
|
+
def readurl(url):
|
108
|
+
"""从url读取文本"""
|
109
|
+
r = requests.get(url)
|
110
|
+
soup = BeautifulSoup(r.text, 'lxml')
|
111
|
+
s = soup.get_text()
|
112
|
+
return s
|
@@ -0,0 +1,186 @@
|
|
1
|
+
#!/usr/bin/env python3
|
2
|
+
# -*- coding: utf-8 -*-
|
3
|
+
# @Author : 陈坤泽
|
4
|
+
# @Email : 877362867@qq.com
|
5
|
+
# @Date : 2021/06/06 17:22
|
6
|
+
|
7
|
+
""" 百分注格式处理 """
|
8
|
+
|
9
|
+
import re
|
10
|
+
|
11
|
+
import bs4
|
12
|
+
from bs4 import BeautifulSoup
|
13
|
+
|
14
|
+
from pyxllib.prog.pupil import dprint
|
15
|
+
from pyxllib.text.pupil import grp_bracket
|
16
|
+
|
17
|
+
|
18
|
+
def gettag_name(tagstr):
|
19
|
+
"""
|
20
|
+
>>> gettag_name('%<topic type=danxuan description=单选题>')
|
21
|
+
'topic'
|
22
|
+
>>> gettag_name('</topic>')
|
23
|
+
'topic'
|
24
|
+
"""
|
25
|
+
m = re.search(r'</?([a-zA-Z_]+)', tagstr)
|
26
|
+
if m:
|
27
|
+
return m.group(1)
|
28
|
+
else:
|
29
|
+
return None
|
30
|
+
|
31
|
+
|
32
|
+
def settag_name(tagstr, *, new_name=None, switch=None):
|
33
|
+
"""设置标签名称,或者将标签类型设为close类型
|
34
|
+
|
35
|
+
>>> settag_name('%<topic type=danxuan description=单选题>', new_name='mdzz')
|
36
|
+
'%<mdzz type=danxuan description=单选题>'
|
37
|
+
>>> settag_name('<topic type=danxuan description=单选题>', switch=False)
|
38
|
+
'</topic>'
|
39
|
+
"""
|
40
|
+
if new_name: # 是否设置新名称
|
41
|
+
tagstr = re.sub(r'(</?)([a-zA-Z_]+)', lambda m: m.group(1) + new_name, tagstr)
|
42
|
+
|
43
|
+
if switch is not None: # 是否设置标签开关
|
44
|
+
if switch: # 将标签改为开
|
45
|
+
tagstr = tagstr.replace('</', '<')
|
46
|
+
else: # 将标签改为关
|
47
|
+
name = gettag_name(tagstr)
|
48
|
+
res = f'</{name}>' # 会删除所有attr属性
|
49
|
+
tagstr = '%' + res if '%<' in tagstr else res
|
50
|
+
|
51
|
+
return tagstr
|
52
|
+
|
53
|
+
|
54
|
+
def gettag_attr(tagstr, attrname):
|
55
|
+
r"""tagstr是一个标签字符串,attrname是要索引的名字
|
56
|
+
返回属性值,如果不存在该属性则返回None
|
57
|
+
|
58
|
+
>>> gettag_attr('%<topic type=danxuan description=单选题> 123\n<a b=c></a>', 'type')
|
59
|
+
'danxuan'
|
60
|
+
>>> gettag_attr('%<topic type="dan xu an" description=单选题>', 'type')
|
61
|
+
'dan xu an'
|
62
|
+
>>> gettag_attr("%<topic type='dan xu an' description=单选题>", 'type')
|
63
|
+
'dan xu an'
|
64
|
+
>>> gettag_attr('%<topic type=dan xu an description=单选题>', 'description')
|
65
|
+
'单选题'
|
66
|
+
>>> gettag_attr('%<topic type=dan xu an description=单选题>', 'type')
|
67
|
+
'dan'
|
68
|
+
>>> gettag_attr('%<topic type=danxuan description=单选题 >', 'description')
|
69
|
+
'单选题'
|
70
|
+
>>> gettag_attr('%<topic type=danxuan description=单选题 >', 'description123') is None
|
71
|
+
True
|
72
|
+
"""
|
73
|
+
soup = BeautifulSoup(tagstr, 'lxml')
|
74
|
+
try:
|
75
|
+
for tag in soup.p.contents:
|
76
|
+
if isinstance(tag, bs4.Tag):
|
77
|
+
return tag.get(attrname, None)
|
78
|
+
except AttributeError:
|
79
|
+
dprint(tagstr)
|
80
|
+
return None
|
81
|
+
|
82
|
+
|
83
|
+
def settag_attr(tagstr, attrname, target_value):
|
84
|
+
r"""tagstr是一个标签字符串,attrname是要索引的名字
|
85
|
+
重设该属性的值,设置成功则返回新的tagstr;否则返回原始值
|
86
|
+
|
87
|
+
close类型不能用这个命令,用了的话不进行任何处理,直接返回
|
88
|
+
|
89
|
+
>>> settag_attr('%<topic type=danxuan> 123\n<a></a>', 'type', 'tiankong')
|
90
|
+
'%<topic type="tiankong"> 123\n<a></a>'
|
91
|
+
>>> settag_attr('%<topic>', 'type', 'tiankong')
|
92
|
+
'%<topic type="tiankong">'
|
93
|
+
>>> settag_attr('</topic>', 'type', 'tiankong')
|
94
|
+
'</topic>'
|
95
|
+
>>> settag_attr('<seq value="1">', 'value', '练习1.2')
|
96
|
+
'<seq value="练习1.2">'
|
97
|
+
>>> settag_attr('<seq type=123 value=1>', 'type', '') # 删除attr操作
|
98
|
+
'<seq value=1>'
|
99
|
+
>>> settag_attr('<seq type=123 value=1>', 'value', '') # 删除attr操作
|
100
|
+
'<seq type=123>'
|
101
|
+
>>> settag_attr('<seq type=123 value=1>', 'haha', '') # 删除attr操作
|
102
|
+
'<seq type=123 value=1>'
|
103
|
+
"""
|
104
|
+
# 如果是close类型是不处理的
|
105
|
+
if tagstr.startswith('</'): return tagstr
|
106
|
+
|
107
|
+
# 预处理targetValue的值,删除空白
|
108
|
+
target_value = re.sub(r'\s', '', target_value)
|
109
|
+
r = re.compile(r'(<|\s)(' + attrname + r'=)(.+?)(\s+\w+=|\s*>)')
|
110
|
+
gs = r.search(tagstr)
|
111
|
+
if target_value:
|
112
|
+
if not gs: # 如果未找到则添加attr与value
|
113
|
+
n = tagstr.find('>')
|
114
|
+
return tagstr[:n] + ' ' + attrname + '="' + target_value + '"' + tagstr[n:]
|
115
|
+
else: # 如果找到则更改value
|
116
|
+
# TODO: 目前的替换值是直接放到正则式里了,这样会有很大的风险,后续看看能不能优化这个处理算法
|
117
|
+
return r.sub(r'\1\g<2>"' + target_value + r'"\4', tagstr)
|
118
|
+
else:
|
119
|
+
if gs:
|
120
|
+
return r.sub(r'\4', tagstr)
|
121
|
+
else:
|
122
|
+
return tagstr
|
123
|
+
|
124
|
+
|
125
|
+
def brieftexstr(s):
|
126
|
+
"""对比两段tex文本
|
127
|
+
"""
|
128
|
+
# 1 删除百分注
|
129
|
+
s = re.sub(r'%' + grp_bracket(2, '<', '>'), r'', s)
|
130
|
+
# 2 删除所有空白字符
|
131
|
+
# debuglib.dprint(debuglib.typename(s))
|
132
|
+
s = re.sub(r'\s+', '', s)
|
133
|
+
# 3 转小写字符
|
134
|
+
s = s.casefold()
|
135
|
+
return s
|
136
|
+
|
137
|
+
|
138
|
+
# 默认不建议开,编校如果用的多,可以在那边定义
|
139
|
+
# 定义常用的几种格式,并且只匹配抓取花括号里面的值,不要花括号本身
|
140
|
+
# SQUARE3 = r'\\[(' + grp_bracket(3, '[')[3:-3] + r')\\]'
|
141
|
+
# BRACE1 = '{(' + grp_bracket(1)[1:-1] + ')}'
|
142
|
+
# BRACE2 = '{(' + grp_bracket(2)[1:-1] + ')}'
|
143
|
+
# BRACE3 = '{(' + grp_bracket(3)[1:-1] + ')}'
|
144
|
+
# BRACE4 = '{(' + grp_bracket(4)[1:-1] + ')}'
|
145
|
+
# BRACE5 = '{(' + grp_bracket(5)[1:-1] + ')}'
|
146
|
+
r""" 使用示例
|
147
|
+
>> m = re.search(r'\\multicolumn' + BRACE3*3, r'\multicolumn{2}{|c|}{$2^{12}$个数}')
|
148
|
+
>> m.groups()
|
149
|
+
('2', '|c|', '$2^{12}$个数')
|
150
|
+
"""
|
151
|
+
|
152
|
+
|
153
|
+
def grp_topic(*, type_value=None):
|
154
|
+
"""定位topic
|
155
|
+
|
156
|
+
:param type_value: 设置题目类型(TODO: 功能尚未开发)
|
157
|
+
"""
|
158
|
+
s = r'%<topic.*?%</topic>' # 注意外部使用的re要开flags=re.DOTALL
|
159
|
+
return s
|
160
|
+
|
161
|
+
|
162
|
+
def grp_figure(cnt_groups=0, parpic=False):
|
163
|
+
r""" 生成跟图片匹配相关的表达式
|
164
|
+
|
165
|
+
D:\2017LaTeX\D招培试卷\高中地理,用过 \captionfig{3-3.eps}{图~3}
|
166
|
+
奕本从2018秋季教材开始使用多种图片格式
|
167
|
+
|
168
|
+
191224周二18:20 更新:匹配到的图片名不带花括号
|
169
|
+
"""
|
170
|
+
ibrace3 = grp_bracket(3, inner=True)
|
171
|
+
|
172
|
+
if cnt_groups == 0: # 不分组
|
173
|
+
s = r'\\(?:includegraphics|figt|figc|figr|fig).*?' + grp_bracket(3) # 注意第1组fig要放最后面
|
174
|
+
elif cnt_groups == 1: # 只分1组,那么只对图片括号内的内容分组
|
175
|
+
s = r'\\(?:includegraphics|figt|figc|figr|fig).*?' + ibrace3
|
176
|
+
elif cnt_groups == 2: # 只分2组,那么只对插图命令和图片分组
|
177
|
+
s = r'\\(includegraphics|figt|figc|figr|fig).*?' + ibrace3
|
178
|
+
elif cnt_groups == 3:
|
179
|
+
s = r'\\(includegraphics|figt|figc|figr|fig)(.*?)' + ibrace3
|
180
|
+
else:
|
181
|
+
s = None
|
182
|
+
|
183
|
+
if s and parpic:
|
184
|
+
s = r'{?\\parpic(?:\[.\])?{' + s + r'}*'
|
185
|
+
|
186
|
+
return s
|