| Conditions | 16 |
| Total Lines | 63 |
| Code Lines | 38 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 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 asyncua.common.structures104.make_structure_code() 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 | from enum import Enum |
||
| 51 | def make_structure_code(name, sdef): |
||
| 52 | """ |
||
| 53 | given a StructureDefinition object, generate Python code |
||
| 54 | """ |
||
| 55 | if sdef.StructureType not in (ua.StructureType.Structure, ua.StructureType.StructureWithOptionalFields): |
||
| 56 | #if sdef.StructureType != ua.StructureType.Structure: |
||
| 57 | raise NotImplementedError(f"Only StructureType implemented, not {ua.StructureType(sdef.StructureType).name} for node {name} with DataTypdeDefinition {sdef}") |
||
| 58 | |||
| 59 | code = f""" |
||
| 60 | |||
| 61 | class {name}: |
||
| 62 | |||
| 63 | ''' |
||
| 64 | {name} structure autogenerated from StructureDefinition object |
||
| 65 | ''' |
||
| 66 | |||
| 67 | """ |
||
| 68 | counter = 0 |
||
| 69 | if sdef.StructureType == ua.StructureType.StructureWithOptionalFields: |
||
| 70 | code += ' ua_switches = {\n' |
||
| 71 | for field in sdef.Fields: |
||
| 72 | |||
| 73 | if field.IsOptional: |
||
| 74 | code += f" '{field.Name}': ('Encoding', {counter}),\n" |
||
| 75 | counter += 1 |
||
| 76 | code += " }\n\n" |
||
| 77 | |||
| 78 | code += ' ua_types = [\n' |
||
| 79 | uatypes = [] |
||
| 80 | for field in sdef.Fields: |
||
| 81 | prefix = 'ListOf' if field.ValueRank >= 1 else '' |
||
| 82 | if field.DataType.NamespaceIndex == 0 and field.DataType.Identifier in ua.ObjectIdNames: |
||
| 83 | uatype = ua.ObjectIdNames[field.DataType.Identifier] |
||
| 84 | elif field.DataType in ua.extension_objects_by_datatype: |
||
| 85 | uatype = ua.extension_objects_by_datatype[field.DataType].__name__ |
||
| 86 | elif field.DataType in ua.enums_by_datatype: |
||
| 87 | uatype = ua.enums_by_datatype[field.DataType].__name__ |
||
| 88 | else: |
||
| 89 | raise RuntimeError(f"Unknown datatype for field: {field} in structure:{name}") |
||
| 90 | if field.ValueRank >= 1 and uatype == 'Char': |
||
| 91 | uatype = 'String' |
||
| 92 | uatypes.append((field, uatype)) |
||
| 93 | code += f" ('{field.Name}', '{prefix + uatype}'),\n" |
||
| 94 | code += " ]\n" |
||
| 95 | code += f""" |
||
| 96 | def __str__(self): |
||
| 97 | vals = [name + ": " + str(val) for name, val in self.__dict__.items()] |
||
| 98 | return "{name}(" + ", ".join(vals) + ")" |
||
| 99 | |||
| 100 | __repr__ = __str__ |
||
| 101 | |||
| 102 | def __init__(self): |
||
| 103 | """ |
||
| 104 | if not sdef.Fields: |
||
| 105 | code += " pass" |
||
| 106 | for field, uatype in uatypes: |
||
| 107 | if field.ValueRank >= 1: |
||
| 108 | default_value = "[]" |
||
| 109 | else: |
||
| 110 | default_value = get_default_value(uatype) |
||
| 111 | code += f" self.{field.Name} = {default_value}\n" |
||
| 112 | print("CODE", code) |
||
| 113 | return code |
||
| 114 | |||
| 242 |