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.
Files changed (186) hide show
  1. pyxllib/__init__.py +9 -2
  2. pyxllib/algo/__init__.py +8 -0
  3. pyxllib/algo/disjoint.py +54 -0
  4. pyxllib/algo/geo.py +541 -0
  5. pyxllib/{util/mathlib.py → algo/intervals.py} +172 -36
  6. pyxllib/algo/matcher.py +389 -0
  7. pyxllib/algo/newbie.py +166 -0
  8. pyxllib/algo/pupil.py +629 -0
  9. pyxllib/algo/shapelylib.py +67 -0
  10. pyxllib/algo/specialist.py +241 -0
  11. pyxllib/algo/stat.py +494 -0
  12. pyxllib/algo/treelib.py +149 -0
  13. pyxllib/algo/unitlib.py +66 -0
  14. pyxllib/autogui/__init__.py +5 -0
  15. pyxllib/autogui/activewin.py +246 -0
  16. pyxllib/autogui/all.py +9 -0
  17. pyxllib/autogui/autogui.py +852 -0
  18. pyxllib/autogui/uiautolib.py +362 -0
  19. pyxllib/autogui/virtualkey.py +102 -0
  20. pyxllib/autogui/wechat.py +827 -0
  21. pyxllib/autogui/wechat_msg.py +421 -0
  22. pyxllib/autogui/wxautolib.py +84 -0
  23. pyxllib/cv/__init__.py +1 -11
  24. pyxllib/cv/expert.py +267 -0
  25. pyxllib/cv/{imlib.py → imfile.py} +18 -83
  26. pyxllib/cv/imhash.py +39 -0
  27. pyxllib/cv/pupil.py +9 -0
  28. pyxllib/cv/rgbfmt.py +1525 -0
  29. pyxllib/cv/slidercaptcha.py +137 -0
  30. pyxllib/cv/trackbartools.py +163 -49
  31. pyxllib/cv/xlcvlib.py +1040 -0
  32. pyxllib/cv/xlpillib.py +423 -0
  33. pyxllib/data/__init__.py +0 -0
  34. pyxllib/data/echarts.py +240 -0
  35. pyxllib/data/jsonlib.py +89 -0
  36. pyxllib/{util/oss2_.py → data/oss.py} +11 -9
  37. pyxllib/data/pglib.py +1127 -0
  38. pyxllib/data/sqlite.py +568 -0
  39. pyxllib/{util → data}/sqllib.py +13 -31
  40. pyxllib/ext/JLineViewer.py +505 -0
  41. pyxllib/ext/__init__.py +6 -0
  42. pyxllib/{util → ext}/demolib.py +119 -35
  43. pyxllib/ext/drissionlib.py +277 -0
  44. pyxllib/ext/kq5034lib.py +12 -0
  45. pyxllib/{util/main.py → ext/old.py} +122 -284
  46. pyxllib/ext/qt.py +449 -0
  47. pyxllib/ext/robustprocfile.py +497 -0
  48. pyxllib/ext/seleniumlib.py +76 -0
  49. pyxllib/{util/tklib.py → ext/tk.py} +10 -11
  50. pyxllib/ext/unixlib.py +827 -0
  51. pyxllib/ext/utools.py +351 -0
  52. pyxllib/{util/webhooklib.py → ext/webhook.py} +45 -17
  53. pyxllib/ext/win32lib.py +40 -0
  54. pyxllib/ext/wjxlib.py +88 -0
  55. pyxllib/ext/wpsapi.py +124 -0
  56. pyxllib/ext/xlwork.py +9 -0
  57. pyxllib/ext/yuquelib.py +1105 -0
  58. pyxllib/file/__init__.py +17 -0
  59. pyxllib/file/docxlib.py +761 -0
  60. pyxllib/{util → file}/gitlib.py +40 -27
  61. pyxllib/file/libreoffice.py +165 -0
  62. pyxllib/file/movielib.py +148 -0
  63. pyxllib/file/newbie.py +10 -0
  64. pyxllib/file/onenotelib.py +1469 -0
  65. pyxllib/file/packlib/__init__.py +330 -0
  66. pyxllib/{util → file/packlib}/zipfile.py +598 -195
  67. pyxllib/file/pdflib.py +426 -0
  68. pyxllib/file/pupil.py +185 -0
  69. pyxllib/file/specialist/__init__.py +685 -0
  70. pyxllib/{basic/_5_dirlib.py → file/specialist/dirlib.py} +364 -93
  71. pyxllib/file/specialist/download.py +193 -0
  72. pyxllib/file/specialist/filelib.py +2829 -0
  73. pyxllib/file/xlsxlib.py +3131 -0
  74. pyxllib/file/xlsyncfile.py +341 -0
  75. pyxllib/prog/__init__.py +5 -0
  76. pyxllib/prog/cachetools.py +64 -0
  77. pyxllib/prog/deprecatedlib.py +233 -0
  78. pyxllib/prog/filelock.py +42 -0
  79. pyxllib/prog/ipyexec.py +253 -0
  80. pyxllib/prog/multiprogs.py +940 -0
  81. pyxllib/prog/newbie.py +451 -0
  82. pyxllib/prog/pupil.py +1197 -0
  83. pyxllib/{sitepackages.py → prog/sitepackages.py} +5 -3
  84. pyxllib/prog/specialist/__init__.py +391 -0
  85. pyxllib/prog/specialist/bc.py +203 -0
  86. pyxllib/prog/specialist/browser.py +497 -0
  87. pyxllib/prog/specialist/common.py +347 -0
  88. pyxllib/prog/specialist/datetime.py +199 -0
  89. pyxllib/prog/specialist/tictoc.py +240 -0
  90. pyxllib/prog/specialist/xllog.py +180 -0
  91. pyxllib/prog/xlosenv.py +108 -0
  92. pyxllib/stdlib/__init__.py +17 -0
  93. pyxllib/{util → stdlib}/tablepyxl/__init__.py +1 -3
  94. pyxllib/{util → stdlib}/tablepyxl/style.py +1 -1
  95. pyxllib/{util → stdlib}/tablepyxl/tablepyxl.py +2 -4
  96. pyxllib/text/__init__.py +8 -0
  97. pyxllib/text/ahocorasick.py +39 -0
  98. pyxllib/text/airscript.js +744 -0
  99. pyxllib/text/charclasslib.py +121 -0
  100. pyxllib/text/jiebalib.py +267 -0
  101. pyxllib/text/jinjalib.py +32 -0
  102. pyxllib/text/jsa_ai_prompt.md +271 -0
  103. pyxllib/text/jscode.py +922 -0
  104. pyxllib/text/latex/__init__.py +158 -0
  105. pyxllib/text/levenshtein.py +303 -0
  106. pyxllib/text/nestenv.py +1215 -0
  107. pyxllib/text/newbie.py +300 -0
  108. pyxllib/text/pupil/__init__.py +8 -0
  109. pyxllib/text/pupil/common.py +1121 -0
  110. pyxllib/text/pupil/xlalign.py +326 -0
  111. pyxllib/text/pycode.py +47 -0
  112. pyxllib/text/specialist/__init__.py +8 -0
  113. pyxllib/text/specialist/common.py +112 -0
  114. pyxllib/text/specialist/ptag.py +186 -0
  115. pyxllib/text/spellchecker.py +172 -0
  116. pyxllib/text/templates/echart_base.html +11 -0
  117. pyxllib/text/templates/highlight_code.html +17 -0
  118. pyxllib/text/templates/latex_editor.html +103 -0
  119. pyxllib/text/vbacode.py +17 -0
  120. pyxllib/text/xmllib.py +747 -0
  121. pyxllib/xl.py +39 -0
  122. pyxllib/xlcv.py +17 -0
  123. pyxllib-0.3.197.dist-info/METADATA +48 -0
  124. pyxllib-0.3.197.dist-info/RECORD +126 -0
  125. {pyxllib-0.0.43.dist-info → pyxllib-0.3.197.dist-info}/WHEEL +4 -5
  126. pyxllib/basic/_1_strlib.py +0 -945
  127. pyxllib/basic/_2_timelib.py +0 -488
  128. pyxllib/basic/_3_pathlib.py +0 -916
  129. pyxllib/basic/_4_loglib.py +0 -419
  130. pyxllib/basic/__init__.py +0 -54
  131. pyxllib/basic/arrow_.py +0 -250
  132. pyxllib/basic/chardet_.py +0 -66
  133. pyxllib/basic/dirlib.py +0 -529
  134. pyxllib/basic/dprint.py +0 -202
  135. pyxllib/basic/extension.py +0 -12
  136. pyxllib/basic/judge.py +0 -31
  137. pyxllib/basic/log.py +0 -204
  138. pyxllib/basic/pathlib_.py +0 -705
  139. pyxllib/basic/pytictoc.py +0 -102
  140. pyxllib/basic/qiniu_.py +0 -61
  141. pyxllib/basic/strlib.py +0 -761
  142. pyxllib/basic/timer.py +0 -132
  143. pyxllib/cv/cv.py +0 -834
  144. pyxllib/cv/cvlib/_1_geo.py +0 -543
  145. pyxllib/cv/cvlib/_2_cvprcs.py +0 -309
  146. pyxllib/cv/cvlib/_2_imgproc.py +0 -594
  147. pyxllib/cv/cvlib/_3_pilprcs.py +0 -80
  148. pyxllib/cv/cvlib/_4_cvimg.py +0 -211
  149. pyxllib/cv/cvlib/__init__.py +0 -10
  150. pyxllib/cv/debugtools.py +0 -82
  151. pyxllib/cv/fitz_.py +0 -300
  152. pyxllib/cv/installer.py +0 -42
  153. pyxllib/debug/_0_installer.py +0 -38
  154. pyxllib/debug/_1_typelib.py +0 -277
  155. pyxllib/debug/_2_chrome.py +0 -198
  156. pyxllib/debug/_3_showdir.py +0 -161
  157. pyxllib/debug/_4_bcompare.py +0 -140
  158. pyxllib/debug/__init__.py +0 -49
  159. pyxllib/debug/bcompare.py +0 -132
  160. pyxllib/debug/chrome.py +0 -198
  161. pyxllib/debug/installer.py +0 -38
  162. pyxllib/debug/showdir.py +0 -158
  163. pyxllib/debug/typelib.py +0 -278
  164. pyxllib/image/__init__.py +0 -12
  165. pyxllib/torch/__init__.py +0 -20
  166. pyxllib/torch/modellib.py +0 -37
  167. pyxllib/torch/trainlib.py +0 -344
  168. pyxllib/util/__init__.py +0 -20
  169. pyxllib/util/aip_.py +0 -141
  170. pyxllib/util/casiadb.py +0 -59
  171. pyxllib/util/excellib.py +0 -495
  172. pyxllib/util/filelib.py +0 -612
  173. pyxllib/util/jsondata.py +0 -27
  174. pyxllib/util/jsondata2.py +0 -92
  175. pyxllib/util/labelmelib.py +0 -139
  176. pyxllib/util/onepy/__init__.py +0 -29
  177. pyxllib/util/onepy/onepy.py +0 -574
  178. pyxllib/util/onepy/onmanager.py +0 -170
  179. pyxllib/util/pyautogui_.py +0 -219
  180. pyxllib/util/textlib.py +0 -1305
  181. pyxllib/util/unorder.py +0 -22
  182. pyxllib/util/xmllib.py +0 -639
  183. pyxllib-0.0.43.dist-info/METADATA +0 -39
  184. pyxllib-0.0.43.dist-info/RECORD +0 -80
  185. pyxllib-0.0.43.dist-info/top_level.txt +0 -1
  186. {pyxllib-0.0.43.dist-info → pyxllib-0.3.197.dist-info/licenses}/LICENSE +0 -0
