Structured Values of Instance Nodes

The instvalue module implements the following classes:

  • StructuredValue: Abstract class for “cooked” structured values of an instance node.
  • ArrayValue: Cooked array value of an instance node.
  • ObjectValue: Cooked object value of an instance node.

The standard Python library function json.load() parses JSON arrays and objects into native data structures – lists and dictionaries, respectively. In order to use them effectively in the Yangson library, we need to “cook” them first, i.e. extend these data structures with additional attributs and methods:

  • In order to be able to generate entity tags for HTTP ETag headers, we need to be able to compute a hash value for arrays and objects. Standard Python lists and dictionaries do not implement the __hash__() method.
  • For each array and object, we also need to record the time stamp of its last modification (to be used in HTTP Last-Modified headers).

Type Aliases

yangson.instvalue.Value

This type alias covers all possible types of cooked values of an instance node, both scalar and structured.

yangson.instvalue.EntryValue

This type alias covers possible types of values of a list of leaf-list entry.

class yangson.instvalue.StructuredValue(ts: datetime.datetime = None)

This class is an abstract superclass for structured values of instance nodes. The constructor argument ts contains the initial value of the timestamp attribute. If it is None, then current time is used.

Instance Attributes

timestamp

This attribute contains a datetime.datetime that records the date and time of the last modification.

Public Methods

copy() → StructuredValue

Return a shallow copy of the receiver with last_modified set to current time.

__setitem__(self, key: InstanceKey, value: Value) → None

Set an array entry or object member key to value and update receiver’s timestamp to the current time.

__eq__(val: StructuredValue) → bool

Return True if the receiver is equal to val. The equality test is based on their hash values.

__hash__() → int

Return hash value for the receiver.

Caution

The hash values are guaranteed to be stable only within the same Python interpreter process. This is because hash values of Python strings change from one invocation to another.

class yangson.instvalue.ArrayValue(val: List[EntryValue] = [], ts: datetime.datetime = None)

Bases: yangson.instvalue.StructuredValue, list

This class represents cooked array values.

The additional constructor argument val contains a list that the ArrayValue instance will hold.

>>> ary = ArrayValue([1, 2, 3])
>>> time.sleep(0.1)
>>> ac = ary.copy()
>>> ary.timestamp < ac.timestamp
True
>>> ary == ac
True
>>> ac[2] = 4
>>> ary == ac
False
class yangson.instvalue.ObjectValue(val: Dict[InstanceName, Value] = {}, ts: datetime.datetime = None)

Bases: yangson.instvalue.StructuredValue, dict

This class represents cooked object values.

The additional constructor argument val contains a dictionary that the ObjectValue instance will hold.

>>> obj = ObjectValue({'one': 1, 'two': 2})
>>> time.sleep(0.1)
>>> oc = obj.copy()
>>> obj.timestamp < oc.timestamp
True
>>> obj == oc
True
>>> oc['three'] = 3
>>> obj == oc
False