reykit 1.1.0__py3-none-any.whl → 1.1.1__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.
reydb/rparameter.py ADDED
@@ -0,0 +1,243 @@
1
+ # !/usr/bin/env python
2
+ # -*- coding: utf-8 -*-
3
+
4
+ """
5
+ @Time : 2022-12-05 14:10:02
6
+ @Author : Rey
7
+ @Contact : reyxbo@163.com
8
+ @Explain : Database parameter methods.
9
+ """
10
+
11
+
12
+ from typing import overload
13
+
14
+ from .rconnection import RDatabase, RDBConnection
15
+
16
+
17
+ __all__ = (
18
+ 'RDBParameter',
19
+ 'RDBPStatus',
20
+ 'RDBPVariable'
21
+ )
22
+
23
+
24
+ class RDBParameter(object):
25
+ """
26
+ Rey's `database parameters` type.
27
+ """
28
+
29
+
30
+ def __init__(
31
+ self,
32
+ rdatabase: RDatabase | RDBConnection,
33
+ global_: bool
34
+ ) -> None:
35
+ """
36
+ Build `database parameters` attributes.
37
+
38
+ Parameters
39
+ ----------
40
+ rdatabase : RDatabase or RDBConnection instance.
41
+ global_ : Whether base global.
42
+ """
43
+
44
+ # Set parameter.
45
+ self.rdatabase = rdatabase
46
+ self.global_ = global_
47
+
48
+
49
+ def __getitem__(self, key: str) -> str | None:
50
+ """
51
+ Get item of parameter dictionary.
52
+
53
+ Parameters
54
+ ----------
55
+ key : Parameter key.
56
+
57
+ Returns
58
+ -------
59
+ Parameter value.
60
+ """
61
+
62
+ # Get.
63
+ value = self.get(key)
64
+
65
+ return value
66
+
67
+
68
+ def __setitem__(self, key: str, value: str | float) -> None:
69
+ """
70
+ Set item of parameter dictionary.
71
+
72
+ Parameters
73
+ ----------
74
+ key : Parameter key.
75
+ value : Parameter value.
76
+ """
77
+
78
+ # Set.
79
+ params = {key: value}
80
+
81
+ # Update.
82
+ self.update(params)
83
+
84
+
85
+ class RDBPStatus(RDBParameter):
86
+ """
87
+ Rey's `database parameter status` type.
88
+ """
89
+
90
+
91
+ @overload
92
+ def get(self, key: None = None) -> dict[str, str]: ...
93
+
94
+ @overload
95
+ def get(self, key: str = None) -> str | None: ...
96
+
97
+ def get(self, key: str | None = None) -> dict[str, str] | str | None:
98
+ """
99
+ Get parameter.
100
+
101
+ Parameters
102
+ ----------
103
+ key : Parameter key.
104
+ - `None`: Return dictionary of all parameters.
105
+ - `str`: Return value of parameter.
106
+
107
+ Returns
108
+ -------
109
+ Status of database.
110
+ """
111
+
112
+ # Generate SQL.
113
+
114
+ ## Global.
115
+ if self.global_:
116
+ sql = 'SHOW GLOBAL STATUS'
117
+
118
+ ## Not global.
119
+ else:
120
+ sql = 'SHOW STATUS'
121
+
122
+ # Execute SQL.
123
+
124
+ ## Dictionary.
125
+ if key is None:
126
+ status = result.fetch_dict(val_field=1)
127
+
128
+ ## Value.
129
+ else:
130
+ sql += ' LIKE :key'
131
+ result = self.rdatabase(sql, key=key)
132
+ row = result.first()
133
+ if row is None:
134
+ status = None
135
+ else:
136
+ status = row['Value']
137
+
138
+ return status
139
+
140
+
141
+ def update(self, params: dict[str, str | float]) -> None:
142
+ """
143
+ Update parameter.
144
+
145
+ Parameters
146
+ ----------
147
+ params : Update parameter key value pairs.
148
+ """
149
+
150
+ # Throw exception.
151
+ raise AssertionError('database status not update')
152
+
153
+
154
+ class RDBPVariable(RDBParameter):
155
+ """
156
+ Rey's `database parameter variable` type.
157
+ """
158
+
159
+
160
+ @overload
161
+ def get(self, key: None = None) -> dict[str, str]: ...
162
+
163
+ @overload
164
+ def get(self, key: str = None) -> str | None: ...
165
+
166
+ def get(self, key: str | None = None) -> dict[str, str] | str | None:
167
+ """
168
+ Get parameter.
169
+
170
+ Parameters
171
+ ----------
172
+ key : Parameter key.
173
+ - `None`: Return dictionary of all parameters.
174
+ - `str`: Return value of parameter.
175
+
176
+ Returns
177
+ -------
178
+ Variables of database.
179
+ """
180
+
181
+ # Generate SQL.
182
+
183
+ ## Global.
184
+ if self.global_:
185
+ sql = 'SHOW GLOBAL VARIABLES'
186
+
187
+ ## Not global.
188
+ else:
189
+ sql = 'SHOW VARIABLES'
190
+
191
+ # Execute SQL.
192
+
193
+ ## Dictionary.
194
+ if key is None:
195
+ variables = result.fetch_dict(val_field=1)
196
+
197
+ ## Value.
198
+ else:
199
+ sql += ' LIKE :key'
200
+ result = self.rdatabase(sql, key=key)
201
+ row = result.first()
202
+ if row is None:
203
+ variables = None
204
+ else:
205
+ variables = row['Value']
206
+
207
+ return variables
208
+
209
+
210
+
211
+ def update(self, params: dict[str, str | float]) -> None:
212
+ """
213
+ Update parameter.
214
+
215
+ Parameters
216
+ ----------
217
+ params : Update parameter key value pairs.
218
+ """
219
+
220
+ # Generate SQL.
221
+ sql_set_list = [
222
+ '%s = %s' % (
223
+ key,
224
+ (
225
+ value
226
+ if value.__class__ in (int, float)
227
+ else "'%s'" % value
228
+ )
229
+ )
230
+ for key, value in params.items()
231
+ ]
232
+ sql_set = ',\n '.join(sql_set_list)
233
+
234
+ # Global.
235
+ if self.global_:
236
+ sql = f'SET GLOBAL {sql_set}'
237
+
238
+ ## Not global.
239
+ else:
240
+ sql = f'SET {sql_set}'
241
+
242
+ # Execute SQL.
243
+ self.rdatabase(sql)
reykit/__init__.py CHANGED
@@ -38,4 +38,4 @@ rzip : File compress methods.
38
38
  from typing import Final
