Helper tools
object_to_dict
Takes (almost) any Python object and deconstructs it into a dict.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
obj
|
Any object. |
required | |
include_imports
|
bool
|
Add a list of modules to import as is imported in current env (default = False). |
False
|
include_class_source
|
bool
|
Add the source of the object's class (default = False). |
False
|
include_methods
|
bool
|
Include object methods (default = False). |
False
|
Returns:
Name | Type | Description |
---|---|---|
dict |
Deconstructed object in typical Rickle dictionary format. |
Source code in rickle\tools.py
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 |
|
generate_random_value
Helper function to generate a random value.
Notes
- integer: Properties include
min
andmax
. Defaults to 0 and 256. - number: Properties include
min
andmax
. Defaults to 0 and 256. - string: Properties include
chars
andlength
. Defaults to ASCII chars and 10. - enum: Properties include
values
. Defaults to ASCII uppercase chars. - array: Properties include
values
andlength
. Defaults to 'integer' and 10.values
can be a string ofvalue_type
. - object: Properties include
keys
,values
,min
, andmax
. Defaults to random ASCII uppercase and 10 random integers, min and max of 1 and 5.values
can be a string ofvalue_type
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
value_type
|
str
|
Either 'string', 'integer', 'number', 'enum', 'array', 'object', or 'any'. |
required |
value_properties
|
dict
|
Properties about the randomly generated value. See notes. |
required |
Returns:
Name | Type | Description |
---|---|---|
value |
Randomly generated value. |
Source code in rickle\tools.py
get_native_type_name
Helper mapping from Python type names to format names.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
python_type_name
|
str
|
Python type name. |
required |
format_type
|
str
|
Format type, either yaml, json, toml, xml, ini, env, or python. |
required |
default
|
str
|
If unmatched, return this default (default = None). |
None
|
Returns:
Name | Type | Description |
---|---|---|
str |
Native name for the given format. |
Source code in rickle\tools.py
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 |
|
supported_encodings
A very rudimentary way to figure out the different supported file encodings supported.
Returns:
Name | Type | Description |
---|---|---|
list |
list
|
List (str) of encoding names. |
Source code in rickle\tools.py
classify_string
Try to classify the type from a string. This is done by attempting to load the string as each type. In the cases where the base decoder is not installed a simple Regex match is attempted.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
input_string
|
str
|
String to classify. |
required |
Returns:
Name | Type | Description |
---|---|---|
str |
The classified type ("json", "yaml", "toml", "xml", "ini", "env", "unknown") |
Source code in rickle\tools.py
toml_null_stripper
Remove null valued key-value pairs or list items.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
dictionary
|
(dict, list)
|
Input dictionary or list. |
required |
Returns:
Name | Type | Description |
---|---|---|
dict |
Output dictionary (or list). |
Source code in rickle\tools.py
infer_read_file_type
Infer the file type and return loaded contents. By default, the type is inferred from the suffix of the
file path. Thus, file.yaml
will be read as a YAML file. If the file extension is not known, the file will be
read and tried to be loaded as both types.
Raises:
Type | Description |
---|---|
ValueError
|
If the type could not be inferred. |
Parameters:
Name | Type | Description | Default |
---|---|---|---|
file_path
|
str
|
Input file path to read. |
required |
Returns:
Name | Type | Description |
---|---|---|
dict |
Loaded YAML (or JSON). |
Source code in rickle\tools.py
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 |
|
infer_read_string_type
Brute force try every possible loading.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
string
|
str
|
Input. |
required |
Returns:
Name | Type | Description |
---|---|---|
dict |
Loaded. |
Source code in rickle\tools.py
parse_ini
Func to create a dictionary from an initialised config parser and then returns inflated dictionary.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
config
|
ConfigParser
|
Initialised ConfigParser. |
required |
path_sep
|
str
|
For inflating sections from deeply nested structures (default = None). |
None
|
list_brackets
|
tuple
|
For list indexes, type of bracket (default = None). |
None
|
Returns:
Name | Type | Description |
---|---|---|
dict |
|
Source code in rickle\tools.py
unparse_ini
Function to flatten a dictionary and create ConfigParser from the result.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
dictionary
|
dict
|
Any dictionary. |
required |
path_sep
|
str
|
For creating sections from deeply nested structures (default = None). |
None
|
list_brackets
|
tuple
|
For list indexes, type of bracket (default = None). |
None
|
Returns:
Name | Type | Description |
---|---|---|
ConfigParser |
ConfigParser
|
Config parser with flattened dictionary set. |
Source code in rickle\tools.py
flatten_dict
Flattens a deepl structure python dictionary into a shallow (or 'thin') dictionary of depth 1.
Notes
Dictionary can only contain types str, bool, int, float, dict, list. Any other types won't be expanded upon.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
dictionary
|
dict
|
Input dictionary. |
required |
path_sep
|
str
|
Path separator. |
None
|
list_brackets
|
tuple
|
Tuple of strings for list index values (default = ('(', ')')). |
('(', ')')
|
Returns:
Name | Type | Description |
---|---|---|
dict |
Flattened to depth 1. |
Source code in rickle\tools.py
inflate_dict
Does reverse operation of flatten_dict
and inflates a shallow dictionary.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
flat_dict
|
dict
|
Input dictionary, can be any dict (won't have an effect). |
required |
path_sep
|
str
|
Path separator. |
None
|
list_brackets
|
tuple
|
Tuple of strings for list index values (default = ('(', ')')). |
('(', ')')
|
Returns:
Name | Type | Description |
---|---|---|
dict |
Inflated dictionary. |
Source code in rickle\tools.py
convert_string
Convert a string from one format to another.
Notes
any -> YAML, JSON, TOML, XML, INI.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
input_string
|
str
|
Data in input_type format. |
required |
output_type
|
str
|
Output type, either ['yaml', 'json', 'toml', 'xml']. |
required |
input_type
|
str
|
Input type, either ['yaml', 'json', 'toml', 'xml', 'env'] (default = None). |
None
|
Notes
Output can not be of type .ENV . If no input type is given, the type is inferred.
Returns:
Name | Type | Description |
---|---|---|
str |
Converted string |
Source code in rickle\tools.py
742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 |
|