Conditions | 12 |
Total Lines | 64 |
Code Lines | 43 |
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 GridDataRetriever.GridDataRetriever.getData() 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 | import os |
||
25 | def getData(self): |
||
26 | """ Sends ThriftClient request and writes out received files.""" |
||
27 | req = GetGridDataRequest() |
||
28 | |||
29 | req.setPluginName(self.pluginName) |
||
30 | req.setModelId(self.modelId) |
||
31 | |||
32 | dt = datetime.strptime(self.cycle, '%y%m%d/%H%M') |
||
33 | ct = datetime.strftime(dt, '%Y-%m-%d %H:%M:%S') |
||
34 | req.setReftime(ct) |
||
35 | req.setFcstsec(self.forecast) |
||
36 | |||
37 | if self.level1 == '-1': |
||
38 | f1 = -999999.0 |
||
39 | else: |
||
40 | f1 = float(self.level1) |
||
41 | |||
42 | if self.level2 == '-1': |
||
43 | f2 = -999999.0 |
||
44 | else: |
||
45 | f2 = float(self.level2) |
||
46 | |||
47 | vcoord = self.vcoord |
||
48 | if vcoord == 'SGMA': |
||
49 | if f1 >= 0.0: |
||
50 | f1 = f1 / 10000 |
||
51 | if f2 >= 0.0: |
||
52 | f2 = f2 / 10000 |
||
53 | elif vcoord == 'DPTH': |
||
54 | if f1 >= 0.0: |
||
55 | f1 = f1 / 100.0 |
||
56 | if f2 >= 0.0: |
||
57 | f2 = f2 / 100.0 |
||
58 | elif vcoord == 'POTV': |
||
59 | if f1 >= 0.0: |
||
60 | f1 = f1 / 1000.0 |
||
61 | if f2 >= 0.0: |
||
62 | f2 = f2 / 1000.0 |
||
63 | |||
64 | req.setLevel1(str(f1)) |
||
65 | req.setLevel2(str(f2)) |
||
66 | req.setVcoord(vcoord) |
||
67 | |||
68 | req.setParm(self.param) |
||
69 | |||
70 | resp = self.client.sendRequest(req) |
||
71 | |||
72 | # Get the dimensions of the grid |
||
73 | kx = int(self.nx) |
||
74 | ky = int(self.ny) |
||
75 | kxky = kx * ky |
||
76 | |||
77 | # Put the data into a NUMPY array |
||
78 | grid = numpy.asarray(resp.getFloatData()) |
||
79 | |||
80 | # All grids need to be flipped from a GEMPAK point of view |
||
81 | # Reshape the array into 2D |
||
82 | grid = numpy.reshape(grid, (ky, kx)) |
||
83 | # Flip the array in the up-down direction |
||
84 | grid = numpy.flipud(grid) |
||
85 | # Reshape the array back into 1D |
||
86 | grid = numpy.reshape(grid, kxky) |
||
87 | |||
88 | return [replacemissing(x) for x in grid] |
||
89 | |||
129 |