Conditions | 22 |
Total Lines | 96 |
Code Lines | 81 |
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 GridInfoRetriever.GridInfoRetriever.getInfo() 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 |
||
19 | def getInfo(self): |
||
20 | """ Sends ThriftClient request and writes out received files.""" |
||
21 | req = GetGridInfoRequest() |
||
22 | req.setPluginName(self.pluginName) |
||
23 | req.setModelId(self.modelId) |
||
24 | |||
25 | req.setReftime(self.cycle) |
||
26 | if len(self.cycle) > 2: |
||
27 | dt = datetime.strptime(self.cycle, '%y%m%d/%H%M') |
||
28 | ct = datetime.strftime(dt, '%Y-%m-%d %H:%M:%S') |
||
29 | req.setReftime(ct) |
||
30 | |||
31 | req.setFcstsec(self.forecast) |
||
32 | |||
33 | resp = self.client.sendRequest(req) |
||
34 | |||
35 | sortresp = sorted(sorted(resp, key=itemgetter("reftime"), reverse=True), key=itemgetter("fcstsec")) |
||
36 | |||
37 | grids = [] |
||
38 | |||
39 | count = 0 |
||
40 | for record in sortresp: |
||
41 | s = '{:<12}'.format(record['param']) |
||
42 | |||
43 | if sys.byteorder == 'little': |
||
44 | parm1 = (ord(s[3]) << 24) + (ord(s[2]) << 16) + (ord(s[1]) << 8) + ord(s[0]) |
||
45 | parm2 = (ord(s[7]) << 24) + (ord(s[6]) << 16) + (ord(s[5]) << 8) + ord(s[4]) |
||
46 | parm3 = (ord(s[11]) << 24) + (ord(s[10]) << 16) + (ord(s[9]) << 8) + ord(s[8]) |
||
47 | else: |
||
48 | parm1 = (ord(s[0]) << 24) + (ord(s[1]) << 16) + (ord(s[2]) << 8) + ord(s[3]) |
||
49 | parm2 = (ord(s[4]) << 24) + (ord(s[5]) << 16) + (ord(s[6]) << 8) + ord(s[7]) |
||
50 | parm3 = (ord(s[8]) << 24) + (ord(s[9]) << 16) + (ord(s[10]) << 8) + ord(s[11]) |
||
51 | |||
52 | dt = datetime.strptime(record['reftime'], '%Y-%m-%d %H:%M:%S.%f') |
||
53 | dattim = dt.month * 100000000 + dt.day * 1000000 + (dt.year%100) * 10000 + dt.hour * 100 + dt.minute |
||
54 | fcsth = (int(record['fcstsec']) / 60) / 60 |
||
55 | fcstm = (int(record['fcstsec']) / 60) % 60 |
||
56 | fcst = 100000 + fcsth * 100 + fcstm |
||
57 | |||
58 | lv1 = float(record['level1']) |
||
59 | if lv1 == -999999.0: |
||
60 | lv1 = -1.0 |
||
61 | lv2 = float(record['level2']) |
||
62 | if lv2 == -999999.0: |
||
63 | lv2 = -1.0 |
||
64 | |||
65 | vcd = record['vcoord'] |
||
66 | if vcd == 'NONE': |
||
67 | ivcd = 0 |
||
68 | elif vcd == 'PRES': |
||
69 | ivcd = 1 |
||
70 | elif vcd == 'THTA': |
||
71 | ivcd = 2 |
||
72 | elif vcd == 'HGHT': |
||
73 | ivcd = 3 |
||
74 | elif vcd == 'SGMA': |
||
75 | ivcd = 4 |
||
76 | if lv1 >= 0.0: |
||
77 | lv1 = lv1 * 10000.0 |
||
78 | if lv2 >= 0.0: |
||
79 | lv2 = lv2 * 10000.0 |
||
80 | elif vcd == 'DPTH': |
||
81 | ivcd = 5 |
||
82 | if lv1 >= 0.0: |
||
83 | lv1 = lv1 * 100.0 |
||
84 | if lv2 >= 0.0: |
||
85 | lv2 = lv2 * 100.0 |
||
86 | elif vcd == 'HYBL': |
||
87 | ivcd = 6 |
||
88 | else: |
||
89 | v = '{:<4}'.format(vcd) |
||
90 | if sys.byteorder == 'little': |
||
91 | ivcd = (ord(v[3]) << 24) + (ord(v[2]) << 16) + (ord(v[1]) << 8) + ord(v[0]) |
||
92 | else: |
||
93 | ivcd = (ord(v[0]) << 24) + (ord(v[1]) << 16) + (ord(v[2]) << 8) + ord(v[3]) |
||
94 | if vcd == 'POTV': |
||
95 | if lv1 >= 0.0: |
||
96 | lv1 = lv1 * 1000.0 |
||
97 | if lv2 >= 0.0: |
||
98 | lv2 = lv2 * 1000.0 |
||
99 | grids.append(9999) |
||
100 | grids.append(dattim) |
||
101 | grids.append(fcst) |
||
102 | grids.append(0) |
||
103 | grids.append(0) |
||
104 | grids.append(int(lv1)) |
||
105 | grids.append(int(lv2)) |
||
106 | grids.append(ivcd) |
||
107 | grids.append(parm1) |
||
108 | grids.append(parm2) |
||
109 | grids.append(parm3) |
||
110 | count += 1 |
||
111 | if count > 29998: |
||
112 | break |
||
113 | |||
114 | return grids |
||
115 | |||
138 |