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