File tree Expand file tree Collapse file tree 5 files changed +94
-17
lines changed Expand file tree Collapse file tree 5 files changed +94
-17
lines changed Original file line number Diff line number Diff line change 11
11
useRunInThread ,
12
12
useCatchError ,
13
13
useExceptDebug ,
14
- useListify
14
+ useListify ,
15
+ useCachedProperty ,
15
16
)
16
17
from .utils import (
17
18
useTimer ,
24
25
useCookieToDict ,
25
26
useHeadersToDict
26
27
)
28
+ from .utils import useIs , useTo
29
+
27
30
from ._datetime import useDateTime
28
31
29
32
from ._path import usePath
43
46
'useExceptDebug' ,
44
47
'useListify' ,
45
48
# utils
49
+ 'useIs' ,
50
+ 'useTo' ,
46
51
'useTimer' ,
47
52
'useTimerManager' ,
48
53
'useUserAgent' ,
Original file line number Diff line number Diff line change 1
1
from . import useragent as useUserAgent
2
2
from .timer import Timer as useTimer , TimerManager as useTimerManager
3
3
from .import_object import import_object as useImport , LazyImport as useLazyImport
4
- from .to_dict import (
4
+ from ._to import (
5
5
cookie_to_dict as useCookieToDict ,
6
6
headers_to_dict as useHeadersToDict ,
7
7
data_to_dict as useDataToDict ,
8
8
)
9
9
from .utils import (
10
10
gen_unique_id as useUniqueId ,
11
11
)
12
+ from . import _to as useTo
13
+ from . import _is as useIs
Original file line number Diff line number Diff line change
1
+ """
2
+ @Author: MicLon
3
+ @Date: 2023/02/19
4
+ @Description: 用于检查数据类型的工具函数
5
+ """
6
+ import re
7
+
8
+
9
+ def is_token (value ) -> bool :
10
+ """
11
+ 检查`value`是否符合token规范
12
+ :param value: 要检查的值
13
+ :return:
14
+ """
15
+ return bool (re .match ('^[A-Za-z0-9]{3,32}$' , value ))
16
+
17
+
18
+ def is_string (value ) -> bool :
19
+ """
20
+ 检查`value`是否是字符串
21
+ :param value: 要检查的值
22
+ :return: bool
23
+ """
24
+ return isinstance (value , (str , bytes ))
25
+
26
+
27
+ def is_regexp (value ) -> bool :
28
+ """
29
+ 检查`value`是否是正则表达式
30
+ :param value: 要检查的值
31
+ :return:
32
+ """
33
+ return isinstance (value , type (re .compile ("regex_test" )))
Original file line number Diff line number Diff line change
1
+ """
2
+ @Author: MicLon
3
+ @Date: 2023/02/19
4
+ @Description: 用于转换数据类型的工具函数
5
+ """
6
+
7
+ from typing import Any
8
+
9
+
10
+ def cookie_to_dict (cookies : str ) -> dict :
11
+ """
12
+ 将字符串cookie转换为字典
13
+ :param cookies: cookie字符串
14
+ :return: dict
15
+ """
16
+ return {cookie .split ('=' )[0 ]: cookie .split ('=' )[- 1 ] for cookie in cookies .split ('; ' )}
17
+
18
+
19
+ def headers_to_dict (headers : str ) -> dict :
20
+ """
21
+ 将字符串headers转换为字典
22
+ :param headers: headers字符串
23
+ :return: dict
24
+ """
25
+ header_dict = {}
26
+ for line in headers .split ('\n ' ):
27
+ if ':' in line :
28
+ key , value = line .split (':' , 1 )
29
+ header_dict [key .strip ()] = value .strip ()
30
+ return header_dict
31
+
32
+
33
+ def data_to_dict (data : str ) -> dict :
34
+ """
35
+ 将字符串data转换为字典
36
+ :param data: data字符串。格式为`key1=value1&key2=value2`
37
+ :return: dict
38
+ """
39
+ return {item .split ('=' )[0 ]: item .split ('=' )[- 1 ] for item in data .split ('&' )}
40
+
41
+
42
+ def to_string (data : Any ) -> str :
43
+ """
44
+ 将任意数据转换为字符串
45
+ :param data: data
46
+ :return: string
47
+ """
48
+ if isinstance (data , str ):
49
+ return data
50
+ if isinstance (data , bytes ):
51
+ return data .decode ()
52
+ return str (data )
Load Diff This file was deleted.
You can’t perform that action at this time.
0 commit comments