Node#

class Node(root: NodeTree, tree: tuple)[source]#

Bases: object

Lazy node of a Nodetree.

The node is implemented in a lazy way. Meaning unless operations are not performed on the node, no checks whether the node is valid or not are performed.

The child nodes of each node can be accessed in the same way than on the Nodetree, either by attribute or by item.

The core functionality of each node is the overloaded call operator. Making a call gets the value(s) for that node. Passing a value to the call operator will set that value to the node on the device. Calling a node that is not a leaf (wildcard or partial node) will return/set the value on every node that matches it (the return type will be a dictionary).

Warning

Setting a value to a non-leaf node will try to set the value of all nodes that matches that node. It should therefor be used with great care to avoid unintentional changes.

>>> nodetree.demods[0].freq()
1000
>>> nodetree.demods[0].freq(2000)
>>> nodetree.demods[0].freq()
2000
>>> nodetree.demods["*"].freq(3000)
>>> nodetree.demods["*"].freq()
{
    '/dev1234/demods/0/freq': 3000
    '/dev1234/demods/1/freq': 3000
    '/dev1234/demods/2/freq': 3000
    '/dev1234/demods/3/freq': 3000
}

Changed in version 0.3.5: Call operator returns WildcardResult when wildcards are used in getting values.

Changed in version 0.5.0: Returns NodeDict instead of WildcardResult

The call operator supports the following flags:

  • deep: Flag if the set operation should be blocking until the data

    has arrived at the device, respectively if the get operation should return the value from the device or the cached value on the data server (if there is any). If this flag is set the operation can take significantly longer. (default = False)

    For a deep get operation the timestamp from the device is returned in addition to the value (The timestamp can be None, e.g. deep gets on LabOne modules).

    >>> nodetree.demods[0].freq(deep=True)
    (343283971893, 2000)
    

    For a deep set the call operator will return the value acknowledged by the device. e.g. important for floating point values with a limited resolution.

    >>> nodetree.demods[0].freq(29999,99999, deep=True)
    3000
    

Warning

The deep flag does not work for wildcard nodes or non leaf nodes since they represent multiple nodes that are set in a transactional set which does not report the acknowledged values.

  • enum: Flag if enumerated values should return the enum value as

    string. (default = True)

  • parse: Flag if the SetParser/GetParser from the Node, if present,

    should be applied or not (default = True).

    The parsers are hard coded lambda functions provided not directly by LabOne but need to be set manually (e.g. toolkit adds these for a selected set of nodes). The Lambda function gets called right before/after the API call to LabOne. Usually they are used to add additional limitations and improve error reporting but in theory they can be used for anything.

    To add a parser to a node use the NodeTree.update_node function.

    >>> nodetree.update_node(
            "/dev1234/demods/0/freq",
            {
                "GetParser": lambda v: print(f"got {v} from LabOne") return v,
                "SetParser": lambda v: print(f"set {v} to LabOne") return v,
            },
        )
    

In addition to the call operator the following magic methods are implemented:

  • __contains__
    >>> "freq" in nodetree.demods[0]
    True
    
  • __iter__
    >>> for node, info in nodetree.demods["*"].freq
    
  • __eq__
    >>> nodetree.demods[0].freq == nodetree.demods["*/freq"]
    
  • __len__ (only implemented for list like nodes)
    >>> len(nodetree.demods)
    4
    
  • __bool__ test if the node is a existing node
    >>> if nodetree.demods[0].freq:
        ...
    
  • __hash__ (e.g. necessary to be able to use nodes as key in dictionaries)

Parameters:
  • root (NodeTree) – Root of the nodetree.

  • tree (tuple) – Tree (node path as tuple) of the current node.

Methods

child_nodes(*[, recursive, leavesonly, ...])

Generator for all child nodes that matches the filters.

get_as_event()

Trigger an event for that node (its child lead nodes).

is_child_node(child_node)

Checks if a node is child node of this node.

is_valid()

Check if the node is a valid node.

subscribe()

Subscribe to this node (its child lead nodes).

unsubscribe()

Unsubscribe this node (its child lead nodes).

wait_for_state_change(value, *[, invert, ...])

Waits until the node has the expected state/value.

Attributes

node_info

Additional information about the node.

raw_tree

Internal representation of the node.

root

Node tree to which this node belongs to.