Model Base Classes
The model base classes provide serialization (JSON, JSON.GZ, msgpack, YAML),
equality checking, and (for settable models) the ability to generate
and apply Tao set commands.
TaoBaseModel
Common base for all PyTao Pydantic models. Provides write() and from_file()
for serialization to JSON, compressed JSON (.json.gz), msgpack, and YAML.
pytao.model.TaoBaseModel
Bases: BaseModel
A helper base class which allows for creating/updating an instance with Tao objects.
Functions
pytao.model.TaoBaseModel.from_file
classmethod
from_file(filename, *, format=None)
Load Tao model data from a previously-written file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filename
|
str or Path
|
|
required |
Returns:
| Type | Description |
|---|---|
TaoModel
|
|
Source code in pytao/model/base.py
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 | |
pytao.model.TaoBaseModel.to_dict
to_dict(include_metadata=False, **kwargs)
Serialize the model to a dictionary.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
include_metadata
|
bool
|
If True, includes base metadata fields like |
False
|
**kwargs
|
Additional arguments to pass to Pydantic's |
{}
|
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
|
Source code in pytao/model/base.py
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 | |
pytao.model.TaoBaseModel.write
write(filename, *, exclude_defaults=True, backup_existing=True, datefmt=DEFAULT_DATEFMT, format=None, indent=False, sort_keys=False)
Write the model data to a file in JSON or YAML format.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filename
|
str or Path
|
The path to the file where the model data should be written. The file format is determined by the extension. |
required |
exclude_defaults
|
bool
|
Exclude model defaults from the output. Defaults to True. |
True
|
backup_existing
|
bool
|
If a file exists at the target, rename it using |
True
|
datefmt
|
str
|
The date format for the backup file. |
DEFAULT_DATEFMT
|
format
|
ArchiveFormat or None
|
File format. If not specified, determined by file extension. |
None
|
indent
|
bool
|
Indent the output, if applicable based on the file format. |
False
|
sort_keys
|
bool
|
Sort the output keys, if applicable based on the file format. |
False
|
Source code in pytao/model/base.py
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 | |
TaoModel
Read-only model populated by querying a Tao instance. Each subclass defines a
_tao_command_attr_ that maps to a specific Tao pipe command.
pytao.model.TaoModel
Bases: TaoBaseModel
A helper base class which allows for creating/updating an instance with Tao objects.
Functions
pytao.model.TaoModel.from_tao
classmethod
from_tao(tao, **kwargs)
Create this structure by querying Tao for its current values.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tao
|
Tao
|
|
required |
**kwargs
|
Keyword arguments to pass to the relevant |
{}
|
Source code in pytao/model/base.py
270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 | |
pytao.model.TaoModel.query
query(tao)
Query Tao again to generate a new instance of this model.
Source code in pytao/model/base.py
262 263 264 | |
TaoSettableModel
Extends TaoModel with the ability to generate set commands and apply changes
back to Tao. Supports differential updates (only changed fields), context-manager
usage, and error-tolerant application.
pytao.model.TaoSettableModel
Bases: TaoModel
A helper base class which allows for setting Tao parameters based on instance attributes.
Attributes
pytao.model.TaoSettableModel.set_commands
property
set_commands
Get all Tao 'set' commands to apply this configuration.
Returns:
| Type | Description |
|---|---|
list of str
|
|
pytao.model.TaoSettableModel.settable_fields
property
settable_fields
Names of all 'settable' (modifiable) fields.
Functions
pytao.model.TaoSettableModel.get_set_commands
get_set_commands(tao=None)
Generate a list of set commands to apply this configuration to tao.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tao
|
Tao or None
|
An instance of the Tao class.
If provided, only differing
configuration parameters will be included in the list of set
commands.
If |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
cmds |
list of str
|
|
Source code in pytao/model/base.py
389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 | |
pytao.model.TaoSettableModel.set
set(tao, *, allow_errors=False, only_changed=False, suppress_plotting=True, suppress_lattice_calc=True, log='DEBUG', exclude_matches=None)
Apply this configuration to Tao.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tao
|
Tao
|
The Tao instance to which the configuration will be applied. |
required |
allow_errors
|
bool
|
Allow individual commands to raise errors. |
False
|
only_changed
|
bool
|
Only apply changes that differ from the current configuration in Tao. |
False
|
suppress_plotting
|
bool
|
Suppress any plotting updates during the commands. |
True
|
suppress_lattice_calc
|
bool
|
Suppress lattice calculations during the commands. |
True
|
log
|
str
|
The log level to use during the configuration application. |
"DEBUG"
|
Returns:
| Name | Type | Description |
|---|---|---|
success |
bool
|
Returns True if the configuration was applied without errors. |
Source code in pytao/model/base.py
437 438 439 440 441 442 443 444 445 446 447 448 449 450 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 | |
pytao.model.TaoSettableModel.set_context
set_context(tao)
Apply this configuration to Tao only for the given with block.
Examples:
Set an initial value for a parameter:
>>> new_state.param = 1
>>> new_state.set()
Then temporarily set it to another value, just for the with block:
>>> new_state.param = 3
>>> with new_state.set_context(tao):
... assert new_state.query(tao).param == 3
After the with block, param will be reset to its previous
value:
>>> assert new_state.query(tao).param == 1
Source code in pytao/model/base.py
514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 | |
Serialization Helpers
pytao.model.load_model_data
load_model_data(filename, *, format=None)
Read the raw model data from a file in JSON, YAML, or msgpack format.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filename
|
str or Path
|
The path to the file where the model data should be written. The file format is determined by the extension. |
required |
format
|
ArchiveFormat or None
|
File format. If not specified, determined by file extension. |
None
|
Source code in pytao/model/base.py
561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 | |
pytao.model.load_model
load_model(filename, cls, *, format=None)
Read the model from a file in JSON, YAML, or msgpack format.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filename
|
str or Path
|
The path to the file where the model data should be written. The file format is determined by the extension. |
required |
cls
|
pydantic.BaseModel class
|
|
required |
format
|
ArchiveFormat or None
|
File format. If not specified, determined by file extension. |
None
|
Source code in pytao/model/base.py
599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 | |
pytao.model.dump_model
dump_model(filename, model, *, exclude_defaults=True, backup_existing=True, datefmt=DEFAULT_DATEFMT, format=None, indent=False, sort_keys=False)
Write the model data to a file in JSON, YAML, or msgpack format.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filename
|
str or Path
|
The path to the file where the model data should be written. The file format is determined by the extension. |
required |
model
|
BaseModel
|
|
required |
exclude_defaults
|
bool
|
Exclude model defaults from the output. Defaults to True. |
True
|
backup_existing
|
bool
|
If a file exists at the target, rename it using |
True
|
datefmt
|
str
|
The date format for the backup file. |
DEFAULT_DATEFMT
|
format
|
ArchiveFormat or None
|
File format. If not specified, determined by file extension. |
None
|
indent
|
bool
|
Indent the output, if applicable based on the file format. |
False
|
sort_keys
|
bool
|
Sort the output keys, if applicable based on the file format. |
False
|
Source code in pytao/model/base.py
640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 | |