39
39
 
40
40
 
41
- __version__: Final[str] = '1.1.0'
41
+ __version__: Final[str] = '1.1.1'
reykit/rcomm.py CHANGED
@@ -9,7 +9,7 @@
9
9
  """
10
10
 
11
11
 
12
- from typing import Any, Literal, Optional, Union
12
+ from typing import Any, Literal
13
13
  from collections.abc import Callable, Iterable
14
14
  from warnings import filterwarnings
15
15
  from os.path import abspath as os_abspath, isfile as os_isfile
@@ -149,7 +149,7 @@ def split_cookie(cookie: str) -> dict[str, str]:
149
149
  return params
150
150
 
151
151
 
152
- def get_content_type(file: Union[str, bytes]) -> Optional[str]:
152
+ def get_content_type(file: str | bytes) -> str | None:
153
153
  """
154
154
  Get HTTP content type of file.
155
155
 
@@ -184,17 +184,17 @@ def get_content_type(file: Union[str, bytes]) -> Optional[str]:
184
184
 
185
185
  def request(
186
186
  url: str,
187
- params: Optional[dict] = None,
188
- data: Optional[Union[dict, str, bytes]] = None,
189
- json: Optional[dict] = None,
190
- files: Optional[dict[str, Union[str, bytes, tuple[Union[str, bytes], dict]]]] = None,
187
+ params: dict | None = None,
188
+ data: dict | str | bytes | None = None,
189
+ json: dict | None = None,
190
+ files: dict[str, str | bytes | tuple[str | bytes, dict]] | None = None,
191
191
  headers: dict[str, str] = {},
192
- timeout: Optional[float] = None,
192
+ timeout: float | None = None,
193
193
  proxies: dict[str, str] = {},
194
194
  stream: bool = False,
195
195
  verify: bool = False,
196
- method: Optional[Literal['get', 'post', 'put', 'patch', 'delete', 'options', 'head']] = None,
197
- check: Union[bool, int, Iterable[int]] = False
196
+ method: Literal['get', 'post', 'put', 'patch', 'delete', 'options', 'head'] | None = None,
197
+ check: bool | int | Iterable[int] = False
198
198
  ) -> Response:
