Conditions | 14 |
Total Lines | 102 |
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 scripts.dawn_runner.run_savu.runSavu() 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 | ''' |
||
37 | def runSavu(path2plugin, params, metaOnly, inputs, persistence): |
||
38 | ''' |
||
39 | path2plugin - is the path to the user script that should be run |
||
40 | params - are the savu parameters |
||
41 | metaOnly - a boolean for whether the data is kept in metadata or is passed as data |
||
42 | inputs - is a dictionary of input objects |
||
43 | ''' |
||
44 | t1 = time.time() |
||
45 | sys_path_0_lock = persistence['sys_path_0_lock'] |
||
46 | sys_path_0_set = persistence['sys_path_0_set'] |
||
47 | plugin_object = persistence['plugin_object'] |
||
48 | axis_labels = persistence['axis_labels'] |
||
49 | axis_values = persistence['axis_values'] |
||
50 | string_key = persistence['string_key'] |
||
51 | parameters = persistence['parameters'] |
||
52 | aux = persistence['aux'] |
||
53 | sys_path_0_lock.acquire() |
||
54 | try: |
||
55 | result = copy(inputs) |
||
56 | |||
57 | scriptDir = os.path.dirname(path2plugin) |
||
58 | sys_path_0 = sys.path[0] |
||
59 | if sys_path_0_set and scriptDir != sys_path_0: |
||
60 | raise Exception("runSavu attempted to change sys.path[0] in a way that " |
||
61 | "could cause a race condition. Current sys.path[0] is {!r}, " |
||
62 | "trying to set to {!r}".format(sys_path_0, scriptDir)) |
||
63 | else: |
||
64 | sys.path[0] = scriptDir |
||
65 | sys_path_0_set = True |
||
66 | |||
67 | if not plugin_object: |
||
68 | parameters = {} |
||
69 | # slight repack here |
||
70 | for key in list(params.keys()): |
||
71 | # print "here" |
||
72 | val = params[key]["value"] |
||
73 | if type(val)==type(''): |
||
74 | val = val.replace('\n','').strip() |
||
75 | # print val |
||
76 | parameters[key] = val |
||
77 | print(("val: {}".format(val))) |
||
78 | # print "initialising the object" |
||
79 | plugin_object = _savu_setup(path2plugin, inputs, parameters) |
||
80 | persistence['plugin_object'] = plugin_object |
||
81 | axis_labels, axis_values = process_init(plugin_object) |
||
82 | # print "I did the initialisation" |
||
83 | # print "axis labels",axis_labels |
||
84 | # print "axis_values", axis_values |
||
85 | # print plugin_object |
||
86 | chkstring = [any(isinstance(ix, str) for ix in axis_values[label]) for label in axis_labels] |
||
87 | if any(chkstring): # are any axis values strings we instead make this an aux out |
||
88 | metaOnly = True |
||
89 | # print "AXIS LABELS"+str(axis_values) |
||
90 | string_key = axis_labels[chkstring.index(True)] |
||
91 | aux = OrderedDict.fromkeys(axis_values[string_key]) |
||
92 | # print aux.keys() |
||
93 | else: |
||
94 | string_key = axis_labels[0]# will it always be the first one? |
||
95 | if not metaOnly: |
||
96 | if len(axis_labels) == 1: |
||
97 | result['xaxis']=axis_values[axis_labels[0]] |
||
98 | result['xaxis_title']=axis_labels[0] |
||
99 | if len(axis_labels) == 2: |
||
100 | # print "set the output axes" |
||
101 | x = axis_labels[0] |
||
102 | result['xaxis_title']=x |
||
103 | y = axis_labels[1] |
||
104 | result['yaxis_title']=y |
||
105 | result['yaxis']=axis_values[y] |
||
106 | result['xaxis']=axis_values[x] |
||
107 | else: |
||
108 | pass |
||
109 | finally: |
||
110 | sys_path_0_lock.release() |
||
111 | |||
112 | if plugin_object.get_max_frames()>1: # we need to get round this since we are frame independant |
||
113 | data = np.expand_dims(inputs['data'], 0) |
||
114 | else: |
||
115 | data = inputs['data'] |
||
116 | |||
117 | print(("metaOnly: {}".format(metaOnly))) |
||
118 | |||
119 | if not metaOnly: |
||
120 | |||
121 | out = plugin_object.process_frames([data]) |
||
122 | # print "ran the plugin" |
||
123 | |||
124 | result['data'] = out |
||
125 | elif metaOnly: |
||
126 | result['data'] = inputs['data'] |
||
127 | # print type(result['data']) |
||
128 | out_array = plugin_object.process_frames([inputs['data']]) |
||
129 | |||
130 | # print aux.keys() |
||
131 | |||
132 | for k,key in enumerate(aux.keys()): |
||
133 | aux[key]=np.array([out_array[k]])# wow really |
||
134 | |||
135 | result['auxiliary'] = aux |
||
136 | t2 = time.time() |
||
137 | print("time to runSavu = "+str((t2-t1))) |
||
138 | return result |
||
139 | |||
235 |