pyxllib/debug/showdir.py DELETED
@@ -1,158 +0,0 @@
1
- #!/usr/bin/env python3
2
- # -*- coding: utf-8 -*-
3
- # @Author : 陈坤泽
4
- # @Email : 877362867@qq.com
5
- # @Data : 2020/06/01
6
-
7
-
8
- import enum
9
- import html
10
- import subprocess
11
-
12
- import pandas as pd
13
- from bs4 import BeautifulSoup
14
-
15
- from pyxllib.basic import *
16
- from .typelib import dataframe_str
17
- from .chrome import getasizeof
18
-
19
-
20
- def getmembers(object, predicate=None):
21
- """自己重写改动的 inspect.getmembers"""
22
- from inspect import isclass, getmro
23
- import types
24
-
25
- if isclass(object):
26
- mro = (object,) + getmro(object)
27
- else:
28
- mro = ()
29
- results = []
30
- processed = set()
31
- names = dir(object)
32
- # :dd any DynamicClassAttributes to the list of names if object is a class;
33
- # this may result in duplicate entries if, for example, a virtual
34
- # attribute with the same name as a DynamicClassAttribute exists
35
- try:
36
- for base in object.__bases__:
37
- for k, v in base.__dict__.items():
38
- if isinstance(v, types.DynamicClassAttribute):
39
- names.append(k)
40
- except AttributeError:
41
- pass
42
- for key in names:
43
- # First try to get the value via getattr. Some descriptors don't
44
- # like calling their __get__ (see bug #1785), so fall back to
45
- # looking in the __dict__.
46
- try:
47
- value = getattr(object, key)
48
- # handle the duplicate key
49
- if key in processed:
50
- raise AttributeError
51
- # except AttributeError:
52
- except: # 加了这种异常获取,190919周四15:14,sqlalchemy.exc.InvalidRequestError
53
- dprint(key) # 抓不到对应的这个属性
54
- for base in mro:
55
- if key in base.__dict__:
56
- value = base.__dict__[key]
57
- break
58
- else:
59
- # could be a (currently) missing slot member, or a buggy
60
- # __dir__; discard and move on
61
- continue
62
-
63
- if not predicate or predicate(value):
64
- results.append((key, value))
65
- processed.add(key)
66
- results.sort(key=lambda pair: pair[0])
67
- return results
68
-
69
-
70
- def showdir(c, *, to_html=None, printf=True):
71
- """查看类信息
72
- 会罗列出类c的所有成员方法、成员变量,并生成一个html文
73
-
74
- 查阅一个对象的成员变量及成员方法
75
- 为了兼容linux输出df时也能对齐,有几个中文域宽处理相关的函数
76
-
77
- :param c: 要处理的对象
78
- :param to_html:
79
- win32上默认True,用chrome、explorer打开
80
- linux上默认False,直接输出到控制台
81
- :param printf:
82
- 默认是True,会输出到浏览器或控制条
83
- 设为False则不输出
84
- """
85
- # 1 输出类表头
86
- res = []
87
- object_name = func_input_message(2)['argnames'][0]
88
- if to_html is None:
89
- to_html = sys.platform == 'win32'
90
- newline = '<br/>' if to_html else '\n'
91
-
92
- t = f'==== 对象名称:{object_name},类继承关系:{inspect.getmro(type(c))},' \
93
- + f'内存消耗:{sys.getsizeof(c)}(递归子类总大小:{getasizeof(c)})Byte ===='
94
-
95
- if to_html:
96
- res.append('<p>')
97
- t = html.escape(t) + '</p>'
98
- res.append(t + newline)
99
-
100
- # 2 html的样式精调
101
- def df2str(df):
102
- if to_html:
103
- df = df.applymap(str) # 不转成文本经常有些特殊函数会报错
104
- df.index += 1 # 编号从1开始
105
- # pd.options.display.max_colwidth = -1 # 如果临时需要显示完整内容
106
- t = df.to_html()
107
- table = BeautifulSoup(t, 'lxml')
108
- table.thead.tr['bgcolor'] = 'LightSkyBlue' # 设置表头颜色
109
- ch = 'A' if '成员变量' in table.tr.contents[3].string else 'F'
110
- table.thead.tr.th.string = f'编号{ch}{len(df)}'
111
- t = table.prettify()
112
- else:
113
- # 直接转文本,遇到中文是会对不齐的,但是showdir主要用途本来就是在浏览器看的,这里就不做调整了
114
- t = dataframe_str(df)
115
- return t
116
-
117
- # 3 添加成员变量和成员函数
118
- # 成员变量
119
- members = getmembers(c)
120
- methods = filter(lambda m: not callable(getattr(c, m[0])), members)
121
- ls = []
122
- for ele in methods:
123
- k, v = ele
124
- if k.endswith(r'________'): # 这个名称的变量是我代码里的特殊标记,不显示
125
- continue
126
- attr = getattr(c, k)
127
- if isinstance(attr, enum.IntFlag): # 对re.RegexFlag等枚举类输出整数值
128
- v = typename(attr) + ',' + str(int(attr)) + ',' + str(v)
129
- else:
130
- try:
131
- text = str(v)
132
- except:
133
- text = '取不到str值'
134
- v = typename(attr) + ',' + text
135
- ls.append([k, v])
136
- df = pd.DataFrame.from_records(ls, columns=['成员变量', '描述'])
137
- res.append(df2str(df) + newline)
138
-
139
- # 成员函数
140
- methods = filter(lambda m: callable(getattr(c, m[0])), members)
141
- df = pd.DataFrame.from_records(methods, columns=['成员函数', '描述'])
142
- res.append(df2str(df) + newline)
143
- res = newline.join(res)
144
-
145
- # 4 使用chrome.exe浏览或输出到控制台
146
- # 这里底层可以封装一个chrome函数来调用,但是这个chrome需要依赖太多功能,故这里暂时手动简单调用
147
- if to_html:
148
- filename = Path(object_name, suffix='.html', root=Path.TEMP). \
149
- write(ensure_gbk(res), if_exists='replace').fullpath
150
- try:
151
- subprocess.run(['chrome.exe', filename])
152
- except FileNotFoundError:
153
- subprocess.run(['explorer', filename], shell=True)
154
- logging.warning('启动chrome.exe失败,可能是没有安装谷歌浏览器或配置环境变量。')
155
- else: # linux环境直接输出表格
156
- print(res)
157
-
158
- return res
pyxllib/debug/typelib.py DELETED
@@ -1,278 +0,0 @@
1
- #!/usr/bin/env python3
2
- # -*- coding: utf-8 -*-
3
- # @Author : 陈坤泽
4
- # @Email : 877362867@qq.com
5
- # @Data : 2020/06/02 11:09
6
-
7
-
8
- from collections import defaultdict, Counter
9
- import textwrap
10
-
11
- import pandas as pd
12
-
13
- from pyxllib.basic import *
14
-
15
-
16
- def east_asian_len(s, ambiguous_width=None):
17
- import pandas.io.formats.format as fmt
18
- return fmt.EastAsianTextAdjustment().len(s)
19
-
20
-
21
- def east_asian_shorten(s, width=50, placeholder='...'):
22
- """考虑中文情况下的域宽截断
23
-
24
- :param s: 要处理的字符串
25
- :param width: 宽度上限,仅能达到width-1的宽度
26
- :param placeholder: 如果做了截断,末尾补足字符
27
-
28
- # width比placeholder还小
29
- >>> east_asian_shorten('a', 2)
30
- 'a'
31
- >>> east_asian_shorten('a啊b'*4, 3)
32
- '..'
33
- >>> east_asian_shorten('a啊b'*4, 4)
34
- '...'
35
-
36
- >>> east_asian_shorten('a啊b'*4, 5, '...')
37
- 'a...'
38
- >>> east_asian_shorten('a啊b'*4, 11)
39
- 'a啊ba啊...'
40
- >>> east_asian_shorten('a啊b'*4, 16, '...')
41
- 'a啊ba啊ba啊b...'
42
- >>> east_asian_shorten('a啊b'*4, 18, '...')
43
- 'a啊ba啊ba啊ba啊b'
44
- """
45
- # 一、如果字符串本身不到width设限,返回原值
46
- s = textwrap.shorten(s, width * 3, placeholder='') # 用textwrap的折行功能,尽量不删除文本
47
- n = east_asian_len(s)
48
- if n < width: return s
49
-
50
- # 二、如果输入的width比placeholder还短
51
- width -= 1
52
- m = east_asian_len(placeholder)
53
- if width <= m:
54
- return placeholder[:width]
55
-
56
- # 三、需要添加 placeholder
57
- # 1 计算长度
58
- width -= m
59
-
60
- # 2 截取s
61
- try:
62
- s = s.encode('gbk')[:width].decode('gbk', errors='ignore')
63
- except UnicodeEncodeError:
64
- i, count = 0, m
65
- while i < n and count <= width:
66
- if ord(s[i]) > 127:
67
- count += 2
68
- else:
69
- count += 1
70
- i += 1
71
- s = s[:i]
72
-
73
- return s + placeholder
74
-
75
-
76
- def dataframe_str(df, *args, ambiguous_as_wide=None, shorten=True):
77
- """输出DataFrame
78
- DataFrame可以直接输出的,这里是增加了对中文字符的对齐效果支持
79
-
80
- :param df: DataFrame数据结构
81
- :param args: option_context格式控制
82
- :param ambiguous_as_wide: 是否对①②③这种域宽有歧义的设为宽字符
83
- win32平台上和linux上①域宽不同,默认win32是域宽2,linux是域宽1
84
- :param shorten: 是否对每个元素提前进行字符串化并控制长度在display.max_colwidth以内
85
- 因为pandas的字符串截取遇到中文是有问题的,可以用我自定义的函数先做截取
86
- 默认开启,不过这步比较消耗时间
87
-
88
- >> df = pd.DataFrame({'哈哈': ['a'*100, '哈\n①'*10, 'a哈'*100]})
89
- 哈哈
90
- 0 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...
91
- 1 哈 ①哈 ①哈 ①哈 ①哈 ①哈 ①哈 ①哈 ①哈 ①...
92
- 2 a哈a哈a哈a哈a哈a哈a哈a哈a哈a哈a哈a哈a哈a哈a哈a...
93
- """
94
- import pandas as pd
95
-
96
- if ambiguous_as_wide is None:
97
- ambiguous_as_wide = sys.platform == 'win32'
98
- with pd.option_context('display.unicode.east_asian_width', True, # 中文输出必备选项,用来控制正确的域宽
99
- 'display.unicode.ambiguous_as_wide', ambiguous_as_wide,
100
- 'max_columns', 20, # 最大列数设置到20列
101
- 'display.width', 200, # 最大宽度设置到200
102
- *args):
103
- if shorten: # applymap可以对所有的元素进行映射处理,并返回一个新的df
104
- df = df.applymap(lambda x: east_asian_shorten(str(x), pd.options.display.max_colwidth))
105
- s = str(df)
106
- return s
107
-
108
-
109
- def prettifystr(s):
110
- """对一个对象用更友好的方式字符串化
111
-
112
- :param s: 输入类型不做限制,会将其以友好的形式格式化
113
- :return: 格式化后的字符串
114
- """
115
- title = ''
116
- if isinstance(s, str):
117
- pass
118
- elif isinstance(s, collections.Counter): # Counter要按照出现频率显示
119
- li = s.most_common()
120
- title = f'collections.Counter长度:{len(s)}\n'
121
- # 不使用复杂的pd库,先简单用pprint即可
122
- # df = pd.DataFrame.from_records(s, columns=['value', 'count'])
123
- # s = dataframe_str(df)
124
- s = pprint.pformat(li)
125
- elif isinstance(s, (list, tuple)):
126
- title = f'{typename(s)}长度:{len(s)}\n'
127
- s = pprint.pformat(s)
128
- elif isinstance(s, (dict, set)):
129
- title = f'{typename(s)}长度:{len(s)}\n'
130
- s = pprint.pformat(s)
131
- else: # 其他的采用默认的pformat
132
- s = pprint.pformat(s)
133
- return title + s
134
-
135
-
136
- class PrettifyStrDecorator:
137
- """将函数的返回值字符串化(调用 prettifystr 美化)"""
138
-
139
- def __init__(self, func):
140
- self.func = func # 使用self.func可以索引回原始函数名称
141
- self.last_raw_res = None # last raw result,上一次执行函数的原始结果
142
-
143
- def __call__(self, *args, **kwargs):
144
- self.last_raw_res = self.func(*args, **kwargs)
145
- return prettifystr(self.last_raw_res)
146
-
147
-
148
- def dict2list(d: dict, *, nsort=False):
149
- """字典转n*2的list
150
- :param d: 字典
151
- :param nsort:
152
- True: 对key使用自然排序
153
- False: 使用d默认的遍历顺序
154
- :return:
155
- """
156
- ls = list(d.items())
157
- if nsort:
158
- ls = sorted(ls, key=lambda x: natural_sort_key(str(x[0])))
159
- return ls
160
-
161
-
162
- def dict2df(d):
163
- """dict类型转DataFrame类型"""
164
- name = typename(d)
165
- if isinstance(d, Counter):
166
- li = d.most_common()
167
- else:
168
- li = dict2list(d, nsort=True)
169
- return pd.DataFrame.from_records(li, columns=(f'{name}-key', f'{name}-value'))
170
-
171
-
172
- def list2df(li):
173
- if li and isinstance(li[0], (list, tuple)): # 有两维时按表格显示
174
- df = pd.DataFrame.from_records(li)
175
- else: # 只有一维时按一列显示
176
- df = pd.DataFrame(pd.Series(li), columns=(typename(li),))
177
- return df
178
-
179
-
180
- def try2df(arg):
181
- """尝试将各种不同的类型转成dataframe"""
182
- if isinstance(arg, dict):
183
- df = dict2df(arg)
184
- elif isinstance(arg, (list, tuple)):
185
- df = list2df(arg)
186
- elif isinstance(arg, pd.Series):
187
- df = pd.DataFrame(arg)
188
- else:
189
- df = arg
190
- return df
191
-
192
-
193
- class NestedDict:
194
- """ 字典嵌套结构相关功能
195
-
196
- TODO 感觉跟 pprint 的嵌套识别美化输出相关,可能有些代码是可以结合简化的~~
197
- """
198
-
199
- @classmethod
200
- def has_subdict(cls, data, include_self=True):
201
- """是否含有dict子结构
202
- :param include_self: 是否包含自身,即data本身是一个dict的话,也认为has_subdict是True
203
- """
204
- if include_self and isinstance(data, dict):
205
- return True
206
- elif isinstance(data, (list, tuple, set)):
207
- for v in data:
208
- if cls.has_subdict(v):
209
- return True
210
- return False
211
-
212
- @classmethod
213
- def to_html_table(cls, data, max_items=10):
214
- """ 以html表格套表格的形式,展示一个嵌套结构数据
215
- :param data: 数据
216
- :param max_items: 项目显示上限,有些数据项目太多了,要精简下
217
- 设为假值则不设上限
218
- :return:
219
- """
220
-
221
- def tohtml(d):
222
- if max_items:
223
- df = try2df(d)
224
- if len(df) > max_items:
225
- n = len(df)
226
- return df[:max_items].to_html(escape=False) + f'... {n - 1}'
227
- else:
228
- return df.to_html(escape=False)
229
- else:
230
- return try2df(d).to_html(escape=False)
231
-
232
- if not cls.has_subdict(data):
233
- res = str(data)
234
- elif isinstance(data, dict):
235
- if isinstance(data, Counter):
236
- d = data
237
- else:
238
- d = dict()
239
- for k, v in data.items():
240
- if cls.has_subdict(v):
241
- v = cls.to_html_table(v, max_items=max_items)
242
- d[k] = v
243
- res = tohtml(d)
244
- else:
245
- li = [cls.to_html_table(x, max_items=max_items) for x in data]
246
- res = tohtml(li)
247
-
248
- return res.replace('\n', ' ')
249
-
250
-
251
- class KeyValuesCounter:
252
- """ 各种键值对出现次数的统计
253
- 会递归找子字典结构,但不存储结构信息,只记录纯粹的键值对信息
254
-
255
- 应用场景:对未知的json结构,批量读取后,显示所有键值对的出现情况
256
- """
257
-
258
- def __init__(self):
259
- self.kvs = defaultdict(Counter)
260
-
261
- def add(self, data, max_value_length=100):
262
- """
263
- :param max_value_length: 添加的值,进行截断,防止有些值太长
264
- """
265
- if not NestedDict.has_subdict(data):
266
- return
267
- elif isinstance(data, dict):
268
- for k, v in data.items():
269
- if NestedDict.has_subdict(v):
270
- self.add(v)
271
- else:
272
- self.kvs[k][shorten(str(v), max_value_length)] += 1
273
- else: # 否则 data 应该是个可迭代对象,才可能含有dict
274
- for x in data:
275
- self.add(x)
276
-
277
- def to_html_table(self, max_items=10):
278
- return NestedDict.to_html_table(self.kvs, max_items=max_items)
pyxllib/image/__init__.py DELETED
@@ -1,12 +0,0 @@
1
- #!/usr/bin/env python3
2
- # -*- coding: utf-8 -*-
3
- # @Author : 陈坤泽
4
- # @Email : 877362867@qq.com
5
- # @Data : 2020/09/15 11:08
6
-
7
-
8
- """
9
- 以前用image命名的,后来改为了cv,为了跟旧版兼容,先放了一个空的image库
10
- """
11
-
12
- from pyxllib.cv import *
pyxllib/torch/__init__.py DELETED
@@ -1,20 +0,0 @@
1
- #!/usr/bin/env python3
2
- # -*- coding: utf-8 -*-
3
- # @Author : 陈坤泽
4
- # @Email : 877362867@qq.com
5
- # @Data : 2020/10/18 16:06
6
-
7
-
8
- """
9
-
10
- 跟pytorch有关的一些常用模型、训练的功能封装
11
-
12
- """
13
-
14
- from pyxllib.torch.modellib import *
15
- from pyxllib.torch.trainlib import *
16
-
17
- # 把pytorch等常用的导入写了
18
- import PIL
19
- import torch.utils.data
20
- from torchvision.datasets import VisionDataset
pyxllib/torch/modellib.py DELETED
@@ -1,37 +0,0 @@
1
- #!/usr/bin/env python3
2
- # -*- coding: utf-8 -*-
3
- # @Author : 陈坤泽
4
- # @Email : 877362867@qq.com
5
- # @Data : 2020/10/18 16:16
6
-
7
- import torch
8
- from torch import nn
9
-
10
-
11
- class LeNet5(nn.Module):
12
- """ https://towardsdatascience.com/implementing-yann-lecuns-lenet-5-in-pytorch-5e05a0911320 """
13
- def __init__(self, n_classes):
14
- super(LeNet5, self).__init__()
15
-
16
- self.feature_extractor = nn.Sequential(
17
- nn.Conv2d(in_channels=1, out_channels=6, kernel_size=5, stride=1),
18
- nn.Tanh(),
19
- nn.AvgPool2d(kernel_size=2),
20
- nn.Conv2d(in_channels=6, out_channels=16, kernel_size=5, stride=1),
21
- nn.Tanh(),
22
- nn.AvgPool2d(kernel_size=2),
23
- nn.Conv2d(in_channels=16, out_channels=120, kernel_size=5, stride=1),
24
- nn.Tanh()
25
- )
26
-
27
- self.classifier = nn.Sequential(
28
- nn.Linear(in_features=120, out_features=84),
29
- nn.Tanh(),
30
- nn.Linear(in_features=84, out_features=n_classes),
31
- )
32
-
33
- def forward(self, x):
34
- x = self.feature_extractor(x)
35
- x = torch.flatten(x, 1)
36
- logits = self.classifier(x)
37
- return logits