Conditions | 68 |
Total Lines | 152 |
Code Lines | 134 |
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 osci.dialog.TechInfoDlg.TechInfoDlg.show() 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 | # |
||
204 | def show(self): |
||
205 | tech = client.getTechInfo(self.techID) |
||
206 | player = client.getPlayer() |
||
207 | techEff = Rules.techImprEff[player.techs.get(self.techID, Rules.techBaseImprovement)] |
||
208 | self.win.title = _('Technology: %s (TL%d)') % (tech.name, tech.level) |
||
209 | # fill data |
||
210 | # fill description |
||
211 | descr = [] |
||
212 | improvement = player.techs.get(self.techID, 0) |
||
213 | if hasattr(tech, 'researchMod'): |
||
214 | if improvement == 0: |
||
215 | descr.append(_('Research costs: %d') % Utils.getTechRCost(player, self.techID)) |
||
216 | descr.append('') |
||
217 | elif improvement < Rules.techMaxImprovement: |
||
218 | descr.append(_('Improvement costs: %d') % Utils.getTechRCost(player, self.techID)) |
||
219 | descr.append('') |
||
220 | # requires |
||
221 | if tech.researchRequires and improvement == 0: |
||
222 | descr.append(_('Research requires:')) |
||
223 | for tmpTechID, improvement in tech.researchRequires: |
||
224 | tmpTech = client.getTechInfo(tmpTechID) |
||
225 | descr.append(_(' - %s improvement %d (TL%d)') % (tmpTech.name, improvement, tmpTech.level)) |
||
226 | if hasattr(tech, "researchReqSRes"): |
||
227 | for stratRes in tech.researchReqSRes: |
||
228 | descr.append(_(" - %s (strategic resource)") % (gdata.stratRes[stratRes])) |
||
229 | descr.append('') |
||
230 | if hasattr(tech, "researchDisables") and tech.researchDisables: |
||
231 | descr.append(_("Research disables:")) |
||
232 | for tmpTechID in tech.researchDisables: |
||
233 | tmpTech = client.getTechInfo(tmpTechID) |
||
234 | descr.append(_(" - %s (TL%d)") % (tmpTech.name, tmpTech.level)) |
||
235 | descr.append('') |
||
236 | if hasattr(tech, 'partialData') and hasattr(tech, 'textPreRsrch'): |
||
237 | # preresearch info |
||
238 | descr.append(_('Estimated use:')) |
||
239 | descr.extend(tech.textPreRsrch.split('\n')) |
||
240 | descr.append('') |
||
241 | elif not hasattr(tech, 'partialData'): |
||
242 | tmp = [] |
||
243 | for improvement in range(1, 6): |
||
244 | for tmpTechID in tech.researchEnables[improvement]: |
||
245 | tmpTech = client.getTechInfo(tmpTechID) |
||
246 | racesDispl = "" |
||
247 | raceChoosen = None |
||
248 | if player.techLevel == 1: |
||
249 | for techID in player.techs.keys(): |
||
250 | if techID in (1990, 1991, 1992): |
||
251 | raceChoosen = client.getTechInfo(techID).data |
||
252 | else: |
||
253 | raceChoosen = player.race |
||
254 | if len(tmpTech.researchRaces) > 0 and len(tmpTech.researchRaces) < 3 and not raceChoosen: |
||
255 | racesDispl = _(", only for %s") % tmpTech.researchRaces |
||
256 | if not raceChoosen or len(tmpTech.researchRaces) == 0 or raceChoosen in tmpTech.researchRaces: |
||
257 | tmp.append(_(' - %s (with improvement %d, on TL%d%s)') % (tmpTech.name, improvement, tmpTech.level, racesDispl)) |
||
258 | if tmp: |
||
259 | descr.append(_('Research/Improvement enables:')) |
||
260 | descr.extend(tmp) |
||
261 | descr.append('') |
||
262 | # production dependency |
||
263 | if tech.prodBioMod != [0, 0, 0, 0] and tech.prodBioMod != [0, 0, 0, 1]: |
||
264 | descr.append(_("Bio production depends:")) |
||
265 | if tech.prodBioMod[0]: descr.append(_(" - %d %% on planet's environment") % (tech.prodBioMod[0] * 100)) |
||
266 | if tech.prodBioMod[1]: descr.append(_(" - %d %% on planet's min. abundance") % (tech.prodBioMod[1] * 100)) |
||
267 | if tech.prodBioMod[2]: descr.append(_(" - %d %% on planet's en. abundance") % (tech.prodBioMod[2] * 100)) |
||
268 | if tech.prodBioMod[3]: descr.append(_(" - %d %% is not dependent on any planet's attribute") % (tech.prodBioMod[3] * 100)) |
||
269 | descr.append("") |
||
270 | if tech.prodEnMod != [0, 0, 0, 0] and tech.prodEnMod != [0, 0, 0, 1]: |
||
271 | descr.append(_("Energy production depends:")) |
||
272 | if tech.prodEnMod[0]: descr.append(_(" - %d %% on planet's environment") % (tech.prodEnMod[0] * 100)) |
||
273 | if tech.prodEnMod[1]: descr.append(_(" - %d %% on planet's min. abundance") % (tech.prodEnMod[1] * 100)) |
||
274 | if tech.prodEnMod[2]: descr.append(_(" - %d %% on planet's en. abundance") % (tech.prodEnMod[2] * 100)) |
||
275 | if tech.prodEnMod[3]: descr.append(_(" - %d %% is not dependent on any planet's attribute") % (tech.prodEnMod[3] * 100)) |
||
276 | descr.append("") |
||
277 | if tech.prodProdMod != [0, 0, 0, 0] and tech.prodProdMod != [0, 0, 0, 1]: |
||
278 | descr.append(_("Constr. points production depends:")) |
||
279 | if tech.prodProdMod[0]: descr.append(_(" - %d %% on planet's environment") % (tech.prodProdMod[0] * 100)) |
||
280 | if tech.prodProdMod[1]: descr.append(_(" - %d %% on planet's min. abundance") % (tech.prodProdMod[1] * 100)) |
||
281 | if tech.prodProdMod[2]: descr.append(_(" - %d %% on planet's en. abundance") % (tech.prodProdMod[2] * 100)) |
||
282 | if tech.prodProdMod[3]: descr.append(_(" - %d %% is not dependent on any planet's attribute") % (tech.prodProdMod[3] * 100)) |
||
283 | descr.append("") |
||
284 | if tech.prodSciMod != [0, 0, 0, 0] and tech.prodSciMod != [0, 0, 0, 1]: |
||
285 | descr.append(_("Research points production depends:")) |
||
286 | if tech.prodSciMod[0]: descr.append(_(" - %d %% on planet's environment") % (tech.prodSciMod[0] * 100)) |
||
287 | if tech.prodSciMod[1]: descr.append(_(" - %d %% on planet's min. abundance") % (tech.prodSciMod[1] * 100)) |
||
288 | if tech.prodSciMod[2]: descr.append(_(" - %d %% on planet's en. abundance") % (tech.prodSciMod[2] * 100)) |
||
289 | if tech.prodSciMod[3]: descr.append(_(" - %d %% is not dependent on any planet's attribute") % (tech.prodSciMod[3] * 100)) |
||
290 | descr.append("") |
||
291 | |||
292 | requiredStrategicResourcesForBuilding = self.getStrategicResourcesText(tech, "buildSRes") |
||
293 | requiredStrategicResourcesForResearch = self.getStrategicResourcesText(tech, "researchReqSRes") |
||
294 | if len(requiredStrategicResourcesForBuilding) > 0 or len(requiredStrategicResourcesForResearch) > 0: |
||
295 | descr.append(_("Required strategic resources:")) |
||
296 | if len(requiredStrategicResourcesForBuilding) > 0: |
||
297 | descr.append(_(" - for building: %s") % requiredStrategicResourcesForBuilding) |
||
298 | if len(requiredStrategicResourcesForResearch) > 0: |
||
299 | descr.append(_(" - for research: %s") % requiredStrategicResourcesForResearch) |
||
300 | |||
301 | descr.append("") |
||
302 | |||
303 | # decription |
||
304 | descr.append(_('Description:')) |
||
305 | if tech.textDescr != u'Not specified': |
||
306 | descr.extend(tech.textDescr.split('\n')) |
||
307 | else: |
||
308 | descr.extend(tech.textPreRsrch.split('\n')) |
||
309 | descr.append('') |
||
310 | # flavor |
||
311 | descr.append(_('Rumours:')) |
||
312 | descr.extend(tech.textFlavor.split('\n')) |
||
313 | descr.append('') |
||
314 | if not len(descr): |
||
315 | descr.append(_('No information available')) |
||
316 | # |
||
317 | self.win.vDescr.text = descr |
||
318 | # re-set techType if neccessary |
||
319 | self.techType = self.techType & ( |
||
320 | getattr(tech ,'isStructure', 0) * V_STRUCT | |
||
321 | getattr(tech ,'isShipHull', 0) * V_HULL | |
||
322 | getattr(tech ,'isShipEquip', 0) * V_SEQUIP | |
||
323 | getattr(tech ,'isProject', 0) * V_PROJECT |
||
324 | ) |
||
325 | if self.techType == V_NONE: |
||
326 | if getattr(tech ,'isStructure', 0): self.techType = V_STRUCT |
||
327 | elif getattr(tech ,'isShipHull', 0): self.techType = V_HULL |
||
328 | elif getattr(tech ,'isShipEquip', 0): self.techType = V_SEQUIP |
||
329 | elif getattr(tech ,'isProject', 0): self.techType = V_PROJECT |
||
330 | # set type buttons |
||
331 | self.win.vStruct.pressed = self.techType == V_STRUCT |
||
332 | self.win.vStruct.enabled = getattr(tech ,'isStructure', 0) |
||
333 | self.win.vHull.pressed = self.techType == V_HULL |
||
334 | self.win.vHull.enabled = getattr(tech, 'isShipHull', 0) |
||
335 | self.win.vSEquip.pressed = self.techType == V_SEQUIP |
||
336 | self.win.vSEquip.enabled = getattr(tech, 'isShipEquip', 0) |
||
337 | self.win.vProject.pressed = self.techType == V_PROJECT |
||
338 | self.win.vProject.enabled = getattr(tech, 'isProject', 0) |
||
339 | # fill data |
||
340 | items = [] |
||
341 | for attr in dir(tech): |
||
342 | value = getattr(tech, attr) |
||
343 | descr, props, showIfDef, default, convertor = techAttrs.get(attr, defaultAttr) |
||
344 | if self.techType & props and (value != default or showIfDef): |
||
345 | item = ui.Item(descr, tValue = convertor(value)) |
||
346 | if V_EFF & props: |
||
347 | item.font = 'normal-bold' |
||
348 | item.tValue = convertor(value * techEff) |
||
349 | if V_EFF_REV & props: |
||
350 | item.font = 'normal-bold' |
||
351 | item.tValue = convertor(value / techEff) |
||
352 | items.append(item) |
||
353 | |||
354 | self.win.vData.items = items |
||
355 | self.win.vData.itemsChanged() |
||
356 | |||
425 |