199
199
  """
200
200
  Send request.
@@ -318,7 +318,7 @@ def request(
318
318
  return response
319
319
 
320
320
 
321
- def download(url: str, path: Optional[str] = None) -> str:
321
+ def download(url: str, path: str | None = None) -> str:
322
322
  """
323
323
  Download file from URL.
324
324
 
@@ -363,7 +363,7 @@ def download(url: str, path: Optional[str] = None) -> str:
363
363
 
364
364
 
365
365
  def get_file_stream_time(
366
- file: Union[str, bytes, int],
366
+ file: str | bytes | int,
367
367
  bandwidth: float
368
368
  ) -> float:
369
369
  """
@@ -402,7 +402,7 @@ def get_file_stream_time(
402
402
 
403
403
  def listen_socket(
404
404
  host: str,
405
- port: Union[str, int],
405
+ port: str | int,
406
406
  handler: Callable[[bytes], Any]
407
407
  ) -> None:
408
408
  """
reykit/rdata.py CHANGED
@@ -9,7 +9,7 @@
9
9
  """
10
10
 
11
11
 
12
- from typing import Any, TypedDict, Optional, Literal, NoReturn, TypeVar, overload
12
+ from typing import Any, TypedDict, Literal, NoReturn, TypeVar, overload
13
13
  from collections.abc import Callable, Iterable, Generator
14
14
 
15
15
  from .rexception import check_least_one, check_most_one
@@ -87,7 +87,7 @@ def count(
87
87
  return result
88
88
 
89
89
 
90
- def flatten(data: Any, *, _flattern_data: Optional[list] = None) -> list:
90
+ def flatten(data: Any, *, _flattern_data: list | None = None) -> list:
91
91
  """
92
92
  Flatten data.
93
93
 
@@ -136,9 +136,9 @@ def split(data: Iterable[Element], share: None = None, bin_size: None = None) ->
136
136
  def split(data: Iterable[Element], share: int = None, bin_size: int = None) -> NoReturn: ...
137
137
 
138
138
  @overload
139
- def split(data: Iterable[Element], share: Optional[int] = None, bin_size: Optional[int] = None) -> list[list[Element]]: ...
139
+ def split(data: Iterable[Element], share: int | None = None, bin_size: int | None = None) -> list[list[Element]]: ...
140
140
 
141
- def split(data: Iterable[Element], share: Optional[int] = None, bin_size: Optional[int] = None) -> list[list[Element]]:
141
+ def split(data: Iterable[Element], share: int | None = None, bin_size: int | None = None) -> list[list[Element]]:
142
142
  """
143
143
  Split data into multiple data.
144
144
 
reykit/remail.py CHANGED
@@ -9,8 +9,6 @@
9
9
  """
10
10
 
11
11
 
12
- from typing import Optional, Union
13
- from io import BufferedIOBase
14
12
  from smtplib import SMTP
15
13
  from email.mime.multipart import MIMEMultipart
16
14
  from email.mime.text import MIMEText
@@ -101,12 +99,12 @@ class REmail(object):
101
99
 
102
100
  def create_email(
103
101
  self,
104
- title: Optional[str],
105
- text: Optional[str],
102
+ title: str | None,
103
+ text: str | None,
106
104
  attachment: dict[str, bytes],
107
- show_from: Optional[str],
108
- show_to: Optional[list[str]],
109
- show_cc: Optional[list[str]]
105
+ show_from: str | None,
106
+ show_to: list[str] | None,
107
+ show_cc: list[str] | None
110
108
  ) -> str:
