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