Tool functions
Common tool functions
read_json(file_path)
: Read json data into python dictwrite_json(content, file_path)
: Write dict into json fileread_pickle(file_path)
: Read content of pickle filewrite_pickle(content, file_path)
: Write content to pickle fileexecute_cmd(cmd)
: Execute given shell commandgen_md5(string_or_file_path)
: Generate md5 value of a given file path or a stringdownload_file(url, save_path)
: Download file for a given link
download_file(url, save_path='download_file', **kwargs)
Download file for a given link
Parameters:
Name | Type | Description | Default |
---|---|---|---|
url |
str
|
download link |
required |
save_path |
Union[str, Path]
|
file save path |
'download_file'
|
**kwargs |
|
{}
|
Returns:
Type | Description |
---|---|
bool
|
download result: True if download success else False |
Source code in cptools\function.py
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 |
|
execute_cmd(cmd, timeout=900)
Execute given shell command
Parameters:
Name | Type | Description | Default |
---|---|---|---|
cmd |
Union[str, list]
|
command, could be a list or string. e.g., ['ls', '-l'] or 'ls -l' |
required |
timeout |
int
|
900
|
Returns:
Type | Description |
---|---|
dict
|
A dict of execute result, including errcode and errmsg(if errcode==0, errmsg is output) |
Source code in cptools\function.py
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
|
gen_md5(string_or_file_path)
Generate md5 value of a given file path or a string
Parameters:
Name | Type | Description | Default |
---|---|---|---|
string_or_file_path |
Union[str, Path]
|
string literal or a file path |
required |
Returns:
Type | Description |
---|---|
str
|
md5 |
Source code in cptools\function.py
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
|
read_json(file_path, by_line=False, **kwargs)
Read json data into python dict
Parameters:
Name | Type | Description | Default |
---|---|---|---|
file_path |
Union[str, Path]
|
file save path |
required |
by_line |
bool
|
if True, read data line by line |
False
|
**kwargs |
other parameters used in open() |
{}
|
Returns:
Type | Description |
---|---|
OrderedDict
|
json content |
Source code in cptools\function.py
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
|
read_pickle(file_path, **kwargs)
Read content of pickle file
Parameters:
Name | Type | Description | Default |
---|---|---|---|
file_path |
Union[str, Path]
|
file save path |
required |
**kwargs |
other parameters used in open() |
{}
|
Returns:
Type | Description |
---|---|
object
|
content of pickle file |
Source code in cptools\function.py
64 65 66 67 68 69 70 71 72 73 74 75 |
|
write_json(content, file_path, by_line=False, **kwargs)
Write dict into json file
Parameters:
Name | Type | Description | Default |
---|---|---|---|
content |
dict
|
data dict |
required |
file_path |
Union[str, Path]
|
file save path |
required |
by_line |
bool
|
if True, write data line by line |
False
|
**kwargs |
other parameters used in open() |
{}
|
Returns:
Type | Description |
---|---|
None |
Source code in cptools\function.py
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
|
write_pickle(content, file_path, **kwargs)
Write content to pickle file
Parameters:
Name | Type | Description | Default |
---|---|---|---|
content |
object
|
python object |
required |
file_path |
Union[str, Path]
|
file save path |
required |
**kwargs |
other parameters used in open() |
{}
|
Returns:
Type | Description |
---|---|
None |
Source code in cptools\function.py
78 79 80 81 82 83 84 85 86 87 88 89 |
|