reykit 1.1.33__py3-none-any.whl → 1.1.34__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.
reykit/rbase.py CHANGED
@@ -50,7 +50,6 @@ __all__ = (
50
50
  'is_class',
51
51
  'is_instance',
52
52
  'is_iterable',
53
- 'is_table',
54
53
  'is_num_str',
55
54
  'get_first_notnone',
56
55
  'get_stack_text',
@@ -514,7 +513,7 @@ def is_class(obj: Any) -> bool:
514
513
 
515
514
  Parameters
516
515
  ----------
517
- obj : Judge object.
516
+ obj : Ojbect.
518
517
 
519
518
  Returns
520
519
  -------
@@ -533,7 +532,7 @@ def is_instance(obj: Any) -> bool:
533
532
 
534
533
  Parameters
535
534
  ----------
536
- obj : Judge object.
535
+ obj : Ojbect.
537
536
 
538
537
  Returns
539
538
  -------
@@ -555,7 +554,7 @@ def is_iterable(
555
554
 
556
555
  Parameters
557
556
  ----------
558
- obj : Judge object.
557
+ obj : Ojbect.
559
558
  exclude_types : Exclude types.
560
559
 
561
560
  Returns
@@ -576,43 +575,6 @@ def is_iterable(
576
575
  return False
577
576
 
578
577
 
579
- def is_table(
580
- obj: Any,
581
- check_fields: bool = True
582
- ) -> bool:
583
- """
584
- Judge whether it is `list[dict]` table format and keys and keys sort of the dict are the same.
585
-
586
- Parameters
587
- ----------
588
- obj : Judge object.
589
- check_fields : Do you want to check the keys and keys sort of the dict are the same.
590
-
591
- Returns
592
- -------
593
- Judgment result.
594
- """
595
-
596
- # Judge.
597
- if type(obj) != list:
598
- return False
599
- for elem in obj:
600
- if type(elem) != dict:
601
- return False
602
-
603
- ## Check fields of table.
604
- if check_fields:
605
- keys_strs = [
606
- ':'.join([str(key) for key in elem.keys()])
607
- for elem in obj
608
- ]
609
- keys_strs_only = set(keys_strs)
610
- if len(keys_strs_only) != 1:
611
- return False
612
-
613
- return True
614
-
615
-
616
578
  def is_num_str(
617
579
  string: str
618
580
  ) -> bool:
reykit/rdata.py CHANGED
@@ -13,11 +13,14 @@ from typing import Any, TypedDict, Literal, overload
13
13
  from collections import Counter, defaultdict as Defaultdict, ChainMap
14
14
  from collections.abc import Callable, Iterable, Generator
15
15
  from itertools import chain as IChain
16
+ from decimal import Decimal
17
+ from json import dumps as json_dumps
16
18
 
17
19
  from .rbase import T, KT, VT, Base, null, check_least_one, check_most_one, is_iterable
18
20
 
19
21
 
20
22
  __all__ = (
23
+ 'to_json',
21
24
  'count',
22
25
  'flatten',
23
26
  'split',
@@ -33,6 +36,48 @@ __all__ = (
33
36
  CountResult = TypedDict('CountResult', {'element': Any, 'number': int})
34
37
 
35
38
 
39
+ def to_json(
40
+ data: Any,
41
+ compact: bool = True
42
+ ) -> str:
43
+ """
44
+ Convert data to JSON format string.
45
+
46
+ Parameters
47
+ ----------
48
+ data : Data.
49
+ compact : Whether compact content.
50
+
51
+ Returns
52
+ -------
53
+ JSON format string.
54
+ """
55
+
56
+ # Get parameter.
57
+ if compact:
58
+ indent = None
59
+ separators = (',', ':')
60
+ else:
61
+ indent = 4
62
+ separators = None
63
+
64
+ # Convert.
65
+ default = lambda value: (
66
+ value.__float__()
67
+ if type(value) == Decimal
68
+ else repr(value)
69
+ )
70
+ string = json_dumps(
71
+ data,
72
+ ensure_ascii=False,
73
+ indent=indent,
74
+ separators=separators,
75
+ default=default
76
+ )
77
+
78
+ return string
79
+
80
+
36
81
  def count(data: Iterable) -> list[CountResult]:
37
82
  """
38
83
  Group count data element value.
reykit/rlog.py CHANGED
@@ -230,12 +230,14 @@ class Log(Base):
230
230
  # Break.
231
231
 
232
232
  ## Switch.
233
- if not self.print_colour: return
233
+ if not self.print_colour:
234
+ return
234
235
 
235
236
  ## Added.
236
237
  pattern = '\033\\[[\\d;]+?m'
237
238
  result = search(pattern, format_)
238
- if result is not None: return
239
+ if result is not None:
240
+ return
239
241
 
240
242
  # 'format_time'.
241
243
  if '%(format_time)s' in format_:
@@ -733,7 +735,8 @@ class Log(Base):
733
735
  """
734
736
 
735
737
  # Break.
736
- if not self.print_replaced: return
738
+ if not self.print_replaced:
739
+ return
737
740
 
738
741
  # Reset.
739
742
  reset_print()
reykit/rmonkey.py CHANGED
@@ -30,72 +30,59 @@ def monkey_patch_sqlalchemy_result_more_fetch():
30
30
  Examples
31
31
  --------
32
32
  >>> result = connection.execute(sql)
33
- >>> result.fetch_table()
34
- >>> result.fetch_dict()
35
- >>> result.fetch_list()
36
- >>> result.fetch_df()
37
- >>> result.fetch_json()
38
- >>> result.fetch_text()
39
- >>> result.fetch_sql()
40
- >>> result.fetch_html()
41
- >>> result.fetch_csv()
42
- >>> result.fetch_excel()
33
+ >>> result.to_table()
34
+ >>> result.to_dict()
35
+ >>> result.to_list()
36
+ >>> result.to_df()
37
+ >>> result.to_json()
38
+ >>> result.to_text()
39
+ >>> result.to_sql()
40
+ >>> result.to_html()
41
+ >>> result.to_csv()
42
+ >>> result.to_excel()
43
43
  >>> result.show()
44
44
  >>> result.exist
45
45
  >>> result.empty
46
46
  """
47
47
 
48
48
  # Import.
49
+ from typing import Self
50
+ from types import MethodType
49
51
  from sqlalchemy.engine.cursor import CursorResult
50
52
  from pandas import DataFrame, NA, concat
51
53
  from .rbase import Base
52
54
  from .rstdout import echo
53
- from .rtable import (
54
- to_table,
55
- to_dict,
56
- to_list,
57
- to_df,
58
- to_json,
59
- to_text,
60
- to_sql,
61
- to_html,
62
- to_csv,
63
- to_excel
64
- )
55
+ from .rtable import Table
65
56
  from .rtime import time_to
66
57
 
67
- # Fetch result as table in 'list[dict]' format.
68
- CursorResult.fetch_table = to_table
69
58
 
70
- # Fetch result as dictionary.
71
- CursorResult.fetch_dict = to_dict
72
-
73
- # Fetch result as list.
74
- CursorResult.fetch_list = to_list
75
-
76
- # Fetch result as DataFrame object.
77
- CursorResult.fetch_df = to_df
78
-
79
- # Fetch result as JSON string.
80
- CursorResult.fetch_json = to_json
81
-
82
- # Fetch result as text.
83
- CursorResult.fetch_text = to_text
59
+ # Add.
60
+ @property
61
+ def method_data(self: Result) -> Self:
62
+ """
63
+ Get Data.
84
64
 
85
- # Fetch result as SQL string.
86
- CursorResult.fetch_sql = to_sql
65
+ Returns
66
+ -------
67
+ Self.
68
+ """
87
69
 
88
- # Fetch result as HTML string.
89
- CursorResult.fetch_html = to_html
70
+ return self
90
71
 
91
- # Fetch result as save csv format file.
92
- CursorResult.fetch_csv = to_csv
93
72
 
94
- # Fetch result as save excel file.
95
- CursorResult.fetch_excel = to_excel
73
+ CursorResult.data = method_data
74
+ CursorResult.to_table = Table.to_table
75
+ CursorResult.to_dict = Table.to_dict
76
+ CursorResult.to_list = Table.to_list
77
+ CursorResult.to_text = Table.to_text
78
+ CursorResult.to_json = Table.to_json
79
+ CursorResult.to_sql = Table.to_sql
80
+ CursorResult.to_df = Table.to_df
81
+ CursorResult.to_html = Table.to_html
82
+ CursorResult.to_csv = Table.to_csv
83
+ CursorResult.to_excel = Table.to_excel
96
84
 
97
85
 
98
- # Print result.
99
86
  def method_show(self: Result, limit: int | None = None) -> None:
100
87
  """
101
88
  Print result.
@@ -112,7 +99,7 @@ def monkey_patch_sqlalchemy_result_more_fetch():
112
99
  limit = 0
113
100
 
114
101
  # Convert.
115
- df: DataFrame = self.fetch_df()
102
+ df: DataFrame = self.to_df()
116
103
  df = df.map(time_to, raising=False)
117
104
  df = df.astype(str)
118
105
  df.replace(['NaT', '<NA>'], 'None', inplace=True)
@@ -148,7 +135,6 @@ def monkey_patch_sqlalchemy_result_more_fetch():
148
135
  CursorResult.show = method_show
149
136
 
150
137
 
151
- # Whether is exist.
152
138
  @property
153
139
  def method_exist(self: Result) -> bool:
154
140
  """
@@ -168,7 +154,6 @@ def monkey_patch_sqlalchemy_result_more_fetch():
168
154
  CursorResult.exist = method_exist
169
155
 
170
156
 
171
- # Whether is empty.
172
157
  @property
173
158
  def method_empty(self: Result) -> bool:
174
159
  """
@@ -198,19 +183,19 @@ def monkey_patch_sqlalchemy_result_more_fetch():
198
183
  __doc__ = CursorResult.__doc__
199
184
 
200
185
  # Add method.
201
- fetch_table = to_table
202
- fetch_dict = to_dict
203
- fetch_list = to_list
204
- fetch_df = to_df
205
- fetch_json = to_json
206
- fetch_text = to_text
207
- fetch_sql = to_sql
208
- fetch_html = to_html
209
- fetch_csv = to_csv
210
- fetch_excel = to_excel
211
- show = method_show
212
- exist = method_exist
213
- empty = method_empty
186
+ to_table: MethodType = Table.to_table
187
+ to_dict: MethodType = Table.to_dict
188
+ to_list: MethodType = Table.to_list
189
+ to_text: MethodType = Table.to_text
190
+ to_json: MethodType = Table.to_json
191
+ to_sql: MethodType = Table.to_sql
192
+ to_df: MethodType = Table.to_df
193
+ to_html: MethodType = Table.to_html
194
+ to_csv: MethodType = Table.to_csv
195
+ to_excel: MethodType = Table.to_excel
196
+ show: MethodType = method_show
197
+ exist: MethodType = method_exist
198
+ empty: MethodType = method_empty
214
199
 
215
200
 
216
201
  return Result
reykit/ros.py CHANGED
@@ -52,9 +52,9 @@ from lxml.etree import ElementChildIterator
52
52
  from pdfplumber import open as pdfplumber_open
53
53
 
54
54
  from .rbase import Base, throw
55
+ from .rdata import to_json
55
56
  from .rre import search, sub
56
57
  from .rsys import run_cmd
57
- from .rtext import to_json
58
58
 
59
59
 
60
60
  __all__ = (
reykit/rstdout.py CHANGED
@@ -232,7 +232,8 @@ def start_print() -> None:
232
232
  """
233
233
 
234
234
  # Check.
235
- if not ConfigStdout._stoped: return
235
+ if not ConfigStdout._stoped:
236
+ return
236
237
 
237
238
  # Start.
238
239
  sys.stdout = ConfigStdout._io_stdout
@@ -289,7 +290,8 @@ def reset_print() -> None:
289
290
  """
290
291
 
291
292
  # Check.
292
- if not ConfigStdout._modified: return
293
+ if not ConfigStdout._modified:
294
+ return
293
295
 
294
296
  # Reset.
295
297
  ConfigStdout._io_stdout.write = ConfigStdout._io_stdout_write