Operations
Duqtools uses delayed operations for filesystem-changing operations.
They are implemented mostly with decorators, but a function could be added directly to the op_queue
.
Operation(action, description, extra_description=None, *args, **kwargs)
Bases: BaseModel
Operation, simple class which has a callable action.
Usually not called directly but used through Operations. has the following members:
Parameters:
-
description
–
description of the operation to be done
-
extra_description
(
Optional[str]
) –extra description, (not colorized)
-
action
–
function to be eventually evaluated
-
*args
–
positional arguments to action
-
**kwargs
–
keyword arguments to action
Source code in duqtools/operations.py
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 |
|
__call__()
Execute the action with the args and kwargs.
Returns:
-
Operation
–The operation that was executed
Source code in duqtools/operations.py
69 70 71 72 73 74 75 76 77 78 79 |
|
Operations()
Bases: deque
Operations Queue which keeps track of all the operations that need to be done.
It's basically dask_delayed, but custom made and a few drawbacks: The return value from an action is eventually discarded, communication between queue items is possible through references, or global values, but not really recommended, and no guidance for this is provided
Source code in duqtools/operations.py
99 100 101 102 103 |
|
add(*args, **kwargs)
convenience Operation wrapper around put.
from duqtools.operations import add_to_op_queue.
op_queue.add(print, args=('Hello World,),
description="Function that prints hello world")
Source code in duqtools/operations.py
111 112 113 114 115 116 117 118 119 120 121 122 |
|
append(item)
Restrict our diet to Operation objects only.
Source code in duqtools/operations.py
128 129 130 131 132 133 134 135 136 |
|
apply()
Apply the next operation in the queue and remove it.
Source code in duqtools/operations.py
138 139 140 141 142 143 |
|
apply_all()
Apply all queued operations and empty the queue.
and show a fancy progress bar while applying
Source code in duqtools/operations.py
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
|
check_unconfirmed_operations()
Safety check, it should never happen that operations are not executed.
, check it anyway at program exit
Source code in duqtools/operations.py
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 |
|
confirm_apply_all()
First asks the user if he wants to apply everything.
Returns:
-
bool(
did we apply everything or not
) –
Source code in duqtools/operations.py
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 |
|
put(item)
synonym for append.
Source code in duqtools/operations.py
124 125 126 |
|
add_to_op_queue(op_desc, extra_desc=None)
Decorator which adds the function call to the op_queue, instead of executing it directly, the string can be a format string and use the function arguments.
from duqtools.operations import add_to_op_queue, op_queue
@add_to_op_queue("Printing hello world", "{name}")
def print_hello_world(name):
print(f"Hello World {name}")
print_hello_world("Snoozy")
op_queue.confirm_apply_all()
Source code in duqtools/operations.py
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 |
|
confirm_operations(func)
Decorator which confirms and applies queued operations after the function.
from duqtools.operations import confirm_operations, op_queue
@confirm_operations
def complicated_stuff()
op_queue.add(print, args=('Hello World,),
description="Function that prints hello world")
op_queue.add(print, args=('Hello World again,),
description="Function that prints hello world, again")
Source code in duqtools/operations.py
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 |
|
op_queue_context()
Context manager to enable the op_queue, and confirm_operations on exit Also disables the op_queue on exit.
Works more or less the same as the @confirm_operations
decorator
Source code in duqtools/operations.py
288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 |
|