Toolbox¶
| MODULE | DESCRIPTION |
|---|---|
p |
|
| CLASS | DESCRIPTION |
|---|---|
Progressor |
A more configurable Progressor class that supports nesting. |
Tool |
|
| FUNCTION | DESCRIPTION |
|---|---|
progressor |
Initialize a progress bar for an iterable |
safe_load |
Safely load in tools to a toolbox placing all failed imports in a |
toolify |
Convert a typed function into a tool for the specified Toolbox class |
Progressor
¶
Progressor(
it: Iterable[T],
message: str = '...',
*,
mode: ProgressorMode
| Iterable[ProgressorMode] = 'auto',
lazy: bool = False,
length_hint: int | None = None,
bar_width: int = 10,
)
A more configurable Progressor class that supports nesting.
Note
To allow for progress position, the input iterable is consumed into a list. If this is not what you want, it's best to handroll a progressor flow.
Example
import time
a = Progressor(range(10), 'A')
b = Progressor(range(10), 'B')
c = Progressor(range(10), 'C')
for i in a:
time.sleep(0.1)
for j in b:
time.sleep(0.1)
for k in c:
time.sleep(0.1)
Each loop will initialize a new progressor and maintain its position.
| PARAMETER | DESCRIPTION |
|---|---|
|
Iterable to create a Progressor for.
TYPE:
|
|
The message to display in the progressor message box. (default:
TYPE:
|
|
Display a count/percent in the progressor message box
TYPE:
|
|
Don't immediately consume the iterable and instead use
TYPE:
|
|
If the iterable has not
TYPE:
|
Note
If you set lazy or length_hint, the progressor will lazily iterate the iterable.
Excluding length_hint when lazy is set with an iterable with no __length_hint__
attribute will cause the progress bar to set the max position to 1.
Alternatively, setting length_hint and not setting lazy will automatically make the progressor lazy.
-
Modules
Toolbox
Progressorroot
| METHOD | DESCRIPTION |
|---|---|
global_position |
Get the position of the innermost (most recent) progressor on the stack |
reset_progressor |
Alias for ResetProgressor |
root |
Get the outermost progressor (this one drives the window progress bar) |
set_label |
Alias for SetProgressorLabel |
set_position |
Alias for SetProgressorPosition |
set_progressor |
Alias for SetProgressor |
| ATTRIBUTE | DESCRIPTION |
|---|---|
position |
Get the position of this progressor
TYPE:
|
Source code in src/arcpie/toolbox.py
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 | |
global_position
classmethod
¶
global_position() -> int
Get the position of the innermost (most recent) progressor on the stack
Source code in src/arcpie/toolbox.py
230 231 232 233 234 235 | |
reset_progressor
classmethod
¶
reset_progressor() -> None
Alias for ResetProgressor
Source code in src/arcpie/toolbox.py
251 252 253 254 | |
root
classmethod
¶
root() -> Progressor[Any]
Get the outermost progressor (this one drives the window progress bar)
Source code in src/arcpie/toolbox.py
241 242 243 244 | |
set_label
classmethod
¶
set_label(label: str) -> None
Alias for SetProgressorLabel
Source code in src/arcpie/toolbox.py
261 262 263 264 | |
set_position
classmethod
¶
set_position(pos: int) -> None
Alias for SetProgressorPosition
Source code in src/arcpie/toolbox.py
256 257 258 259 | |
set_progressor
classmethod
¶
Alias for SetProgressor
Source code in src/arcpie/toolbox.py
246 247 248 249 | |
Tool
¶
Tool()
Bases: ToolABC
| ATTRIBUTE | DESCRIPTION |
|---|---|
project |
Get the current project that the tool is running in if it exists (otherwise: None)
TYPE:
|
Source code in src/arcpie/toolbox.py
45 46 47 48 | |
progressor
¶
Initialize a progress bar for an iterable
| PARAMETER | DESCRIPTION |
|---|---|
|
The iterable to initialize a progressor for
TYPE:
|
|
The message to show in the progress bar label area
TYPE:
|
Example
for parcel in progressor(parcels, 'Gathering'):
...
Note
The iterable is consumed on progressor initialization to allow a size check.
Each iteration of the progressor will update the label area with a count in the
format: {msg}: [{idx}/{total}]
Source code in src/arcpie/toolbox.py
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 | |
safe_load
¶
safe_load(
tools: dict[str, list[str]],
*,
scope: dict[str, Any] | None = None,
reload_module: bool = False,
) -> list[type[ToolABC]]
Safely load in tools to a toolbox placing all failed imports in a Broken Tools category
| PARAMETER | DESCRIPTION |
|---|---|
|
A mapping of tool modules to tool files or tool classes in a module |
|
The |
|
Reload the module after importing (default: False)
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
list[type[ToolABC]]
|
A list of tool classes |
Note
To isolate bugs in a large toolbox, it is reccomended that you use file/module level importing
with matching toolclass names (e.g. Tool.py -> class Tool) where the Tool mapping is:
Tools = {'tools': ['Tool.py']}
instead of:
Tools = {'tools.Tool': ['Tool']}
This allows the import to fail softly on a single tool instead of breaking imports for all other
toolclasses in that Tool.py module.
Any tools with errors will be placed in a Broken Tools category in the toolbox with the full
stack trace placed in its description field. The final component of the exception will be placed
at the end of the tool label for easy debugging.
Example
>>> # File import
>>> Tools = {
... # import matching class from named file in a submodule (tools)
... # where ToolFileA is ToolFileA.py with a class ToolFileA
... 'tools': ['ToolFileA', 'ToolFileB'],
... # Import explicit ToolClass from toolfile (MyTools.py)
... # NOT RECOMMENDED
... 'tools.MyTools': ['ToolClassA', 'ToolClassB'],
... }
>>> tools = safe_import(globals(), Tools)
Source code in src/arcpie/toolbox.py
354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 | |
toolify
¶
toolify(
*tool_registries: list[type[ToolABC]],
name: str | None = None,
params: ParameterTypeMap | None = None,
debug: bool = False,
logger: Logger | None = None,
)
Convert a typed function into a tool for the specified Toolbox class
| PARAMETER | DESCRIPTION |
|---|---|
|
The tool registry lists to add this tool to |
|
The name of the tool
TYPE:
|
|
A mapping of parameter names to Parameter types and a callable constructor that converts the parameter to the expected value for the function parameter. You can also pass a fully formed arcpy.Parameter object as the first item in the tuple instead of a simple type
TYPE:
|
|
Print the converted arguments to the ArcGIS Pro message console (default: False)
TYPE:
|
|
An optional logger to use for logging all runs of the toolified tool
TYPE:
|
Usage
>>> @toolify(
>>> TOOL_REGISTRY,
>>> name='PDF Exporter',
>>> params={
>>> 'project': ('DEFile', lambda p: Project(p.valueAsText)),
>>> 'outfile': ('DEFile', lambda p: Path(p.valueAsText))
>>> }
>>> )
>>> def export_pdf(project: Project|str='CURRENT', outfile: Path|str='out.pdf') -> None:
>>> ...
Source code in src/arcpie/toolbox.py
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 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 | |