111
109
  """
112
110
  create email content.
@@ -169,14 +167,14 @@ class REmail(object):
169
167
 
170
168
  def send_email(
171
169
  self,
172
- to: Union[str, list[str]],
173
- title: Optional[str] = None,
174
- text: Optional[str] = None,
170
+ to: str | list[str],
171
+ title: str | None = None,
172
+ text: str | None = None,
175
173
  attachment: dict[str, FileBytes] = {},
176
- cc: Optional[Union[str, list[str]]] = None,
177
- show_from: Optional[str] = None,
178
- show_to: Optional[Union[str, list[str]]] = None,
179
- show_cc: Optional[Union[str, list[str]]] = None
174
+ cc: str | list[str] | None = None,
175
+ show_from: str | None = None,
176
+ show_to: str | list[str] | None = None,
177
+ show_cc: str | list[str] | None = None
180
178
  ) -> None:
181
179
  """
182
180
  Send email.
reykit/rexception.py CHANGED
@@ -20,7 +20,7 @@
20
20
  """
21
21
 
22
22
 
23
- from typing import Any, Optional, Union, NoReturn
23
+ from typing import Any, NoReturn
24
24
  from types import TracebackType
25
25
  from collections.abc import Iterable
26
26
  from sys import exc_info as sys_exc_info
@@ -61,7 +61,7 @@ def throw(
61
61
  exception: type[BaseException] = AssertionError,
62
62
  value: Any = RNull,
63
63
  *values: Any,
64
- text: Optional[str] = None,
64
+ text: str | None = None,
65
65
  frame: int = 2
66
66
  ) -> NoReturn:
67
67
  """
