Instance Routes

The instroute module implements the following classes:

Doctest snippets for this module use the data model and instance document from Example 2.

>>> dm = DataModel.from_file('yang-library-ex2.json',
... [".", "../../../yang-modules/ietf"])
>>> with open('example-data.json') as infile:
...   ri = json.load(infile)
>>> inst = dm.from_raw(ri)
class yangson.instroute.InstanceRoute(iterable=(), /)

Bases: tuple[InstanceRouteItem, …]

This class represents a route into an instance value.

Instances of this class can be conveniently created by using one of the methods parse_resource_id() and parse_instance_id() in the DataModel class.

Instances of this class are also used as the cooked value of the instance-identifier type.

>>> irt = dm.parse_resource_id('/example-2:bag/foo=3/in-words')
>>> len(irt)
4

Public Methods

__str__() str

Return instance-id as the string representation of the receiver.

>>> str(irt)
'/example-2:bag/foo[number="3"]/in-words'
__hash__() int

Return the hash value of the receiver.

class yangson.instroute.InstanceRouteItem(*args, **kwargs)

Bases: Protocol

This protocol class defines a required API for instance route items.

Module instance defines several private classes (MemberName, ActionName, EntryIndex, EntryValue and EntryKeys) conforming to the protocol that implement constituents of instance routes – selectors of instance nodes.

>>> irt[0].__class__
<class 'yangson.instance.MemberName'>
>>> irt[2].__class__
<class 'yangson.instance.EntryKeys'>

Public Methods

__eq__(other: object) bool

Return True if the receiver is equal to other.

__str__() str

Return string representation of the receiver (i-i segment).

>>> str(irt[0])
'/example-2:bag'
>>> str(irt[2])
'[number="3"]'
goto_step(inst: InstanceNode) InstanceNode:

Return the child of inst (an InstanceNode) selected by the receiver.

>>> irt[0].goto_step(inst).json_pointer()
'/example-2:bag'
>>> irt[2].goto_step(inst['example-2:bag']['foo']).json_pointer()
'/example-2:bag/foo/1'
peek_step(val: StructuredValue, sn: DataNode) tuple(Value | None, DataNode:

Return a tuple consisting of:

  • the value selected by the receiver from val

  • schema node corresponding to that value.

Unlike goto_step(), this method just peeks into instance values and doesn’t create a new instance node. In order to be able to apply a sequence of such peek steps prescribed by an InstanceRoute, schema nodes have to be tracked separately.

>>> irt[0].peek_step(inst.value, inst.schema_node)[0]['bar']
True
>>> irt[0].peek_step(inst.value, inst.schema_node)[1].__class__
<class 'yangson.schemanode.ContainerNode'>