| Conditions | 10 |
| Total Lines | 51 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 3 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like _instantiate_node() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
| 1 | """ |
||
| 75 | def _instantiate_node(server, parentid, rdesc, nodeid, bname, recursive=True): |
||
| 76 | """ |
||
| 77 | instantiate a node type under parent |
||
| 78 | """ |
||
| 79 | |||
| 80 | addnode = ua.AddNodesItem() |
||
| 81 | addnode.RequestedNewNodeId = nodeid |
||
| 82 | addnode.BrowseName = bname |
||
| 83 | addnode.ParentNodeId = parentid |
||
| 84 | addnode.ReferenceTypeId = rdesc.ReferenceTypeId |
||
| 85 | addnode.TypeDefinition = rdesc.TypeDefinition |
||
| 86 | |||
| 87 | node_type = Node(server, rdesc.NodeId) |
||
| 88 | |||
| 89 | refs = node_type.get_referenced_nodes(refs=ua.ObjectIds.HasModellingRule) |
||
| 90 | # skip optional elements |
||
| 91 | if not(len(refs) == 1 and refs[0].nodeid == ua.NodeId(ua.ObjectIds.ModellingRule_Optional) ): |
||
| 92 | |||
| 93 | if rdesc.NodeClass in (ua.NodeClass.Object, ua.NodeClass.ObjectType): |
||
| 94 | addnode.NodeClass = ua.NodeClass.Object |
||
| 95 | _read_and_copy_attrs(node_type, ua.ObjectAttributes(), addnode) |
||
| 96 | |||
| 97 | elif rdesc.NodeClass in (ua.NodeClass.Variable, ua.NodeClass.VariableType): |
||
| 98 | addnode.NodeClass = ua.NodeClass.Variable |
||
| 99 | _read_and_copy_attrs(node_type, ua.VariableAttributes(), addnode) |
||
| 100 | elif rdesc.NodeClass in (ua.NodeClass.Method,): |
||
| 101 | addnode.NodeClass = ua.NodeClass.Method |
||
| 102 | _read_and_copy_attrs(node_type, ua.MethodAttributes(), addnode) |
||
| 103 | else: |
||
| 104 | print("Instantiate: Node class not supported: ", rdesc.NodeClass) |
||
| 105 | return |
||
| 106 | |||
| 107 | res = server.add_nodes([addnode])[0] |
||
| 108 | |||
| 109 | if recursive: |
||
| 110 | parents = get_sub_types( server,node_type, includeitself = True) |
||
| 111 | for parent in parents: |
||
| 112 | descs = parent.get_children_descriptions(includesubtypes=False) |
||
| 113 | for c_rdesc in descs: |
||
| 114 | #TODO: smells, is there a better way to test if a browse name is already present ? |
||
| 115 | # skip items that already exists, prefer the 'lowest' one in object hierarchy |
||
| 116 | node = Node(server, res.AddedNodeId) |
||
| 117 | try: |
||
| 118 | node.get_child(c_rdesc.BrowseName) |
||
| 119 | except UaError as e: |
||
| 120 | _instantiate_node(server, res.AddedNodeId, c_rdesc, nodeid=ua.NodeId(namespaceidx=res.AddedNodeId.NamespaceIndex), bname=c_rdesc.BrowseName) |
||
| 121 | |||
| 122 | return Node(server, res.AddedNodeId) |
||
| 123 | |||
| 124 | else: |
||
| 125 | return None |
||
| 126 | |||
| 142 |