@@ -176,7 +176,7 @@ def warn(
176
176
 
177
177
 
178
178
  def catch_exc(
179
- title: Optional[str] = None
179
+ title: str | None = None
180
180
  ) -> tuple[str, type[BaseException], BaseException, TracebackType]:
181
181
  """
182
182
  Catch exception information and print, must used in `except` syntax.
@@ -303,7 +303,7 @@ def check_file_exist(path: str) -> None:
303
303
 
304
304
  def check_response_code(
305
305
  code: int,
306
- range_: Optional[Union[int, Iterable[int]]] = None
306
+ range_: int | Iterable[int] | None = None
307
307
  ) -> bool:
308
308
  """
309
309
  Check if the response code is in range.
reykit/rimage.py CHANGED
@@ -9,7 +9,7 @@
9
9
  """
10
10
 
11
11
 
12
- from typing import Any, Union, Optional
12
+ from typing import Any
13
13
  from io import BytesIO
14
14
  from qrcode import make as qrcode_make
15
15
  from qrcode.image.pil import PilImage
@@ -42,7 +42,7 @@ monkey_image_type = monkey_path_pil_image_get_bytes()
42
42
  RImage = monkey_image_type
43
43
 
44
44
 
45
- def encode_qrcode(text: str, path: Optional[str] = None) -> bytes:
45
+ def encode_qrcode(text: str, path: str | None = None) -> bytes:
46
46
  """
47
47
  Encoding text to QR code image.
48
48
 
@@ -73,7 +73,7 @@ def encode_qrcode(text: str, path: Optional[str] = None) -> bytes:
73
73
  return file_bytes
74
74
 
75
75
 
76
- def decode_qrcode(image: Union[str, bytes]) -> list[str]:
76
+ def decode_qrcode(image: str | bytes) -> list[str]:
77
77
  """
78
78
  Decoding QR code or bar code image.
79
79
 
@@ -108,14 +108,14 @@ def decode_qrcode(image: Union[str, bytes]) -> list[str]:
108
108
 
109
109
 
110
110
  def compress_image(
111
- input_image: Union[str, bytes],
112
- ouput_image: Optional[str] = None,
111
+ input_image: str | bytes,
112
+ ouput_image: str | None = None,
113
113
  target_size: float = 0.5,
114
114
  rate: int = 5,
115
115
  reduce: bool = False,
116
116
  max_quality: int = 75,
117
117
  min_quality: int = 0
118
- ) -> Optional[bytes]:
118
+ ) -> bytes | None:
119
119
  """
120
120
  Compress image file.
121
121
 
@@ -187,7 +187,7 @@ def compress_image(
187
187
  rfile(content)
188
188
 
189
189
 
190
- def to_pimage(image: Union[str, bytes]) -> RImage:
190
+ def to_pimage(image: str | bytes) -> RImage:
191
191
  """
192
192
  Get `Image` instance of `PIL` package.
193
193
 
@@ -215,8 +215,8 @@ def to_pimage(image: Union[str, bytes]) -> RImage:
215
215
 
216
216
 
217
217
  def generate_captcha_image(
218
- text: Optional[Union[int, str]] = None,
219
- path: Optional[str] = None,
218
+ text: int | str | None = None,
219
+ path: str | None = None,
220
220
  **kwargs: Any
221
221
  ) -> bytes:
222
222
  """
reykit/rlog.py CHANGED
@@ -10,7 +10,7 @@
10
10
 
11
11
 
12
12
  from __future__ import annotations
13
- from typing import Any, Optional, Union, Literal, Final, NoReturn, overload, override
13
+ from typing import Any, Literal, Final, NoReturn, overload, override
14
14
  from collections.abc import Callable
15
15
  from queue import Queue
16
16
  from os.path import abspath as os_abspath
@@ -291,7 +291,7 @@ class RLog(object):
291
291
  def get_default_filter_method(
292
292
  self,
293
293
  format_: str,
294
- mode : Optional[Literal['print', 'file']] = None
294
+ mode : Literal['print', 'file'] | None = None
295
295
  ) -> Callable[[LogRecord], Literal[True]]:
296
296
  """
297
297
  Get default filter method of handler.
@@ -397,8 +397,8 @@ class RLog(object):
397
397
  def add_print(
398
398
  self,
399
399
  level: int = DEBUG,
400
- format_: Optional[str] = None,
401
- filter_: Optional[Callable[[LogRecord], bool]] = None
400
+ format_: str | None = None,
401
+ filter_: Callable[[LogRecord], bool] | None = None
402
402
  ) -> StreamHandler:
403
403
  """
404
404
  Add print output record handler.
@@ -443,56 +443,56 @@ class RLog(object):
443
443
  @overload
444
444
  def add_file(
445
445
  self,
446
- path: Optional[str] = None,
447
- mb: Optional[float] = None,
446
+ path: str | None = None,
447
+ mb: float | None = None,
448
448
  time: None = None,
449
449
  level: int = DEBUG,
450
- format_: Optional[str] = None,
451
- filter_: Optional[Callable[[LogRecord], bool]] = None
450
+ format_: str | None = None,
451
+ filter_: Callable[[LogRecord], bool] | None = None
452
452
  ) -> ConcurrentRotatingFileHandler: ...
453
453
 
454
454
  @overload
455
455
  def add_file(
456
456
  self,
457
- path: Optional[str] = None,
457
+ path: str | None = None,
458
458
  mb: None = None,
459
- time: Union[float, Literal['m', 'w0', 'w1', 'w2', 'w3', 'w4', 'w5', 'w6']] = None,
459
+ time: float | Literal['m', 'w0', 'w1', 'w2', 'w3', 'w4', 'w5', 'w6'] = None,
460
460
  level: int = DEBUG,
461
- format_: Optional[str] = None,
462
- filter_: Optional[Callable[[LogRecord], bool]] = None
461
+ format_: str | None = None,
462
+ filter_: Callable[[LogRecord], bool] | None = None
463
463
  ) -> ConcurrentTimedRotatingFileHandler: ...
464
464
 
465
465
  @overload
466
466
  def add_file(
467
467
  self,
468
- path: Optional[str] = None,
468
+ path: str | None = None,
469
469
  mb: None = None,
470
470
  time: Any = None,
471
471
  level: int = DEBUG,
472
- format_: Optional[str] = None,
473
- filter_: Optional[Callable[[LogRecord], bool]] = None
472
+ format_: str | None = None,
473
+ filter_: Callable[[LogRecord], bool] | None = None
474
474
  ) -> NoReturn: ...
475
475
 
476
476
  @overload
477
477
  def add_file(
478
478
  self,
479
- path: Optional[str] = None,
479
+ path: str | None = None,
480
480
  mb: float = None,
481
- time: Union[float, Literal['m', 'w0', 'w1', 'w2', 'w3', 'w4', 'w5', 'w6']] = None,
481
+ time: float | Literal['m', 'w0', 'w1', 'w2', 'w3', 'w4', 'w5', 'w6'] = None,
482
482
  level: int = DEBUG,
483
- format_: Optional[str] = None,
484
- filter_: Optional[Callable[[LogRecord], bool]] = None
483
+ format_: str | None = None,
484
+ filter_: Callable[[LogRecord], bool] | None = None
485
485
  ) -> NoReturn: ...
486
486
 
487
487
  def add_file(
488
488
  self,
489
- path: Optional[str] = None,
490
- mb: Optional[float] = None,
491
- time: Optional[Union[float, Literal['m', 'w0', 'w1', 'w2', 'w3', 'w4', 'w5', 'w6']]] = None,
489
+ path: str | None = None,
490
+ mb: float | None = None,
491
+ time: float | Literal['m', 'w0', 'w1', 'w2', 'w3', 'w4', 'w5', 'w6'] | None = None,
492
492
  level: int = DEBUG,
493
- format_: Optional[str] = None,
494
- filter_: Optional[Callable[[LogRecord], bool]] = None
495
- ) -> Union[ConcurrentRotatingFileHandler, ConcurrentTimedRotatingFileHandler]:
493
+ format_: str | None = None,
494
+ filter_: Callable[[LogRecord], bool] | None = None
495
+ ) -> ConcurrentRotatingFileHandler | ConcurrentTimedRotatingFileHandler:
496
496
  """
497
497
  Add file output record handler, can split files based on size or time.
498
498
 
@@ -613,9 +613,9 @@ class RLog(object):
613
613
 
614
614
  def add_queue(
615
615
  self,
616
- queue: Optional[Queue] = None,
616
+ queue: Queue | None = None,
617
617
  level: int = DEBUG,
618
- filter_: Optional[Callable[[LogRecord], bool]] = None
618
+ filter_: Callable[[LogRecord], bool] | None = None
619
619
  ) -> tuple[QueueHandler, Queue[LogRecord]]:
620
620
  """
621
621
  Add queue output record handler.
@@ -655,7 +655,7 @@ class RLog(object):
655
655
  self,
656
656
  method: Callable[[LogRecord], Any],
657
657
  level: int = DEBUG,
658
- filter_: Optional[Callable[[LogRecord], bool]] = None
658
+ filter_: Callable[[LogRecord], bool] | None = None
659
659
  ) -> None:
