knify 1.8.48__tar.gz → 1.8.50__tar.gz
Sign up to get free protection for your applications and to get access to all the features.
- {knify-1.8.48 → knify-1.8.50}/PKG-INFO +2 -1
- knify-1.8.50/knify/excelutil.py +59 -0
- {knify-1.8.48 → knify-1.8.50}/knify/help.py +1 -1
- knify-1.8.50/knify/listutil.py +15 -0
- {knify-1.8.48 → knify-1.8.50}/knify.egg-info/PKG-INFO +2 -1
- {knify-1.8.48 → knify-1.8.50}/knify.egg-info/SOURCES.txt +1 -0
- {knify-1.8.48 → knify-1.8.50}/knify.egg-info/requires.txt +1 -0
- {knify-1.8.48 → knify-1.8.50}/setup.py +2 -1
- knify-1.8.48/knify/listutil.py +0 -7
- {knify-1.8.48 → knify-1.8.50}/LICENSE +0 -0
- {knify-1.8.48 → knify-1.8.50}/README.md +0 -0
- {knify-1.8.48 → knify-1.8.50}/knify/__init__.py +0 -0
- {knify-1.8.48 → knify-1.8.50}/knify/dateutil.py +0 -0
- {knify-1.8.48 → knify-1.8.50}/knify/fileutil.py +0 -0
- {knify-1.8.48 → knify-1.8.50}/knify/logger.py +0 -0
- {knify-1.8.48 → knify-1.8.50}/knify/objutil.py +0 -0
- {knify-1.8.48 → knify-1.8.50}/knify/threadutil.py +0 -0
- {knify-1.8.48 → knify-1.8.50}/knify/warnutil.py +0 -0
- {knify-1.8.48 → knify-1.8.50}/knify.egg-info/dependency_links.txt +0 -0
- {knify-1.8.48 → knify-1.8.50}/knify.egg-info/top_level.txt +0 -0
- {knify-1.8.48 → knify-1.8.50}/setup.cfg +0 -0
@@ -1,6 +1,6 @@
|
|
1
1
|
Metadata-Version: 2.1
|
2
2
|
Name: knify
|
3
|
-
Version: 1.8.
|
3
|
+
Version: 1.8.50
|
4
4
|
Summary: Development tools for python
|
5
5
|
Home-page: https://github.com/qicongsheng/knify
|
6
6
|
Author: qicongsheng
|
@@ -11,3 +11,4 @@ Platform: any
|
|
11
11
|
License-File: LICENSE
|
12
12
|
Requires-Dist: loguru
|
13
13
|
Requires-Dist: urllib3
|
14
|
+
Requires-Dist: openpyxl
|
@@ -0,0 +1,59 @@
|
|
1
|
+
#!/usr/bin/env python
|
2
|
+
# -*- coding:utf-8 -*-
|
3
|
+
# Author: qicongsheng
|
4
|
+
from typing import Callable
|
5
|
+
|
6
|
+
from openpyxl.reader.excel import load_workbook
|
7
|
+
|
8
|
+
|
9
|
+
class Header:
|
10
|
+
def __init__(self, index: int, name: str | None, transformer: Callable[[object], object] = None):
|
11
|
+
self.index = index
|
12
|
+
self.name = name
|
13
|
+
self.transformer = transformer
|
14
|
+
|
15
|
+
|
16
|
+
class HeaderBuilder:
|
17
|
+
def __init__(self):
|
18
|
+
self.default_transformer = None
|
19
|
+
self.data = []
|
20
|
+
|
21
|
+
def set_default_transformer(self, transformer: Callable[[object], object] = None):
|
22
|
+
self.default_transformer = transformer
|
23
|
+
return self
|
24
|
+
|
25
|
+
def build(self, name: str | None, transformer: Callable[[object], object] = None):
|
26
|
+
target_transformer = transformer if transformer is not None else self.default_transformer
|
27
|
+
self.data.append(Header(len(self.data), name, target_transformer))
|
28
|
+
return self
|
29
|
+
|
30
|
+
def to_headers(self):
|
31
|
+
return self.data
|
32
|
+
|
33
|
+
|
34
|
+
def read_excel(file_path: str, sheet: str | int | None = 0, headers: list[Header] | None = None, start_row: int = 1):
|
35
|
+
results = []
|
36
|
+
workbook = load_workbook(filename=file_path)
|
37
|
+
sheet_ = workbook[sheet] if isinstance(sheet, str) else workbook[workbook.sheetnames[sheet]]
|
38
|
+
headers_ = [cell.value for cell in sheet_[1]]
|
39
|
+
for row_idx, row in enumerate(sheet_.rows):
|
40
|
+
if row_idx < start_row:
|
41
|
+
continue
|
42
|
+
result = {}
|
43
|
+
for header_idx, header_ in enumerate(headers_):
|
44
|
+
# 没有传入headers,使用默认header
|
45
|
+
if headers is None or len(headers) == 0:
|
46
|
+
result[header_] = row[header_idx].value
|
47
|
+
# 传入了headers
|
48
|
+
else:
|
49
|
+
target_headers = list(filter(lambda h_: h_.index == header_idx, headers))
|
50
|
+
header = target_headers[0] if target_headers is not None and len(target_headers) > 0 else None
|
51
|
+
if header is None:
|
52
|
+
continue
|
53
|
+
else:
|
54
|
+
col_name = header.name if header.name is not None else header_
|
55
|
+
cell_value = row[header_idx].value
|
56
|
+
cell_value = cell_value if header.transformer is None else header.transformer(row[header_idx].value)
|
57
|
+
result[col_name] = cell_value
|
58
|
+
results.append(result)
|
59
|
+
return results
|
@@ -0,0 +1,15 @@
|
|
1
|
+
#!/usr/bin/env python
|
2
|
+
# -*- coding:utf-8 -*-
|
3
|
+
# Author: qicongsheng
|
4
|
+
|
5
|
+
|
6
|
+
def partition(list_obj: list, partition_size):
|
7
|
+
return [list_obj[i:i + partition_size] for i in range(0, len(list_obj), partition_size)]
|
8
|
+
|
9
|
+
|
10
|
+
def is_empty(list_obj: list):
|
11
|
+
return list_obj is None or len(list_obj) == 0
|
12
|
+
|
13
|
+
|
14
|
+
def is_not_empty(list_obj: list):
|
15
|
+
return list_obj is not None and len(list_obj) > 0
|
@@ -1,6 +1,6 @@
|
|
1
1
|
Metadata-Version: 2.1
|
2
2
|
Name: knify
|
3
|
-
Version: 1.8.
|
3
|
+
Version: 1.8.50
|
4
4
|
Summary: Development tools for python
|
5
5
|
Home-page: https://github.com/qicongsheng/knify
|
6
6
|
Author: qicongsheng
|
@@ -11,3 +11,4 @@ Platform: any
|
|
11
11
|
License-File: LICENSE
|
12
12
|
Requires-Dist: loguru
|
13
13
|
Requires-Dist: urllib3
|
14
|
+
Requires-Dist: openpyxl
|
knify-1.8.48/knify/listutil.py
DELETED
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|