pyATS Datastructures
pyATS Datastructures provides extended data structures designed to enhance network automation and testing workflows. It offers specialized objects like `DataObject`, `DataList`, `DataDict`, and `MetaData` that allow for attribute-based access, schema validation, and meta-data handling, often used within the Cisco pyATS framework for managing configuration and state data. The current version is 26.3, and it follows the pyATS release cadence, typically releasing new versions quarterly or as needed with the main pyATS framework.
Common errors
-
AttributeError: 'DataObject' object has no attribute 'keys'
cause Attempting to use standard dictionary methods like `keys()`, `items()`, or `values()` directly on a `DataObject` instance, which is not a `dict` subclass.fixIf you need a dictionary representation to iterate over its keys or items, convert the `DataObject` to a native Python dictionary first: `data.to_dict().keys()` or `data.to_dict().items()`. -
ModuleNotFoundError: No module named 'pyats.datastructure'
cause Typo in the package name during import. The correct package name is `pyats.datastructures` (plural 's').fixCorrect the import statement to use the plural form: `from pyats.datastructures import DataObject`.
Warnings
- gotcha While `DataObject` provides dictionary-like and attribute-like access, it does not inherit directly from `dict` or `list`. Direct use of standard dictionary/list methods like `.keys()`, `.items()`, or `.append()` on a `DataObject` instance itself will result in an `AttributeError` (unless explicitly implemented or if it contains a `DataDict`/`DataList` instance).
- gotcha When assigning a plain Python `dict` or `list` to an attribute of a `DataObject`, `pyats-datastructures` will implicitly convert them into `DataDict` or `DataList` instances. While this offers convenience, it means subsequent operations on these nested structures will follow `DataDict`/`DataList` rules, not standard `dict`/`list` behavior.
Install
-
pip install pyats-datastructures
Imports
- DataObject
from pyats.datastructures import DataObject
- DataList
from pyats.datastructures import DataList
- DataDict
from pyats.datastructures import DataDict
- And
from pyats.datastructures.logic import And
Quickstart
from pyats.datastructures import DataObject
# Create a DataObject
data = DataObject()
# Assign attributes like a regular object
data.attr = 'value'
data.attr_2 = 123
data.nested = DataObject()
data.nested.item = 'nested_value'
# Access attributes
print(f"data.attr: {data.attr}")
print(f"data.nested.item: {data.nested.item}")
# Convert to a standard Python dictionary
plain_dict = data.to_dict()
print(f"Converted to dict: {plain_dict}")
assert plain_dict == {'attr': 'value', 'attr_2': 123, 'nested': {'item': 'nested_value'}}