pybasemkit API Documentation
argparse_action
Created on 2025-08-29
@author: wf
StoreDictKeyPair
Bases: Action
Custom argparse action to store key-value pairs as a dictionary.
This class implements an argparse action to parse and store command-line arguments in the form of key-value pairs. The pairs should be separated by a comma and each key-value pair should be separated by an equals sign.
Example
--option key1=value1,key2=value2,key3=value3
Reference
https://stackoverflow.com/a/42355279/1497139
Source code in basemkit/argparse_action.py
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 | |
__call__(_parser, namespace, values, _option_string=None)
Parse key-value pairs and store them as a dictionary in the namespace.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
parser
|
ArgumentParser
|
The argument parser object. |
required |
namespace
|
Namespace
|
The namespace to store the parsed values. |
required |
values
|
str
|
The string containing key-value pairs separated by commas. |
required |
option_string
|
Optional[str]
|
The option string, if provided. |
required |
Source code in basemkit/argparse_action.py
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | |
base_cmd
Created on 2025-06-16
Minimal reusable command line base class with standard options.
@author: wf
BaseCmd
Minimal reusable command line base class with standard options: --about, --debug, --force, --quiet, --verbose, --version.
Intended to be subclassed by tools requiring consistent CLI behavior.
Source code in basemkit/base_cmd.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 | |
__init__(version, description=None)
Initialize the BaseCmd instance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
version
|
Any
|
An object with .name, .version, .description, and .doc_url attributes. |
required |
description
|
str
|
Optional CLI description. Defaults to version.description. |
None
|
Source code in basemkit/base_cmd.py
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | |
add_arguments(parser)
Add standard CLI arguments to the parser, sorted by long option name.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
parser
|
ArgumentParser
|
The parser to add arguments to. |
required |
Source code in basemkit/base_cmd.py
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 | |
cmd_parse(argv)
delegate method
Source code in basemkit/base_cmd.py
112 113 114 115 | |
getArgParser(description, version_msg)
Compatibility layer for legacy camelCase contract. Calls get_arg_parser with overridden description and version_msg.
Source code in basemkit/base_cmd.py
75 76 77 78 79 80 81 82 83 84 | |
get_arg_parser()
Create and configure the argument parser.
Returns:
| Name | Type | Description |
|---|---|---|
ArgumentParser |
ArgumentParser
|
The configured argument parser. |
Source code in basemkit/base_cmd.py
86 87 88 89 90 91 92 93 94 | |
handle_args(args)
Handle parsed arguments. Intended to be overridden in subclasses.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
args
|
Namespace
|
Parsed argument namespace. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
True if argument was handled and no further processing is required. |
Source code in basemkit/base_cmd.py
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 | |
handle_exception(e)
Handle exceptions occurring during execution. Subclasses can override this to provide custom error handling.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
e
|
BaseException
|
The exception that was raised. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
int |
int
|
The exit code (1 for KeyboardInterrupt, 2 for other exceptions). |
Source code in basemkit/base_cmd.py
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 | |
main(version, argv=None)
classmethod
Entry point for scripts using this command line interface.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
version
|
Any
|
Version metadata object passed to constructor. |
required |
argv
|
list
|
Optional command line arguments. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
int |
int
|
Exit code from |
Source code in basemkit/base_cmd.py
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 | |
optional_debug(args)
Optionally start pydevd remote debugging if debugServer is specified.
see https://www.pydev.org/manual_adv_remote_debugger.html
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
args
|
Namespace
|
Parsed CLI arguments |
required |
Source code in basemkit/base_cmd.py
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 | |
parse_args(argv=None)
Parse command line arguments.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
argv
|
list
|
Optional list of command line arguments. Defaults to sys.argv. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
Namespace |
Namespace
|
Parsed argument values. |
Source code in basemkit/base_cmd.py
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 | |
run(argv=None)
Execute the command line logic.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
argv
|
list
|
Optional command line arguments. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
int |
int
|
Exit code: 0 = OK, 1 = KeyboardInterrupt, 2 = Exception. |
Source code in basemkit/base_cmd.py
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 | |
basetest
Created on 2021-08-19
@author: wf
Basetest
Bases: TestCase
base test case
Source code in basemkit/basetest.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 | |
inPublicCI()
staticmethod
are we running in a public Continuous Integration Environment?
Source code in basemkit/basetest.py
36 37 38 39 40 41 42 43 | |
isUser(name)
staticmethod
Checks if the system has the given name
Source code in basemkit/basetest.py
45 46 47 48 | |
setUp(debug=False, profile=True)
setUp test environment
Source code in basemkit/basetest.py
22 23 24 25 26 27 28 29 30 | |
timeout(seconds)
staticmethod
Decorator to enforce a timeout on test methods.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
seconds
|
float
|
Timeout duration in seconds. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Callable |
Callable
|
A decorator that wraps a function and raises TimeoutError if it exceeds the allowed execution time. |
Raises:
| Type | Description |
|---|---|
TimeoutError
|
If the wrapped function exceeds the timeout. |
Exception
|
If the wrapped function raises any other exception. |
Source code in basemkit/basetest.py
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 | |
docker_util
Created on 2025-05-14
@author: wf
DockerUtil
docker utilities
Source code in basemkit/docker_util.py
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 | |
bash()
bash into the server container.
Source code in basemkit/docker_util.py
254 255 256 257 258 | |
docker_cmd(cmd, options='', args='')
create the given docker command with the given options
Source code in basemkit/docker_util.py
207 208 209 210 211 212 213 214 215 216 217 | |
docker_info()
Check if Docker is responsive on the host system.
Source code in basemkit/docker_util.py
237 238 239 240 241 242 | |
handle_exception(context, ex)
handle the given exception
Source code in basemkit/docker_util.py
30 31 32 33 34 35 36 37 38 39 40 41 | |
inspect()
Retrieve full .State of the Docker container.
Returns:
| Name | Type | Description |
|---|---|---|
dict |
Optional[Dict[str, Any]]
|
parsed .State structure or None on error |
Source code in basemkit/docker_util.py
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 | |
line_patch(path, line_callback, title, msg)
Patch a file in the container line-by-line via callback and check in using RCS.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str
|
Path to file inside container. |
required |
line_callback
|
Callable[[str], Tuple[str, bool]]
|
Function to patch a line. Returns (line, found). |
required |
title
|
str
|
What is being patched, used for error message. |
required |
msg
|
str
|
RCS check-in message. |
required |
Source code in basemkit/docker_util.py
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 | |
logs()
show the logs of the container
Source code in basemkit/docker_util.py
232 233 234 235 | |
patch_file(file_path, callback, push_back=True)
Copy a file from the container, apply a patch callback, and optionally copy it back.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file_path
|
str
|
Absolute path to the file inside the container. |
required |
callback
|
Callable[[str], None]
|
Function to apply changes to the local copy. |
required |
push_back
|
bool
|
If True, copy the modified file back to the container. |
True
|
Source code in basemkit/docker_util.py
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 | |
rm()
remove the server container.
Source code in basemkit/docker_util.py
249 250 251 252 | |
run(command)
Run a command in the container
Source code in basemkit/docker_util.py
135 136 137 138 139 | |
run_docker_cmd(cmd, options='', args='')
run the given docker commmand with the given options
Source code in basemkit/docker_util.py
219 220 221 222 223 224 225 226 227 228 229 230 | |
run_local(cmd, tee=False)
Run a command with sourced profile
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cmd
|
str
|
The command to run |
required |
tree
|
if true show stdout/stderr while running the command |
required |
Returns:
| Type | Description |
|---|---|
CompletedProcess
|
subprocess.CompletedProcess: The result of the command |
Source code in basemkit/docker_util.py
141 142 143 144 145 146 147 148 149 150 151 152 153 | |
run_script(name, script_content, tee=False, *args)
Run a script in the container with parameters
Source code in basemkit/docker_util.py
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 | |
run_shell_command(command, success_msg=None, error_msg=None)
Helper function for running shell commands with consistent error handling.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
command
|
str
|
Shell command to run |
required |
success_msg
|
str
|
Message to log on success |
None
|
error_msg
|
str
|
Message to log on error |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
shell_result |
ShellResult
|
a shell result |
Source code in basemkit/docker_util.py
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 | |
stop()
stop the server container
Source code in basemkit/docker_util.py
244 245 246 247 | |
persistent_log
Created on 2024-10-04
@author: wf
Log
Wrapper for persistent logging.
Source code in basemkit/persistent_log.py
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 | |
__post_init__()
Initializes the log with level mappings and updates the level counts.
Source code in basemkit/persistent_log.py
62 63 64 65 66 67 68 69 70 71 72 73 74 | |
clear()
Clears all log entries.
Source code in basemkit/persistent_log.py
76 77 78 79 80 81 | |
color_msg(color, msg)
Display a colored message
Source code in basemkit/persistent_log.py
58 59 60 | |
dump()
dump my entries
Source code in basemkit/persistent_log.py
141 142 143 144 145 146 | |
get_counter(level)
Returns the counter for the specified log level.
Source code in basemkit/persistent_log.py
93 94 95 96 97 | |
get_level_summary(level, limit=7)
Get a summary of the most common counts for the specified log level.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
level
|
str
|
The log level name ('error', 'warn', 'info'). |
required |
limit
|
int
|
The maximum number of most common entries to include in the summary (default is 7). |
7
|
Returns:
| Type | Description |
|---|---|
Tuple[int, str]
|
Tuple[int, str]: A tuple containing the count of log entries and a summary message. |
Source code in basemkit/persistent_log.py
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 | |
log(icon, kind, msg)
Log a message with the specified icon and kind.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
icon
|
str
|
The icon representing the log level ('❌', '⚠️', '✅'). |
required |
kind
|
str
|
The category or type of the log message. |
required |
msg
|
str
|
The log message to record. |
required |
Source code in basemkit/persistent_log.py
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 | |
update_level_counts()
Updates the counts for each log level based on the existing entries.
Source code in basemkit/persistent_log.py
83 84 85 86 87 88 89 90 91 | |
LogEntry
Represents a log entry with a message, kind, and log level name.
Source code in basemkit/persistent_log.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 | |
as_html()
Return HTML representation of log entry with appropriate styling
Source code in basemkit/persistent_log.py
42 43 44 45 46 47 | |
as_text()
Return text representation of log entry with timestamp
Source code in basemkit/persistent_log.py
37 38 39 40 | |
profiler
Created on 2022-11-18
@author: wf
Profiler
simple profiler
Source code in basemkit/profiler.py
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 | |
__init__(msg, profile=True, with_start=True)
Construct the profiler with the given message and flags.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
msg
|
str
|
The message to show if profiling is active. |
required |
profile
|
bool
|
True if profiling messages should be shown. |
True
|
with_start
|
bool
|
If True, show start message immediately. |
True
|
Source code in basemkit/profiler.py
15 16 17 18 19 20 21 22 23 24 25 26 27 | |
start()
start profiling
Source code in basemkit/profiler.py
29 30 31 32 33 34 35 | |
time(extraMsg='')
time the action and print if profile is active
Source code in basemkit/profiler.py
37 38 39 40 41 42 43 44 | |
shell
Created on 2025-05-14
@author: wf
Shell
Runs commands with environment from profile
Source code in basemkit/shell.py
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 | |
__init__(profile=None, shell_path=None)
Initialize shell with optional profile
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
profile
|
str
|
Path to profile file to source e.g. ~/.zprofile |
None
|
shell_path
|
str
|
the shell_path e.g. /bin/zsh |
None
|
Source code in basemkit/shell.py
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 | |
find_profile()
Find the appropriate profile file for the current shell
Searches for the profile file corresponding to the shell_name in the user's home directory.
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
Path to the profile file or None if not found |
Source code in basemkit/shell.py
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 | |
ofArgs(args)
classmethod
Create Shell from command line args
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
args
|
Namespace
|
Arguments with optional profile |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Shell |
Shell
|
Configured Shell |
Source code in basemkit/shell.py
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 | |
proc_stats(title, procs, ignores=[])
Show process statistics with checkmark/crossmark and success/failure summary.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
title
|
str
|
A short title to label the output section. |
required |
procs
|
Dict[Path, CompletedProcess]
|
Mapping of input files to their process results. |
required |
ignores
|
List[str]
|
List of substrings. If any is found in stderr, the error is ignored. |
[]
|
Source code in basemkit/shell.py
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 | |
run(cmd, text=True, debug=False, tee=False)
Run command with profile, always capturing output and optionally teeing it.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cmd
|
str
|
Command to run |
required |
text
|
bool
|
Text mode for subprocess I/O |
True
|
debug
|
bool
|
Print the command to be run |
False
|
tee
|
bool
|
If True, also print output live while capturing |
False
|
Returns:
| Type | Description |
|---|---|
CompletedProcess
|
subprocess.CompletedProcess |
Source code in basemkit/shell.py
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 | |
ShellResult
result of a command line call
Source code in basemkit/shell.py
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | |
StdTee
Manages teeing for both stdout and stderr using StreamTee instances. Captures output in instance variables.
Source code in basemkit/shell.py
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 | |
run(process, tee=True)
classmethod
Run teeing and capture for the given process. Returns a StdTee instance with stdout/stderr captured.
Source code in basemkit/shell.py
111 112 113 114 115 116 117 118 119 120 | |
StreamTee
Tees a single input stream to both a mirror and a capture buffer.
Source code in basemkit/shell.py
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 | |
SysTee
Tee sys.stdout and sys.stderr to a logfile while preserving original output.
Source code in basemkit/shell.py
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 | |
yamlable
Created on 2023-12-08, Extended on 2023-16-12 and 2024-01-25
@author: wf, ChatGPT
Prompts for the development and extension of the 'YamlAble' class within the 'yamable' module:
- Develop 'YamlAble' class in 'yamable' module. It should convert dataclass instances to/from YAML.
- Implement methods for YAML block scalar style and exclude None values in 'YamlAble' class.
- Add functionality to remove None values from dataclass instances before YAML conversion.
- Ensure 'YamlAble' processes only dataclass instances, with error handling for non-dataclass objects.
- Extend 'YamlAble' for JSON serialization and deserialization.
- Add methods for saving/loading dataclass instances to/from YAML and JSON files in 'YamlAble'.
- Implement loading of dataclass instances from URLs for both YAML and JSON in 'YamlAble'.
- Write tests for 'YamlAble' within the pyLodStorage context. Use 'samples 2' example from pyLoDStorage https://github.com/WolfgangFahl/pyLoDStorage/blob/master/lodstorage/sample2.py as a reference.
- Ensure tests cover YAML/JSON serialization, deserialization, and file I/O operations, using the sample-based approach..
- Use Google-style docstrings, comments, and type hints in 'YamlAble' class and tests.
- Adhere to instructions and seek clarification for any uncertainties.
- Add @lod_storable annotation support that will automatically YamlAble support and add @dataclass and @dataclass_json prerequisite behavior to a class
DateConvert
date converter
Source code in basemkit/yamlable.py
81 82 83 84 85 86 87 88 89 | |
YamlAble
Bases: Generic[T]
An extended YAML handler class for converting dataclass objects to and from YAML format, and handling loading from and saving to files and URLs.
Source code in basemkit/yamlable.py
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 | |
from_dict2(data)
classmethod
Creates an instance of a dataclass from a dictionary, typically used in deserialization.
Source code in basemkit/yamlable.py
334 335 336 337 338 339 340 341 342 | |
from_yaml(yaml_str)
classmethod
Deserializes a YAML string to a dataclass instance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
yaml_str
|
str
|
A string containing YAML formatted data. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
T |
T
|
An instance of the dataclass. |
Source code in basemkit/yamlable.py
156 157 158 159 160 161 162 163 164 165 166 167 168 169 | |
load_from_json_file(filename)
classmethod
Loads a dataclass instance from a JSON file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filename
|
str
|
The path to the JSON file. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
T |
T
|
An instance of the dataclass. |
Source code in basemkit/yamlable.py
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 | |
load_from_json_url(url)
classmethod
Loads a dataclass instance from a JSON string obtained from a URL.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
url
|
str
|
The URL pointing to the JSON data. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
T |
T
|
An instance of the dataclass. |
Source code in basemkit/yamlable.py
252 253 254 255 256 257 258 259 260 261 262 263 264 265 | |
load_from_yaml_file(filename)
classmethod
Loads a dataclass instance from a YAML file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filename
|
str
|
The path to the YAML file. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
T |
T
|
An instance of the dataclass. |
Source code in basemkit/yamlable.py
186 187 188 189 190 191 192 193 194 195 196 197 198 | |
load_from_yaml_stream(stream)
classmethod
Loads a dataclass instance from a YAML stream.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
stream
|
TextIO
|
The input stream containing YAML data. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
T |
T
|
An instance of the dataclass. |
Source code in basemkit/yamlable.py
171 172 173 174 175 176 177 178 179 180 181 182 183 184 | |
load_from_yaml_url(url)
classmethod
Loads a dataclass instance from a YAML string obtained from a URL.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
url
|
str
|
The URL pointing to the YAML data. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
T |
T
|
An instance of the dataclass. |
Source code in basemkit/yamlable.py
200 201 202 203 204 205 206 207 208 209 210 211 212 213 | |
read_from_url(url)
classmethod
Helper method to fetch content from a URL.
Source code in basemkit/yamlable.py
279 280 281 282 283 284 285 286 287 288 | |
remove_ignored_values(value, ignore_none=True, ignore_underscore=False, ignore_empty=True)
classmethod
Recursively removes specified types of values from a dictionary or list. By default, it removes keys with None values. Optionally, it can also remove keys starting with an underscore.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
Any
|
The value to process (dictionary, list, or other). |
required |
ignore_none
|
bool
|
Flag to indicate whether None values should be removed. |
True
|
ignore_underscore
|
bool
|
Flag to indicate whether keys starting with an underscore should be removed. |
False
|
ignore_empty
|
bool
|
Flag to indicate whether empty collections should be removed. |
True
|
Source code in basemkit/yamlable.py
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 | |
represent_literal(dumper, data)
Custom representer for block scalar style for strings.
Source code in basemkit/yamlable.py
116 117 118 119 120 121 122 | |
represent_none(_, __)
Custom representer for ignoring None values in the YAML output.
Source code in basemkit/yamlable.py
110 111 112 113 114 | |
save_to_json_file(filename, **kwargs)
Saves the current dataclass instance to a JSON file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filename
|
str
|
The path where the JSON file will be saved. |
required |
**kwargs
|
Any
|
Additional keyword arguments for the |
{}
|
Source code in basemkit/yamlable.py
267 268 269 270 271 272 273 274 275 276 277 | |
save_to_yaml_file(filename)
Saves the current dataclass instance to a YAML file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filename
|
str
|
The path where the YAML file will be saved. |
required |
Source code in basemkit/yamlable.py
225 226 227 228 229 230 231 232 233 234 | |
save_to_yaml_stream(file)
Saves the current dataclass instance to the given YAML stream.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file
|
TextIO
|
The stream to which YAML content will be saved. |
required |
Source code in basemkit/yamlable.py
215 216 217 218 219 220 221 222 223 | |
to_yaml(ignore_none=True, ignore_underscore=True, allow_unicode=True, sort_keys=False)
Converts this dataclass object to a YAML string, with options to omit None values and/or underscore-prefixed variables, and using block scalar style for strings.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ignore_none
|
bool
|
Flag to indicate whether None values should be removed from the YAML output. |
True
|
ignore_underscore
|
bool
|
Flag to indicate whether attributes starting with an underscore should be excluded from the YAML output. |
True
|
allow_unicode
|
bool
|
Flag to indicate whether to allow unicode characters in the output. |
True
|
sort_keys
|
bool
|
Flag to indicate whether to sort the dictionary keys in the output. |
False
|
Returns:
| Type | Description |
|---|---|
str
|
A string representation of the dataclass object in YAML format. |
Source code in basemkit/yamlable.py
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 | |
lod_storable(cls)
Decorator to make a class LoDStorable by inheriting from YamlAble. This decorator also ensures the class is a dataclass and has JSON serialization/deserialization capabilities.
Source code in basemkit/yamlable.py
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 | |