Note
Note that each main class and module has a help() method, accessible via <class/module>.help(). These help() methods are simply docstring printers and hinters, so they will not be touched on in this documentation.
« Back (storage.py) |
Next » (edit.py) |
|---|---|
load.py
This module houses the Load class, a helper class that loads JSON objects stored with Storage.store() back into Storage objects.
Structure of the Module
Tip
Click on a class or method below to go straight to its documentation!
Storage.Load
Methods
Storage.Load.by_key()
@classmethod
def by_key(cls,
file_path: str,
key: Any,
raw: bool=False
) -> Storage | None:
Load a json file and find the key to extract a single key-multivalue pair and its values.
Arguments
| Symbol | Type Hint | Default | Description |
|---|---|---|---|
file_path |
str |
Required | Path to JSON file. |
key |
Any |
Required | Top-level key to search for (strings recommended). |
raw |
bool |
False |
If False attempt to decode encoded values; if True return raw stored values. |
Output
Storage: Returns a Storage object containing the loaded data if found.None: Returns None if the key was not found or if there was an error.
Example
s = Storage.Load.by_key("db.json", "users")
print(repr(s))
Storage.Load.by_index()
@classmethod
def by_index(cls,
file_path: str,
index: int,
raw: bool=False
) -> Storage | None:
Load a json file and find the index at which to extract a single key-multivalue pair and its values.
Do note that this method bases the start index at 0.
Arguments
| Symbol | Type Hint | Default | Description |
|---|---|---|---|
file_path |
str |
Required | Path to JSON file. |
index |
int |
Required | 0-based index of top-level key to return. |
raw |
bool |
False |
If False attempt to decode encoded values; if True return raw stored values. |
Returns
Storage: If sucessful, a Storage object will be returned with the loaded data.None: Only returned on failure to load the file or if the index is out of bounds.
Example
Storage.Load.keys()
@classmethod
def keys(cls,
file_path: str
) -> list[str] | None:
Load a json file and returns the keys of that file.
Arguments
| Symbol | Type Hint | Default | Description |
|---|---|---|---|
file_path |
str |
Required | Path to JSON file. |
Returns
list[str]: A list containing strings of the top level keys in the loaded JSON file.None: May return None on error or if no keys were found.
Example
Storage.Load.values()
@classmethod
def values(cls,
file_path: str,
key: Any,
keys: bool=False,
raw: bool=True
) -> list[str] | None:
Loads a json file and returns the values under the inputed key.
Unlike other loading methods, this one returns the raw values by default.
Keys can also be returned as a key-value pair if keys=True.
Arguments
| Symbol | Type Hint | Default | Description |
|---|---|---|---|
file_path |
str |
Required | Path to JSON file. |
key |
Any |
Required | Top-level key to fetch values for. |
keys |
bool |
False |
If True, returns ["prop: value", ...] strings; otherwise returns list of values. |
raw |
bool |
True |
If True, return raw stored values (no decode). If False, attempt to decode encoded ints. |
Returns
list[str]: A list containing the values under the specified key in the loaded data. Ifkeys=True, then the list will contain key-value pairs in the format “key: value”.None: Returns None if the key was not found or if there was an error.
Example
Other Info
- This class cannot be instantiated. Attempting to do so will raise
kms.NoInstantiationError. - Aside from
Storage, this is the only class with [a] method(s) that return(s) aStorageobject.