Conditions | 17 |
Total Lines | 113 |
Code Lines | 76 |
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 notations_builder.generate_graphml_keys() 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 | ''' |
||
14 | def generate_graphml_keys(notations): |
||
15 | ''' |
||
16 | Derive the GraphML preamble from our notations file. This is cool, |
||
17 | because our GraphML import / export magically matches to the notations file. |
||
18 | |||
19 | GraphML allows to define extensions as part of the document, by having |
||
20 | <key> elements that describe the extensions. Later <data> elements |
||
21 | can refer to these keys. |
||
22 | |||
23 | Our GraphML export behaves nicely and always adds this preamble, so that |
||
24 | other GraphML tools can render the values nicely. For this reason, |
||
25 | the 'graphml_keys' list is generated. It is just an ugly collection of XML |
||
26 | definitions, which means that Marcus violates his own understand of |
||
27 | beautiful code ... |
||
28 | |||
29 | Our GraphML import does not rely on the existence of this preamble, but |
||
30 | simply wants to know if a given GraphML <data> element refers to a valid key. |
||
31 | For this reason, the 'graphml_graph_data' and 'graphml_node_data' list is generated. |
||
32 | It tells the import code if a graph / node element in the input XML is allowed |
||
33 | to have a particulary named data element. |
||
34 | ''' |
||
35 | graphml_keys = {} |
||
36 | graphml_graph_data = {} |
||
37 | graphml_node_data = {} |
||
38 | |||
39 | def generate_key_xml(name, kind, default, for_what='node'): |
||
40 | return \ |
||
41 | ' <key id="%s" for="%s" attr.name="%s" attr.type="%s">\n' \ |
||
42 | ' <default>%s</default>\n' \ |
||
43 | ' </key>' % (name, for_what, name, kind, default,) |
||
44 | |||
45 | for notation in notations: |
||
46 | notation_kind = notation['kind'] |
||
47 | graphml_graph_data[notation_kind] = set(['kind']) |
||
48 | graphml_node_data[notation_kind] = set(['id', 'kind', 'x', 'y']) |
||
49 | properties = set() |
||
50 | all_keys = [ |
||
51 | generate_key_xml('id', 'string', '0'), |
||
52 | generate_key_xml('kind', 'string', 'node'), |
||
53 | generate_key_xml('x', 'long', '0'), |
||
54 | generate_key_xml('y', 'long', '0'), |
||
55 | generate_key_xml('kind', 'string', 'faulttree', 'graph') |
||
56 | ] |
||
57 | |||
58 | for node_kind, node in notation['nodes'].items(): |
||
59 | for property_name, propertie in node.get('properties', {}).items(): |
||
60 | if property_name in properties: |
||
61 | continue |
||
62 | else: |
||
63 | properties.add(property_name) |
||
64 | |||
65 | property_default = propertie.get('default', '') |
||
66 | property_kind = propertie['kind'] |
||
67 | key = None |
||
68 | keys = None |
||
69 | |||
70 | if property_kind in {'text', 'textfield'}: |
||
71 | key = generate_key_xml( |
||
72 | property_name, 'string', property_default if propertie != 'name' else 'Node') |
||
73 | graphml_node_data[notation_kind].add(property_name) |
||
74 | elif property_kind == 'compound': |
||
75 | parts_index = property_default[0] |
||
76 | compound_parts = propertie['parts'] |
||
77 | |||
78 | keys = [ |
||
79 | generate_key_xml( |
||
80 | property_name, 'string', property_default[1]), |
||
81 | generate_key_xml( |
||
82 | property_name + 'Kind', 'string', compound_parts[parts_index]['partName']), |
||
83 | ] |
||
84 | graphml_node_data[notation_kind].add(property_name) |
||
85 | elif property_kind == 'bool': |
||
86 | key = generate_key_xml( |
||
87 | property_name, 'boolean', 'true' if property_default else 'false') |
||
88 | graphml_node_data[notation_kind].add(property_name) |
||
89 | elif property_kind == 'choice': |
||
90 | index = propertie['values'].index(property_default) |
||
91 | property_default = property_default[index] |
||
92 | key = generate_key_xml( |
||
93 | property_name, 'string', property_default) |
||
94 | graphml_node_data[notation_kind].add(property_name) |
||
95 | elif property_kind == 'epsilon': |
||
96 | keys = [ |
||
97 | generate_key_xml( |
||
98 | property_name, 'double', property_default[0]), |
||
99 | generate_key_xml( |
||
100 | property_name + 'Epsilon', 'double', property_default[1]) |
||
101 | ] |
||
102 | graphml_node_data[notation_kind].add(property_name) |
||
103 | elif property_kind in {'numeric', 'range'}: |
||
104 | kind = 'long' if propertie.get( |
||
105 | 'step', 1) == 1 else 'double' |
||
106 | |||
107 | if property_name != 'missionTime': |
||
108 | key = generate_key_xml( |
||
109 | property_name, kind, property_default) |
||
110 | graphml_node_data[notation_kind].add(property_name) |
||
111 | else: |
||
112 | key = generate_key_xml( |
||
113 | property_name, kind, property_default, 'graph') |
||
114 | graphml_graph_data[notation_kind].add(property_name) |
||
115 | elif property_kind == 'transfer': |
||
116 | key = generate_key_xml(property_name, 'string', '') |
||
117 | graphml_node_data[notation_kind].add(property_name) |
||
118 | |||
119 | if key is not None: |
||
120 | all_keys.append(key) |
||
121 | else: |
||
122 | all_keys.extend(keys) |
||
123 | |||
124 | graphml_keys[notation_kind] = '\n'.join(all_keys) |
||
125 | |||
126 | return graphml_keys, graphml_graph_data, graphml_node_data |
||
127 | |||
228 |