660
660
  """
661
661
  Add method record handler.
@@ -771,7 +771,7 @@ class RLog(object):
771
771
  def log(
772
772
  self,
773
773
  *messages: Any,
774
- level: Optional[int] = None,
774
+ level: int | None = None,
775
775
  catch: bool = True,
776
776
  **params: Any
777
777
  ) -> None:
@@ -970,7 +970,7 @@ class RRecord(object):
970
970
 
971
971
  def __init__(
972
972
  self,
973
- path: Optional[str] = '_rrecord'
973
+ path: str | None = '_rrecord'
974
974
  ) -> None:
975
975
  """
976
976
  Build `record` attributes.
reykit/rmonkey.py CHANGED
@@ -29,7 +29,6 @@ def monkey_patch_sqlalchemy_result_more_fetch():
29
29
  """
30
30
 
31
31
 
32
- from typing import Optional
33
32
  from sqlalchemy.engine.cursor import CursorResult
34
33
  from pandas import DataFrame, NA, concat
35
34
 
@@ -81,7 +80,7 @@ def monkey_patch_sqlalchemy_result_more_fetch():
81
80
 
82
81
 
83
82
  # Print result.
84
- def method_show(self: RResult, limit: Optional[int] = None) -> None:
83
+ def method_show(self: RResult, limit: int | None = None) -> None:
85
84
  """
86
85
  Print result.
87
86
 
@@ -207,18 +206,18 @@ def monkey_patch_sqlalchemy_row_index_field():
207
206
  """
208
207
 
209
208
 
210
- from typing import Any, Union, overload
209
+ from typing import Any, overload
211
210
  from sqlalchemy.engine.row import Row
212
211
 
213
212
 
214
213
  # Define.
215
214
  @overload
216
- def __getitem__(self, index: Union[str, int]) -> Any: ...
215
+ def __getitem__(self, index: str | int) -> Any: ...
217
216
 
218
217
  @overload
219
218
  def __getitem__(self, index: slice) -> tuple: ...
220
219
 
221
- def __getitem__(self, index: Union[str, int, slice]) -> Union[Any, tuple]:
220
+ def __getitem__(self, index: str | int | slice) -> Any | tuple:
222
221
  """
223
222
  Index row value.
224
223