hyfi.utils
CONFs
Source code in hyfi/utils/conf.py
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 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 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 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 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 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 328 329 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 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 |
|
ensure_kwargs(_kwargs, _fn)
staticmethod
Ensure that the given keyword arguments are valid for the given function.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
_kwargs
|
dict
|
The keyword arguments to validate. |
required |
_fn
|
callable
|
The function to validate the keyword arguments against. |
required |
Returns:
Name | Type | Description |
---|---|---|
dict |
The valid keyword arguments for the given function. |
Source code in hyfi/utils/conf.py
ensure_list(value)
staticmethod
Ensure that the given value is a list. If the value is None or an empty string, an empty list is returned. If the value is already a list, it is returned as is. If the value is a string, it is returned as a list containing only that string. Otherwise, the value is converted to a dictionary using the CONF.to_dict method and the resulting dictionary is returned as a list.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
value
|
Any
|
The value to ensure as a list. |
required |
Returns:
Name | Type | Description |
---|---|---|
List |
The value as a list. |
Source code in hyfi/utils/conf.py
is_config(cfg)
staticmethod
Determines whether the input object is a valid configuration object.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
cfg
|
Any
|
The object to check. |
required |
Returns:
Name | Type | Description |
---|---|---|
bool |
True if the object is a valid configuration object, False otherwise. |
Source code in hyfi/utils/conf.py
is_dictlike(cfg)
staticmethod
Determines whether the input object is a valid dictionary configuration object.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
cfg
|
Any
|
The object to check. |
required |
Returns:
Name | Type | Description |
---|---|---|
bool |
True if the object is a valid dictionary configuration object, False otherwise. |
Source code in hyfi/utils/conf.py
is_list(cfg)
staticmethod
Determines whether the input object is a valid list configuration object.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
cfg
|
Any
|
The object to check. |
required |
Returns:
Name | Type | Description |
---|---|---|
bool |
True if the object is a valid list configuration object, False otherwise. |
Source code in hyfi/utils/conf.py
is_listlike(cfg)
staticmethod
Determines whether the input object is a valid list configuration object.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
cfg
|
Any
|
The object to check. |
required |
Returns:
Name | Type | Description |
---|---|---|
bool |
True if the object is a valid list configuration object, False otherwise. |
Source code in hyfi/utils/conf.py
load(file_)
staticmethod
Load a configuration file and return a configuration object.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
file_
|
Union[str, Path, IO[Any]]
|
The path to the configuration file or a file-like object. |
required |
Returns:
Type | Description |
---|---|
Union[DictConfig, ListConfig]
|
Union[DictConfig, ListConfig]: The configuration object. |
Source code in hyfi/utils/conf.py
load_json(f, encoding='utf-8', **kwargs)
staticmethod
Load a JSON file into a dictionary.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
f
|
Union[str, Path, IO[Any]]
|
The path to the file or a file-like object. |
required |
encoding
|
str
|
The encoding to use. Defaults to "utf-8". |
'utf-8'
|
**kwargs
|
Additional arguments to pass to json.load(). |
{}
|
Returns:
Name | Type | Description |
---|---|---|
dict |
dict
|
The dictionary loaded from the JSON file. |
Source code in hyfi/utils/conf.py
merge(*configs)
staticmethod
Merge a list of previously created configs into a single one.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
*configs
|
Union[DictConfig, ListConfig, Dict[DictKeyType, Any], List[Any], Tuple[Any, ...], Any]
|
Input configs. |
()
|
Returns:
Type | Description |
---|---|
Union[ListConfig, DictConfig]
|
Union[ListConfig, DictConfig]: The merged config object. |
Source code in hyfi/utils/conf.py
merge_as_dict(*configs)
staticmethod
Merge a list of previously created configs into a single dictionary.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
*configs
|
Union[DictConfig, ListConfig, Dict[DictKeyType, Any], List[Any], Tuple[Any, ...], Any]
|
Input configs. |
()
|
Returns:
Type | Description |
---|---|
Union[ListConfig, DictConfig]
|
Union[ListConfig, DictConfig]: The merged config object as a dictionary. |
Source code in hyfi/utils/conf.py
print(cfg, resolve=True, **kwargs)
staticmethod
Prints the configuration object in a human-readable format.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
cfg
|
Any
|
The configuration object to print. |
required |
resolve
|
bool
|
Whether to resolve the configuration object before printing. Defaults to True. |
True
|
**kwargs
|
Additional keyword arguments to pass to the pprint.pprint function. |
{}
|
Returns:
Type | Description |
---|---|
None |
Source code in hyfi/utils/conf.py
replace_keys(_dict, old_key, new_key)
staticmethod
Replace a key in a dictionary.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
_dict
|
Mapping[str, Any]
|
The dictionary to update. |
required |
old_key
|
str
|
The old key to replace. |
required |
new_key
|
str
|
The new key to use. |
required |
Returns:
Name | Type | Description |
---|---|---|
Mapping |
Mapping
|
The updated dictionary. |
Source code in hyfi/utils/conf.py
save(config, f, resolve=False)
staticmethod
Save a configuration object to a file.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
config
|
Any
|
The configuration object to save. |
required |
f
|
Union[str, Path, IO[Any]]
|
The path to the file or a file-like object. |
required |
resolve
|
bool
|
Whether to resolve the configuration object before saving. Defaults to False. |
False
|
Source code in hyfi/utils/conf.py
save_json(json_dict, f, indent=4, ensure_ascii=False, default=None, encoding='utf-8', **kwargs)
staticmethod
Save a dictionary to a JSON file.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
json_dict
|
dict
|
The dictionary to save. |
required |
f
|
Union[str, Path, IO[Any]]
|
The path to the file or a file-like object. |
required |
indent
|
int
|
The number of spaces to use for indentation. Defaults to 4. |
4
|
ensure_ascii
|
bool
|
Whether to escape non-ASCII characters. Defaults to False. |
False
|
default
|
Any
|
A function to convert non-serializable objects. Defaults to None. |
None
|
encoding
|
str
|
The encoding to use. Defaults to "utf-8". |
'utf-8'
|
**kwargs
|
Additional arguments to pass to json.dump(). |
{}
|
Source code in hyfi/utils/conf.py
select(cfg, key, default=None, throw_on_resolution_failure=True, throw_on_missing=False)
staticmethod
Wrapper for OmegaConf. select value from a config object using a key.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
cfg
|
Any
|
Config node to select from |
required |
key
|
str
|
Key to select |
required |
default
|
Any
|
Default value to return if key is not found |
None
|
throw_on_resolution_failure
|
bool
|
Raise an exception if an interpolation resolution error occurs, otherwise return None |
True
|
throw_on_missing
|
bool
|
Raise an exception if an attempt to select a missing key (with the value '???') is made, otherwise return None |
False
|
Returns:
Type | Description |
---|---|
selected value or None if not found. |
Source code in hyfi/utils/conf.py
to_config(cfg)
staticmethod
Convert a config object to OmegaConf
Parameters:
Name | Type | Description | Default |
---|---|---|---|
cfg
|
Any
|
The config to convert. |
required |
Returns:
Type | Description |
---|---|
Union[DictConfig, ListConfig]
|
A Config object that corresponds to the given config. |
Source code in hyfi/utils/conf.py
to_container(cfg, resolve=False, throw_on_missing=False, enum_to_str=False, structured_config_mode=SCMode.DICT)
staticmethod
Convert the input config object to a nested container (e.g. dictionary).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
cfg
|
Any
|
The input config object. |
required |
resolve
|
bool
|
Whether to resolve the config object before converting it to a container. Defaults to False. |
False
|
throw_on_missing
|
bool
|
Whether to throw an exception if a missing key is encountered. Defaults to False. |
False
|
enum_to_str
|
bool
|
Whether to convert enum values to strings. Defaults to False. |
False
|
structured_config_mode
|
SCMode
|
The structured config mode to use. Defaults to SCMode.DICT. |
DICT
|
Returns:
Type | Description |
---|---|
The nested container (e.g. dictionary) representation of the input config object. |
Source code in hyfi/utils/conf.py
to_dict(cfg)
staticmethod
Convert a config to a dict
Parameters:
Name | Type | Description | Default |
---|---|---|---|
cfg
|
Any
|
The config to convert. |
required |
Returns:
Type | Description |
---|---|
Any
|
The dict representation of the config. |
Source code in hyfi/utils/conf.py
to_yaml(cfg, resolve=False, sort_keys=False)
staticmethod
Convert the input config object to a YAML string.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
cfg
|
Any
|
The input config object. |
required |
resolve
|
bool
|
Whether to resolve the config object before converting it to YAML. Defaults to False. |
False
|
sort_keys
|
bool
|
Whether to sort the keys in the resulting YAML string. Defaults to False. |
False
|
Returns:
Name | Type | Description |
---|---|---|
str |
str
|
The YAML string representation of the input config object. |
Source code in hyfi/utils/conf.py
update(_dict, _overrides)
staticmethod
Update a dictionary with overrides.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
_dict
|
Mapping[str, Any]
|
The dictionary to update. |
required |
_overrides
|
Mapping[str, Any]
|
The dictionary with overrides. |
required |
Returns:
Name | Type | Description |
---|---|---|
Mapping |
Mapping
|
The updated dictionary. |
Source code in hyfi/utils/conf.py
DATASETs
Bases: DSAggregate
, DSBasic
, DSCombine
, DSLoad
, DSPlot
, DSReshape
, DSSave
, DSSlice
, DSUtils
A class representing a collection of datasets.
This class inherits from various dataset utility classes and provides a convenient way to access and manipulate datasets.
Source code in hyfi/utils/datasets/__init__.py
ENVs
Source code in hyfi/utils/envs.py
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 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 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 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 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 |
|
check_and_set_osenv_var(key, value)
staticmethod
Check and set value to environment variable
Source code in hyfi/utils/envs.py
dotenv_values(dotenv_path='', **kwargs)
staticmethod
Load dotenv file and return a dict of key / value pairs. This is a wrapper around : py : func : dotenv. dotenv_values
Parameters:
Name | Type | Description | Default |
---|---|---|---|
dotenv_path
|
str
|
path to. env file |
''
|
Returns:
Type | Description |
---|---|
dict of key / value pairs ( key = value ) |
Source code in hyfi/utils/envs.py
expand_posix_vars(posix_expr, context=None)
staticmethod
Expand POSIX variables in a string.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
posix_expr
|
str
|
The string containing POSIX variables to be expanded. |
required |
context
|
dict
|
A dictionary containing additional variables to be used in the expansion. Defaults to None. |
None
|
Returns:
Name | Type | Description |
---|---|---|
str |
str
|
The expanded string. |
Examples:
>>> expand_posix_vars("$HOME")
'/home/user'
>>> expand_posix_vars("$HOME/$USER", {"USER": "testuser"})
'/home/user/testuser'
Source code in hyfi/utils/envs.py
find_dotenv(filename='.env', raise_error_if_not_found=False, usecwd=False)
staticmethod
Search in increasingly higher folders for the given file
Returns path to the file if found, or an empty string otherwise
Source code in hyfi/utils/envs.py
find_dotenv_dir(filename='.env', raise_error_if_not_found=False, usecwd=False)
staticmethod
Search in increasingly higher folders for the given file
Returns path to the file if found, or an empty string otherwise
Source code in hyfi/utils/envs.py
get_osenv(key='', default=None)
staticmethod
Get the value of an environment variable or return the default value
getcwd()
staticmethod
Get the original working directory before Hydra changed it.
This function tries to call the get_original_cwd
function from the hydra.utils
module,
which returns the original working directory if it exists. If the get_original_cwd
function
raises a ValueError
exception, it means that Hydra did not change the working directory,
so the function falls back to calling the os.getcwd
function, which returns the current
working directory.
Returns:
Name | Type | Description |
---|---|---|
str |
The original working directory before Hydra changed it. |
Source code in hyfi/utils/envs.py
is_interactive()
staticmethod
Decide whether this is running in a REPL or IPython notebook
load_dotenv(override=False, dotenv_file='.env', raise_error_if_not_found=False, usecwd=False, verbose=False, **kwargs)
staticmethod
Load. env file from given directory or from current directory. This is a convenience function for use in tests that want to run dotenv in a non - interactive environment
Parameters:
Name | Type | Description | Default |
---|---|---|---|
override
|
bool
|
If True override existing. env file |
False
|
dotenv_file
|
str
|
Name of. env file to look for in given directory or current directory |
'.env'
|
verbose
|
bool
|
Print debug information to console |
False
|
Returns:
Type | Description |
---|---|
None
|
None or a Path object for the. env file |
Source code in hyfi/utils/envs.py
osenv()
staticmethod
Return the os environment variables as a dictionary.
Returns:
Name | Type | Description |
---|---|---|
dict |
A dictionary containing the os environment variables. |
set_osenv(key, value)
staticmethod
Set the value of an environment variable
Source code in hyfi/utils/envs.py
FUNCs
Source code in hyfi/utils/funcs.py
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 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 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 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 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 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 328 329 |
|
any_to_utf8(b)
staticmethod
Convert any string to utf-8
Source code in hyfi/utils/funcs.py
cast_str_to_bool(value)
staticmethod
Parse anything to bool.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
value
|
Any
|
Anything to be casted to a bool. Tries to be as smart as possible.
|
required |
Source code in hyfi/utils/funcs.py
check_max_len(s, len_func, max_len)
staticmethod
check_min_len(s, len_func, min_len)
staticmethod
dict_product(dicts)
staticmethod
list(dict_product(dict(number=[1,2], character='ab'))) [{'character': 'a', 'number': 1}, {'character': 'a', 'number': 2}, {'character': 'b', 'number': 1}, {'character': 'b', 'number': 2}]
Source code in hyfi/utils/funcs.py
fancy_print(*args, color=None, bold=False, **kwargs)
staticmethod
Print with color and bold
Source code in hyfi/utils/funcs.py
force_str_end(original_str, end='\n')
staticmethod
Make sure a original_str
ends with end
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
original_str
|
str
|
String that you want to ensure ending. |
required |
end
|
str
|
String that must exist at the end of |
'\n'
|
Source code in hyfi/utils/funcs.py
get_offset_ranges(count, num_workers)
staticmethod
Get offset ranges for parallel processing
Source code in hyfi/utils/funcs.py
human_readable_type_name(t)
staticmethod
Generates a useful-for-humans label for a type. For builtin types, it's just the class name (eg "str" or "int"). For other types, it includes the module (eg "pathlib.Path").
Source code in hyfi/utils/funcs.py
humanbytes(B, units=None)
staticmethod
Return the given bytes as a human friendly KB, MB, GB, or TB string
Source code in hyfi/utils/funcs.py
len_bytes(x)
staticmethod
len_segments(x, sep)
staticmethod
len_sents(x, sep)
staticmethod
len_words(x)
staticmethod
len_wospc(x)
staticmethod
lower_case_with_underscores(string)
staticmethod
Converts 'CamelCased' to 'camel_cased'.
now(_format='%Y-%m-%d %H:%M:%S')
staticmethod
ordinal(num)
staticmethod
parse_size(sizestr)
staticmethod
Parse a size string into a number of bytes. For example, "16K" will return 16384. If no suffix is provided, bytes are assumed. This function is case-insensitive.
:param sizestr: A string representing a size, such as "16K", "2M", "1G". :return: The number of bytes that the string represents.
Source code in hyfi/utils/funcs.py
printf(action, msg='', style=None, indent=10, verbose=True, file_=sys.stdout)
staticmethod
Print string with common format.
Source code in hyfi/utils/funcs.py
printf_exception(e, action, msg='', indent=0, quiet=False)
staticmethod
Print exception with common format.
Source code in hyfi/utils/funcs.py
readable_types_list(type_list)
staticmethod
Generates a useful-for-humans label for a list of types.
strptime(_date_str, _format='%Y-%m-%d')
staticmethod
Return a datetime object from a string
to_dateparm(_date, _format='%Y-%m-%d')
staticmethod
Return a date parameter string
Source code in hyfi/utils/funcs.py
today(_format='%Y-%m-%d')
staticmethod
unescape_dict(d)
staticmethod
GPUs
Source code in hyfi/utils/gpumon.py
gpu_usage(all=False, attrList=None, useOldCode=False)
staticmethod
Show GPU utilization in human readable format. This is a wrapper around the GPUtil library.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
all
|
If True show all available GPUs ( default : False ) |
False
|
|
attrList
|
List of attributes to show ( default : None ) |
None
|
|
useOldCode
|
If True use old code instead of new code ( default : False ) |
False
|
Returns:
Type | Description |
---|---|
A string with the |
Source code in hyfi/utils/gpumon.py
IOLIBs
Source code in hyfi/utils/iolibs.py
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 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 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 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 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 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 328 329 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 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 |
|
append_to_jsonl(data, filename, encoding='utf-8')
staticmethod
Append a json payload to the end of a jsonl file.
Source code in hyfi/utils/iolibs.py
check_path(_path, alt_path='')
staticmethod
copy(src, dst, follow_symlinks=True)
staticmethod
Copy a file or directory. This is a wrapper around shutil.copy. If you need to copy an entire directory (including all of its contents), or if you need to overwrite existing files in the destination directory, shutil.copy() would be a better choice.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
src
|
PathLikeType
|
Path to the file or directory to be copied. |
required |
dst
|
PathLikeType
|
Path to the destination directory. If the destination directory does not exist it will be created. |
required |
follow_symlinks
|
bool
|
Whether or not symlinks should be followed |
True
|
Source code in hyfi/utils/iolibs.py
copy_file(src, dst, follow_symlinks=True)
staticmethod
Copy one file to another place.
Source code in hyfi/utils/iolibs.py
copyfile(src, dst, follow_symlinks=True)
staticmethod
Copy a file or directory. This is a wrapper around shutil.copyfile. If you want to copy a single file from one location to another, shutil.copyfile() is the appropriate function to use.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
src
|
PathLikeType
|
Path to the file or directory to copy. |
required |
dst
|
PathLikeType
|
Path to the destination file or directory. If the destination file already exists it will be overwritten. |
required |
follow_symlinks
|
bool
|
Whether to follow symbolic links or not |
True
|
Source code in hyfi/utils/iolibs.py
exists(a, *p)
staticmethod
extractall(path, dest='', force_extract=False)
staticmethod
Extract archive file.
Parameters
path: str Path of archive file to be extracted. dest: str, optional Directory to which the archive file will be extracted. If None, it will be set to the parent directory of the archive file.
Source code in hyfi/utils/iolibs.py
get_filepaths(filename_patterns, base_dir=None, recursive=True, use_cached=False, verbose=False, **kwargs)
staticmethod
Get a list of filepaths from a list of filename patterns
Source code in hyfi/utils/iolibs.py
get_files_from_archive(archive_path, filetype='')
staticmethod
Get a list of files from an archive
Source code in hyfi/utils/iolibs.py
get_modified_time(path)
staticmethod
Return the modification time of a file
Source code in hyfi/utils/iolibs.py
glob_re(pattern, base_dir, recursive=False)
staticmethod
Glob files matching a regular expression
Source code in hyfi/utils/iolibs.py
handle_remove_readonly(func, path, exc)
staticmethod
Handle errors when trying to remove read-only files through shutil.rmtree
.
This handler makes sure the given file is writable, then re-execute the given removal function.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
func
|
Callable
|
An OS-dependant function used to remove a file. |
required |
path
|
str
|
The path to the file to remove. |
required |
exc
|
Tuple[BaseException, OSError, TracebackType]
|
A |
required |
Source code in hyfi/utils/iolibs.py
is_dir(a, *p)
staticmethod
is_file(a, *p)
staticmethod
is_valid_regex(expr)
staticmethod
Check if a string is a valid regular expression
Source code in hyfi/utils/iolibs.py
join_path(a, *p)
staticmethod
Join path components intelligently.
load_jsonl(filename, encoding='utf-8')
staticmethod
Load a jsonl file into a list of json objects.
load_wordlist(filepath, sort=True, lowercase=False, unique=True, remove_tag=False, max_ngram_to_include=None, ngram_delimiter=';', remove_delimiter=False, encoding='utf-8', verbose=True)
staticmethod
Load the word list from the file.
Source code in hyfi/utils/iolibs.py
mkdir(_path)
staticmethod
process_wordlist(words, sort=True, lowercase=False, unique=True, remove_tag=False, max_ngram_to_include=None, ngram_delimiter=';', remove_delimiter=False, verbose=True)
staticmethod
Preprocess the word list.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
words
|
List[str]
|
List of words. |
required |
sort
|
bool
|
Sort the words. Defaults to True. |
True
|
lowercase
|
bool
|
Convert the words to lowercase. Defaults to False. |
False
|
unique
|
bool
|
Remove duplicate words. Defaults to True. |
True
|
remove_tag
|
bool
|
Remove the tag from the words. Defaults to False. |
False
|
max_ngram_to_include
|
Optional[int]
|
Maximum ngram to include. Defaults to None. |
None
|
ngram_delimiter
|
str
|
Delimiter for ngram. Defaults to ";". |
';'
|
remove_delimiter
|
bool
|
Remove the delimiter. Defaults to False. |
False
|
verbose
|
bool
|
Show the progress. Defaults to True. |
True
|
Returns:
Type | Description |
---|---|
List[str]
|
List[str]: List of words. |
Source code in hyfi/utils/iolibs.py
read(uri, mode='rb', encoding=None, head=None, **kwargs)
staticmethod
Read data from a file or url
Source code in hyfi/utils/iolibs.py
readlink(link)
staticmethod
A custom version of os.readlink/pathlib.Path.readlink.
pathlib.Path.readlink is what we ideally would want to use, but it is only available on python>=3.9. os.readlink doesn't support Path and bytes on Windows for python<3.8
Source code in hyfi/utils/iolibs.py
remove_duplicates_from_list_of_dicts(data, key)
staticmethod
Remove duplicates from a list of dicts based on a key.
Source code in hyfi/utils/iolibs.py
save_jsonl(data, filename, encoding='utf-8')
staticmethod
Save a list of json objects to a jsonl file.
Source code in hyfi/utils/iolibs.py
save_wordlist(words, filepath, sort=True, encoding='utf-8', verbose=True)
staticmethod
Save the word list to the file.
Source code in hyfi/utils/iolibs.py
walk_to_root(path)
staticmethod
Yield directories starting from the given directory up to the root
Source code in hyfi/utils/iolibs.py
LOGGING
Source code in hyfi/utils/logging.py
getLogger(_name=None, _log_level=None, **kwargs)
staticmethod
Get a logger with a given name and log level. It is possible to pass a logger name and log level to this function.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
_name
|
The name of the logger to get. If not specified the name of the module is used. |
None
|
|
_log_level
|
The log level to set. |
None
|
Returns:
Type | Description |
---|---|
Logger
|
The logger with the given name and log level set to the value specified in HYFI_LOG_LEVEL |
Source code in hyfi/utils/logging.py
setLogger(level=None, force=True, filterwarnings_action='ignore', **kwargs)
staticmethod
Set the logging level and format. This is a wrapper around logging. basicConfig to allow the user to specify a different logging level for each test and to filter warnings that are not caught by the filterwarnings_action
Parameters:
Name | Type | Description | Default |
---|---|---|---|
level
|
The logging level to use |
None
|
|
force
|
If True ( default ) all warnings will be logged even if there are no warnings |
True
|
|
filterwarnings_action
|
The action to call when a warning is logged |
'ignore'
|
Returns:
Type | Description |
---|---|
None
|
The logger that was |
Source code in hyfi/utils/logging.py
NBs
Source code in hyfi/utils/notebooks.py
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 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 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 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 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 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 328 329 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 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 |
|
clear_output(wait=False)
staticmethod
colored_str(s, color='black')
staticmethod
create_button(description, button_style='', icon='check', layout=None, **kwargs)
staticmethod
Create a button widget.
Source code in hyfi/utils/notebooks.py
create_dropdown(options, value, description, disabled=False, style=None, layout=None, **kwargs)
staticmethod
Create a dropdown widget.
Source code in hyfi/utils/notebooks.py
create_floatslider(min=0.0, max=1.0, step=0.1, value=None, description='', disabled=False, continuous_update=False, orientation='horizontal', readout=True, readout_format='.1f', style=None, layout=None, **kwargs)
staticmethod
Create a float slider widget.
Source code in hyfi/utils/notebooks.py
create_image(filename=None, format=None, width=None, height=None, **kwargs)
staticmethod
Create an image widget.
Source code in hyfi/utils/notebooks.py
create_radiobutton(options, description, value=None, disabled=False, style=None, layout=None, **kwargs)
staticmethod
Create a radiobutton widget.
Source code in hyfi/utils/notebooks.py
create_textarea(value, description, placeholder='', disabled=False, style=None, layout=None, **kwargs)
staticmethod
Create a textarea widget.
Source code in hyfi/utils/notebooks.py
display(*objs, include=None, exclude=None, metadata=None, transient=None, display_id=None, **kwargs)
staticmethod
Display an object in the current notebook.
Source code in hyfi/utils/notebooks.py
display_image(data=None, url=None, filename=None, format=None, embed=None, width=None, height=None, retina=False, unconfined=False, metadata=None, **kwargs)
staticmethod
Display an image, which can be given as raw data or a URL.
Parameters
data : unicode, str or bytes
The raw image data or a URL or filename to load the data from.
This always results in embedded image data.
url : unicode
A URL to download the data from. If you specify url=
,
the image data will not be embedded unless you also specify embed=True
.
filename : unicode
Path to a local file to load the data from.
Images from a file are always embedded.
format : unicode
The format of the image data (png/jpeg/jpg/gif). If a filename or URL is given
for format will be inferred from the filename extension.
embed : bool
Should the image data be embedded using a data URI (True) or be
loaded using an tag. Set this to True if you want the image
to be viewable later with no internet connection in the notebook.
Default is `True`, unless the keyword argument `url` is set, then
default value is `False`.
Note that QtConsole is not able to display images if `embed` is set to `False`
width : int Width in pixels to which to constrain the image in html height : int Height in pixels to which to constrain the image in html retina : bool Automatically set the width and height to half of the measured width and height. This only works for embedded images because it reads the width/height from image data. For non-embedded images, you can just set the desired display width and height directly. unconfined: bool Set unconfined=True to disable max-width confinement of the image. metadata: dict Specify extra metadata to attach to the image.
Source code in hyfi/utils/notebooks.py
get_display()
staticmethod
Get the display object for the current environment.
Source code in hyfi/utils/notebooks.py
hide_code_in_slideshow()
staticmethod
Hide code in slideshow.
Source code in hyfi/utils/notebooks.py
is_colab()
staticmethod
Check if the code is running in Google Colab.
Source code in hyfi/utils/notebooks.py
is_notebook()
staticmethod
Check if the code is running in a notebook.
Source code in hyfi/utils/notebooks.py
load_extentions(exts=None)
staticmethod
Load extentions.
Source code in hyfi/utils/notebooks.py
mount_google_drive(project_root='', project_name='', mountpoint='/content/drive', force_remount=False, timeout_ms=120000)
staticmethod
Mount Google Drive to Colab.
Source code in hyfi/utils/notebooks.py
set_matplotlib_formats(*formats, **kwargs)
staticmethod
Set matplotlib formats.
PKGs
Source code in hyfi/utils/packages.py
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 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 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 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 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 |
|
apti(name, verbose=False)
staticmethod
Install a package using apt.
Source code in hyfi/utils/packages.py
ensure_import_module(name, libpath, liburi, specname='', syspath='')
staticmethod
Ensure a module is imported, if not, clone it from a git repo and load it
Source code in hyfi/utils/packages.py
get_caller_module_name(caller_stack_depth=2)
staticmethod
Get the name of the module that called this function.
Source code in hyfi/utils/packages.py
get_module_name_stack()
staticmethod
Get the name of the module that called this function.
Source code in hyfi/utils/packages.py
get_next_level_caller_package_name()
staticmethod
Get the name of the package that called this function.
Source code in hyfi/utils/packages.py
getsource(obj)
staticmethod
Return the source code of the object.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
obj
|
str
|
The object to get the source code from. |
required |
Returns:
Name | Type | Description |
---|---|---|
str |
str
|
The source code of the object. |
Source code in hyfi/utils/packages.py
gitclone(url, targetdir='', verbose=False)
staticmethod
Clone a git repository from the specified URL.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
url
|
str
|
The URL of the git repository to clone. |
required |
targetdir
|
str
|
The directory to clone the repository into. Defaults to "". |
''
|
verbose
|
bool
|
Whether to print the output of the git command. Defaults to False. |
False
|
Source code in hyfi/utils/packages.py
load_module_from_file(name, libpath, specname='')
staticmethod
Load a module from a file
Source code in hyfi/utils/packages.py
pip(name, upgrade=False, prelease=False, editable=False, quiet=True, find_links='', requirement=False, force_reinstall=False, verbose=False, **kwargs)
staticmethod
Install a package using pip.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name
|
str
|
The name of the package to install. |
required |
upgrade
|
bool
|
Whether to upgrade the package if it is already installed. Defaults to False. |
False
|
prelease
|
bool
|
Whether to include pre-release versions. Defaults to False. |
False
|
editable
|
bool
|
Whether to install the package in editable mode. Defaults to False. |
False
|
quiet
|
bool
|
Whether to suppress output. Defaults to True. |
True
|
find_links
|
str
|
URL to look for packages at. Defaults to "". |
''
|
requirement
|
bool
|
Whether to install from the given requirements file. Defaults to False. |
False
|
force_reinstall
|
bool
|
Whether to force a reinstall of the package. Defaults to False. |
False
|
verbose
|
bool
|
Whether to print the output of the pip command. Defaults to False. |
False
|
**kwargs
|
Additional keyword arguments to pass to pip. |
{}
|
Returns:
Type | Description |
---|---|
None
|
None |
Source code in hyfi/utils/packages.py
pipi(name, verbose=False)
staticmethod
Install a package using pip.
Source code in hyfi/utils/packages.py
pipie(name, verbose=False)
staticmethod
Install a editable package using pip.
Source code in hyfi/utils/packages.py
safe_import_module(module_name)
staticmethod
Safely imports a module.
Source code in hyfi/utils/packages.py
SAFEEVAL
Source code in hyfi/utils/safeeval.py
safe_eval(expr, operators=None, functions=None, names=None)
staticmethod
Simply evaluate an expresssion
Source code in hyfi/utils/safeeval.py
SafeEval
Bases: object
A very simple expression parser.
s = SafeEval() s.eval("20 + 30 - ( 10 * 5)") 0
Source code in hyfi/utils/safeeval.py
339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 |
|
__init__(operators=None, functions=None, names=None)
Create the evaluator instance. Set up valid operators (+,-, etc) functions (add, random, get_val, whatever) and names.
Source code in hyfi/utils/safeeval.py
eval(expr, previously_parsed=None)
evaluate an expresssion, using the operators, functions and names previously set up.
Source code in hyfi/utils/safeeval.py
parse(expr)
staticmethod
parse an expression into a node tree