Utils¤
This module contains helper functions used by multiple modules.
lexos.utils._decode_bytes(raw_bytes)
¤
Decode raw bytes from a user's file into a string.
Args raw_bytes (bytes, str): The bytes to be decoded to a python string.
Returns:
Type | Description |
---|---|
str
|
The decoded string. |
Source code in lexos\utils.py
330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 |
|
lexos.utils._try_decode_bytes_(raw_bytes)
¤
Try to decode raw bytes (helper function for decode_bytes().
Parameters:
Name | Type | Description | Default |
---|---|---|---|
raw_bytes |
bytes
|
The bytes you want to decode to string. |
required |
Returns:
Type | Description |
---|---|
str
|
A decoded string. |
Source code in lexos\utils.py
296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 |
|
lexos.utils.ensure_list(item)
¤
Ensure string is converted to a Path.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
item |
Any
|
Anything. |
required |
Returns:
Type | Description |
---|---|
List
|
The item inside a list if it is not already a list. |
Source code in lexos\utils.py
22 23 24 25 26 27 28 29 30 31 32 33 |
|
lexos.utils.ensure_path(path)
¤
Ensure string is converted to a Path.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path |
Any
|
Anything. If string, it's converted to Path. |
required |
Returns:
Type | Description |
---|---|
Any
|
Path or original argument. |
Source code in lexos\utils.py
36 37 38 39 40 41 42 43 44 45 46 47 48 |
|
lexos.utils.get_encoding(input_string)
¤
Use chardet to return the encoding type of a string.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
input_string |
bytes
|
A bytestring. |
required |
Returns:
Type | Description |
---|---|
str
|
The string's encoding type. |
Source code in lexos\utils.py
217 218 219 220 221 222 223 224 225 226 227 228 |
|
lexos.utils.get_github_raw_paths(path, user=None, repo=None, branch=None)
¤
Get raw paths to files in a GitHub directory.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path |
Union[Path, str]
|
The path to the directory. |
required |
user |
Optional[str]
|
The user name of the GitHub repository. |
None
|
repo |
Optional[str]
|
The repository name of the GitHub repository. |
None
|
branch |
Optional[str]
|
The branch of the GitHub repository. |
None
|
Returns:
Name | Type | Description |
---|---|---|
list |
list
|
A list of raw download paths. |
Source code in lexos\utils.py
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
|
lexos.utils.get_paths(path)
¤
Get a list paths in a directory.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path |
Union[Path, str]
|
The path to the directory. |
required |
Returns:
Name | Type | Description |
---|---|---|
list |
list
|
A list of file paths. |
Source code in lexos\utils.py
51 52 53 54 55 56 57 58 59 60 |
|
lexos.utils.is_dir(filepath)
¤
Check if a path corresponds to a directory.
Source code in lexos\utils.py
102 103 104 |
|
lexos.utils.is_github_dir(filepath)
¤
Check if a path corresponds to a directory on GitHub.
Source code in lexos\utils.py
107 108 109 110 111 112 |
|
lexos.utils.is_docx(filepath)
¤
Check if a file is a docx.
Source code in lexos\utils.py
115 116 117 |
|
lexos.utils.is_file(filepath)
¤
Check if a path corresponds to a file.
Source code in lexos\utils.py
120 121 122 |
|
lexos.utils.is_pdf(filepath)
¤
Check if a file is a pdf.
Source code in lexos\utils.py
125 126 127 |
|
lexos.utils.is_url(s)
¤
Check if string is a URL.
Source code in lexos\utils.py
130 131 132 133 134 135 136 137 138 139 140 141 142 |
|
lexos.utils.normalize(raw_bytes)
¤
Normalise a string to LexosFile format.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
raw_bytes |
bytes
|
The input bytestring. |
required |
Returns:
Type | Description |
---|---|
str
|
Normalised version of the input string. |
Source code in lexos\utils.py
231 232 233 234 235 236 237 238 239 240 241 |
|
lexos.utils.normalize_file(filepath, destination_dir='.')
¤
Normalise a file to LexosFile format and save the file.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
filepath |
Union[Path, str]
|
The path to the input file. |
required |
destination_dir |
Union[Path, str]
|
The path to the directory where the files. will be saved. |
'.'
|
Source code in lexos\utils.py
277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 |
|
lexos.utils.normalize_files(filepaths, destination_dir='.')
¤
Normalise a list of files to LexosFile format and save the files.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
filepaths |
List[Union[Path, str]]
|
The list of paths to input files. |
required |
destination_dir |
Union[Path, str]
|
The path to the directory where the files. will be saved. |
'.'
|
Source code in lexos\utils.py
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 |
|
lexos.utils.normalize_strings(strings)
¤
Normalise a list of strings to LexosFile format.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
strings |
List[Union[bytes, str]]
|
The list of input strings. |
required |
Returns:
Type | Description |
---|---|
List[str]
|
A list of normalised versions of the input strings. |
Source code in lexos\utils.py
244 245 246 247 248 249 250 251 252 253 254 255 256 |
|
lexos.utils.to_collection(val, val_type, col_type)
¤
Validate and cast a value or values to a collection.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
val |
object
|
Value or values to validate and cast. |
required |
val_type |
type
|
Type of each value in collection, e.g. |
required |
col_type |
type
|
Type of collection to return, e.g. |
required |
Returns:
Type | Description |
---|---|
Collection[AnyVal]
|
Collection of type |
Raises:
Type | Description |
---|---|
TypeError
|
An invalid value was passed. |
Source code in lexos\utils.py
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 170 171 172 173 174 175 |
|
lexos.utils.unzip_archive(archive_path, extract_dir)
¤
Extract a zip archive.
For adding a progress indicator, see https://stackoverflow.com/questions/4006970/monitor-zip-file-extraction-python.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
archive_path |
str
|
The path to the archive file to be unzipped. |
required |
extract_dir |
str
|
The path to folder where the archive will be extracted. |
required |
Source code in lexos\utils.py
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 |
|
lexos.utils.zip_folder(source_dir, archive_file)
¤
Zip a folder recursively with no extra root folder in the archive.
Works with a progress indicator.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
source_dir |
Path
|
The path to the source directory. |
required |
archive_file |
Path
|
The path to the archive file to be created (including file extension). |
required |
Source code in lexos\utils.py
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 |
|