Modules
StuffSaver
¶
StuffSaver class to save and reload data from a temporary folder
Attributes:
Name | Type | Description |
---|---|---|
path |
str
|
Path to the temporary folder where the data will be saved. |
backend |
_type_
|
Backend to use for saving the data. It should have the methods load and dump. |
Methods:
Name | Description |
---|---|
exists |
typing.Hashable) -> bool: Check if the data exists in the temporary folder. |
reload |
typing.Hashable) -> typing.Any: Reload the data from the temporary folder. |
Source code in stuff_saver/stuff_saver.py
7 8 9 10 11 12 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 |
|
__init__(path='./tmp', backend=None)
¶
Create a StuffSaver object
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path |
str
|
Path to the temporary folder where the data will be saved. Defaults to "./tmp". |
'./tmp'
|
backend |
_type_
|
Backend to use for saving the data. It should have the methods load and dump. Defaults to None which uses the pickle backend. |
None
|
Source code in stuff_saver/stuff_saver.py
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
|
delete(request_key)
¶
Deletes the file associated with the given request_key. Args: request_key (typing.Hashable): The request_key to delete the file for. Returns: None
Source code in stuff_saver/stuff_saver.py
102 103 104 105 106 107 108 109 110 111 112 113 |
|
exists(request_key)
¶
Check if the data exists in the temporary folder.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
request_key |
Hashable
|
request_key key to check if the data exists. |
required |
Returns:
Name | Type | Description |
---|---|---|
bool |
bool
|
True if the data exists in the temporary folder, False otherwise. |
Source code in stuff_saver/stuff_saver.py
39 40 41 42 43 44 45 46 47 48 49 50 |
|
reload(request_key)
¶
Reloads the data associated with the given request_key.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
request_key |
Hashable
|
The request_key to reload the data for. |
required |
Returns:
Type | Description |
---|---|
Any
|
typing.Any: The reloaded data. |
Raises:
Type | Description |
---|---|
FileNotFoundError
|
If the file associated with the request_key does not exist. |
IOError
|
If there is an error reading the file. |
Source code in stuff_saver/stuff_saver.py
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
|
reload_folder()
¶
Reloads the folder specified by self.path
and returns a list of previously saved data.
Returns:
Name | Type | Description |
---|---|---|
list |
List[Any]
|
A list of loaded data from the files in the folder. |
Source code in stuff_saver/stuff_saver.py
87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
|
save(request_key, data)
¶
Save the given data to a file.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
request_key |
Hashable
|
The request_key object used to generate the saving path. |
required |
data |
Any
|
The data to be saved. |
required |
Returns:
Type | Description |
---|---|
None
|
None |
Source code in stuff_saver/stuff_saver.py
72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
|
wrap(request_key, fn, *args, **kwargs)
¶
Wraps a function call with caching mechanism. Args: request_key (typing.Hashable): The key used to identify the cached result. fn (callable): The function to be wrapped. args: Positional arguments to be passed to the function. *kwargs: Keyword arguments to be passed to the function. Returns: typing.Any: The result of the function call. Example: >>> def add(a, b): ... return a + b ... >>> ss = StuffSaver() >>> wrapped_add = ss.wrap("add", add, 2, 3) >>> print(wrapped_add) 5
Source code in stuff_saver/stuff_saver.py
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 |
|
hasher(word)
¶
Hashes a given word and returns the hashed value.
Parameters: - word (typing.Hashable): The word to be hashed.
Returns: - str: The hashed value of the word.
Source code in stuff_saver/utils.py
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
|
mkdir_p(path)
¶
Create a directory and its parent directories if they do not exist. Args: path (str): The path of the directory to be created. Raises: OSError: If an error occurs while creating the directory.
Source code in stuff_saver/utils.py
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
|
slugify(value, lower=True)
¶
Convert a string into a slug by removing special characters and replacing spaces with hyphens.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
value |
str
|
The string to be slugified. |
required |
lower |
bool
|
Whether to convert the slug to lowercase. Defaults to True. |
True
|
Returns:
Name | Type | Description |
---|---|---|
str |
str
|
The slugified string. |
Source code in stuff_saver/utils.py
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
|