knify 1.8.54__tar.gz → 1.8.56__tar.gz

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: knify
3
- Version: 1.8.54
3
+ Version: 1.8.56
4
4
  Summary: Development tools for python
5
5
  Home-page: https://github.com/qicongsheng/knify
6
6
  Author: qicongsheng
@@ -18,15 +18,21 @@ def now_str(format: str = FORMAT_YYMMDDHMS) -> str:
18
18
  return date_to_str(now(), format)
19
19
 
20
20
 
21
- def date_to_str(date_obj: datetime, format: str = FORMAT_YYMMDDHMS) -> str:
21
+ def date_to_str(date_obj: datetime, format: str = FORMAT_YYMMDDHMS):
22
+ if date_obj is None:
23
+ return None
22
24
  if type(date_obj) == datetime.timedelta:
23
25
  return (datetime.datetime(1970, 1, 1) + date_obj).strftime(format)
24
26
  return date_obj.strftime(format)
25
27
 
26
28
 
27
29
  def str_to_date(str_obj: str, format: str) -> datetime:
30
+ if str_obj is None:
31
+ return None
28
32
  return datetime.datetime.strptime(str_obj, format)
29
33
 
30
34
 
31
35
  def date_to_timestamp(date_obj: datetime):
36
+ if date_obj is None:
37
+ return None
32
38
  return time.mktime(date_obj.timetuple())
@@ -5,6 +5,9 @@ from typing import Callable
5
5
 
6
6
  from openpyxl.reader.excel import load_workbook
7
7
 
8
+ from . import listutil
9
+ from . import objutil
10
+
8
11
 
9
12
  class Header:
10
13
  def __init__(self, index: int, name: str | None, transformer: Callable[[object], object] = None):
@@ -18,28 +21,28 @@ class HeaderBuilder:
18
21
  self.default_transformer = None
19
22
  self.headers = []
20
23
 
21
- def set_default_transformer(self, transformer: Callable[[object], object] = None):
24
+ def set_default_transformer(self, transformer: Callable[[object], object] = None) -> object:
22
25
  self.default_transformer = transformer
23
26
  return self
24
27
 
25
- def set_names(self, names: list[str] = None):
28
+ def set_names(self, names: list[str] = None) -> object:
26
29
  for index_, name in enumerate(names):
27
30
  self.headers.append(Header(index_, name, self.default_transformer))
28
31
  return self
29
32
 
30
- def set_transformer(self, name: str, transformer: Callable[[object], object] = None):
33
+ def set_transformer(self, name: str, transformer: Callable[[object], object] = None) -> object:
31
34
  for header in self.headers:
32
35
  if name == header.name:
33
36
  header.transformer = transformer
34
37
  return self
35
38
 
36
- def append(self, index: int, name: str | None, transformer: Callable[[object], object] = None):
37
- target_tindex = index if index is not None else len(self.headers)
38
- target_transformer = transformer if transformer is not None else self.default_transformer
39
- self.headers.append(Header(target_tindex, name, target_transformer))
39
+ def append(self, index: int, name: str | None, transformer: Callable[[object], object] = None) -> object:
40
+ target_index = objutil.default_if_none(index, len(self.headers))
41
+ target_transformer = objutil.default_if_none(transformer, self.default_transformer)
42
+ self.headers.append(Header(target_index, name, target_transformer))
40
43
  return self
41
44
 
42
- def to_headers(self):
45
+ def to_headers(self) -> list[Header]:
43
46
  return self.headers
44
47
 
45
48
 
@@ -47,26 +50,31 @@ def read_excel(file_path: str, sheet: str | int | None = 0, headers: list[Header
47
50
  results = []
48
51
  workbook = load_workbook(filename=file_path)
49
52
  sheet_ = workbook[sheet] if isinstance(sheet, str) else workbook[workbook.sheetnames[sheet]]
50
- headers_ = [cell.value for cell in sheet_[1]]
53
+ headers_ = [cell.value for cell in sheet_[start_row]]
51
54
  for row_idx, row in enumerate(sheet_.rows):
52
55
  if row_idx < start_row:
53
56
  continue
54
57
  result = {}
55
58
  for header_idx, header_ in enumerate(headers_):
56
59
  # 没有传入headers,使用默认header
57
- if headers is None or len(headers) == 0:
60
+ if listutil.is_empty(headers):
58
61
  result[header_] = row[header_idx].value
59
62
  # 传入了headers
60
63
  else:
61
- target_headers = list(filter(lambda h_: h_.index == header_idx, headers))
62
- header = target_headers[0] if target_headers is not None and len(target_headers) > 0 else None
64
+ header = listutil.find_first(list(filter(lambda h_: h_.index == header_idx, headers)))
63
65
  if header is None:
64
66
  continue
65
67
  else:
66
- col_name = header.name if header.name is not None else header_
68
+ col_name = objutil.default_if_none(header.name, header_)
67
69
  cell_value = row[header_idx].value
68
70
  cell_value = cell_value if header.transformer is None else header.transformer(row[header_idx].value)
69
71
  result[col_name] = cell_value
70
- if len(result.keys()) > 0:
72
+ if objutil.has_keys(result):
71
73
  results.append(result)
72
74
  return results
75
+
76
+
77
+ def read_headers(file_path: str, sheet: str | int | None = 0, header_row: int = 0):
78
+ workbook = load_workbook(filename=file_path)
79
+ sheet_ = workbook[sheet] if isinstance(sheet, str) else workbook[workbook.sheetnames[sheet]]
80
+ return [cell.value for cell in sheet_[header_row + 1]]
@@ -8,7 +8,7 @@ def get_pip_name():
8
8
 
9
9
 
10
10
  def get_version():
11
- return '1.8.54'
11
+ return '1.8.56'
12
12
 
13
13
 
14
14
  def print_version():
@@ -13,3 +13,7 @@ def is_empty(list_obj: list):
13
13
 
14
14
  def is_not_empty(list_obj: list):
15
15
  return list_obj is not None and len(list_obj) > 0
16
+
17
+
18
+ def find_first(list_obj: list) -> object:
19
+ return list_obj[0] if list_obj is not None and len(list_obj) > 0 else None
@@ -5,3 +5,7 @@
5
5
 
6
6
  def default_if_none(obj: object, default_value: object) -> object:
7
7
  return default_value if obj is None or obj == '' else obj
8
+
9
+
10
+ def has_keys(obj: object) -> bool:
11
+ return obj is not None and len(obj.keys()) > 0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: knify
3
- Version: 1.8.54
3
+ Version: 1.8.56
4
4
  Summary: Development tools for python
5
5
  Home-page: https://github.com/qicongsheng/knify
6
6
  Author: qicongsheng
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