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
ETagheaders, 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-Modifiedheaders).
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.datetimethat records the date and time of the last modification.
Public Methods
- copy() StructuredValue
Return a shallow copy of the receiver with
last_modifiedset 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
Trueif the receiver is equal to val. The equality test is based on their hash values.
- class yangson.instvalue.ArrayValue(val: List[EntryValue] = [], ts: datetime.datetime = None)
Bases:
StructuredValue,listThis class represents cooked array values.
The additional constructor argument val contains a list that the
ArrayValueinstance 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:
StructuredValue,dictThis class represents cooked object values.
The additional constructor argument val contains a dictionary that the
ObjectValueinstance 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