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