Rework functions in util.py

This commit is contained in:
DarkPhoenix
2010-09-05 17:41:09 +04:00
parent a31ea5f712
commit e6f24eaae5
4 changed files with 39 additions and 25 deletions

View File

@@ -21,7 +21,7 @@ from gui import builtinViewColumns
from gui.viewColumn import ViewColumn
from gui import bitmapLoader
import controller
from util import shorten
from util import formatAmount
class AttributeDisplay(ViewColumn):
name = "Attribute Display"
@@ -54,7 +54,7 @@ class AttributeDisplay(ViewColumn):
def getText(self, mod):
attr = mod.getModifiedItemAttr(self.info.name)
if attr:
return (shorten(attr, 3, 0, 3))
return (formatAmount(attr, 3, 0, 3))
else:
return ""

View File

@@ -21,7 +21,7 @@ from gui import builtinViewColumns
from gui.viewColumn import ViewColumn
from gui import bitmapLoader
import controller
from util import shorten
from util import formatAmount
class MaxRange(ViewColumn):
name = "Max range"
@@ -55,10 +55,10 @@ class MaxRange(ViewColumn):
if falloff is None:
falloff = ""
else:
falloff = "+%sm" % shorten(falloff, 3, 0, 3)
falloff = "+%sm" % formatAmount(falloff, 3, 0, 3)
if maxRange:
return "%sm%s" % (shorten(maxRange, 3, 0, 3), falloff)
return "%sm%s" % (formatAmount(maxRange, 3, 0, 3), falloff)
else:
return "" + falloff

View File

@@ -24,7 +24,7 @@ import gui.mainFrame
import controller
from eos.types import Slot, Hardpoint
from gui import pygauge as PG
from util import shorten
from util import formatAmount
class StatsPane(wx.Panel):
def collapseChanged(self, event):
@@ -88,14 +88,14 @@ class StatsPane(wx.Panel):
label.SetLabel(value)
label.SetToolTip(wx.ToolTip(value))
else:
label.SetLabel(shorten(value, prec, lowest, highest))
label.SetLabel(formatAmount(value, prec, lowest, highest))
label.SetToolTip(wx.ToolTip("%.1f" % value))
for labelName, value, prec, lowest, highest in stats:
label = getattr(self, labelName)
value = value() if fit is not None else 0
value = value if value is not None else 0
label.SetLabel(shorten(value, prec, lowest, highest))
label.SetLabel(formatAmount(value, prec, lowest, highest))
label.SetToolTip(wx.ToolTip("%.1f" % value))
# cap stuff
capState = fit.capState if fit is not None else 0
@@ -167,12 +167,12 @@ class StatsPane(wx.Panel):
lbl = getattr(self, "labelResistance%sEhp" % tankType.capitalize())
if ehp is not None:
total += ehp[tankType]
lbl.SetLabel(shorten(ehp[tankType], 3, 0, 9))
lbl.SetLabel(formatAmount(ehp[tankType], 3, 0, 9))
lbl.SetToolTip(wx.ToolTip("%s: %d" % (tankType.capitalize(), ehp[tankType])))
else:
lbl.SetLabel("0")
self.labelEhp.SetLabel(shorten(total, 3, 0, 9))
self.labelEhp.SetLabel(formatAmount(total, 3, 0, 9))
self.labelEhp.SetToolTip(wx.ToolTip("Effective: %d" % total))
damagePattern = fit.damagePattern if fit is not None else None
@@ -212,13 +212,13 @@ class StatsPane(wx.Panel):
if maxType == "shieldPassive":
self.labelMiniTankReinforced.SetLabel("")
self.labelMiniTankSustained.SetLabel(shorten(maxAmount, 3, 0, 9))
self.labelMiniTankSustained.SetLabel(formatAmount(maxAmount, 3, 0, 9))
self.labelMiniTankUnitReinforced.SetLabel("")
bitmap = bitmapLoader.getBitmap("%s_big" % maxType, "icons")
else:
self.labelMiniTankReinforced.SetLabel(shorten(maxAmount, 3, 0, 9))
self.labelMiniTankReinforced.SetLabel(formatAmount(maxAmount, 3, 0, 9))
sustainable = fit.sustainableTank["%sRepair" % maxType]
self.labelMiniTankSustained.SetLabel(shorten(sustainable, 3, 0, 9))
self.labelMiniTankSustained.SetLabel(formatAmount(sustainable, 3, 0, 9))
self.labelMiniTankUnitReinforced.SetLabel(" HP/S")
bitmap = bitmapLoader.getBitmap("%sActive_big" % maxType, "icons")
@@ -424,7 +424,7 @@ class StatsPane(wx.Panel):
sizerResistances.Add(wx.StaticText(self.fullPanel, wx.ID_ANY, "EHP"), wx.GBPosition( row, col ), wx.GBSpan( 1, 1 ), wx.ALIGN_CENTER)
col=0
row+=1
gaugeColours=( ((38,133,198),(52,86,98)), ((198,38,38),(83,65,67)), ((163,163,163),(74,90,93)), ((198,133,38),(81,83,67)) )
for tankType in ("shield", "armor", "hull", "separator", "damagePattern"):
@@ -436,7 +436,7 @@ class StatsPane(wx.Panel):
sizerResistances.Add(wx.StaticLine(self.fullPanel, wx.ID_ANY), wx.GBPosition( row, col ), wx.GBSpan( 1, 6 ), wx.EXPAND|wx.ALIGN_CENTER)
row+=1
col=0
continue
currGColour=0
@@ -479,7 +479,7 @@ class StatsPane(wx.Panel):
row+=1
col=0
# Resistances
sizerHeaderRechargeRates = wx.BoxSizer(wx.HORIZONTAL)
self.sizerBase.Add(sizerHeaderRechargeRates, 0, wx.EXPAND | wx.LEFT, 3)

32
util.py
View File

@@ -1,4 +1,4 @@
def shorten(val, prec=4, lowest=0, highest=0):
def formatAmount(val, prec=4, lowest=0, highest=0):
"""
Add suffix to value, transform value to match new suffix and round it.
@@ -7,17 +7,32 @@ def shorten(val, prec=4, lowest=0, highest=0):
prec -- precision of final number (number of significant positions to show)
lowest -- lowest order for suffixizing
highest -- highest order for suffixizing
"""
# Separate value to mantissa and suffix
mantissa, suffix = suffixizeAmount(val, lowest, highest)
# Round mantissa and add suffix
newMantissa = processAmount(mantissa, prec)
result = u"{0}{1}".format(newMantissa, suffix)
return result
def suffixizeAmount(val, lowest, highest):
"""
Add suffix to value and transform value to match new suffix.
Keyword arguments:
val -- value to process
lowest -- lowest order for suffixizing
highest -- highest order for suffixizing
Suffixes below lowest and above highest orders won't be used.
"""
# Take numbers only matching/above lowest possible positive suffix
if abs(val) >= 1000 and highest >= 3:
suffixmap = {3 : "k", 6 : "M", 9 : "G"}
# Start from highest possible suffix
for key in sorted(suffixmap, reverse = True):
# Find first suitable suffix and check if it's not above highest order
if val >= 10**key and key <= highest:
return u"{0}{1}".format(process(val/float(10**key), prec), suffixmap[key])
return val/float(10**key), suffixmap[key]
# Take numbers between 0 and 1, and matching/below highest possible negative suffix
elif abs(val) < 1 and val != 0 and lowest <= -3:
suffixmap = {-6 : u'\u03bc', -3 : "m"}
@@ -26,16 +41,15 @@ def shorten(val, prec=4, lowest=0, highest=0):
# Check if mantissa with next suffix is in range [1, 1000)
# Here we assume that each next order is greater than previous by 3
if val < 10**(key+3) and key >= lowest:
return u"{0}{1}".format(process(val/float(10**key), prec), suffixmap[key])
# No suitable suffixes are found withing given order borders, or value
return val/float(10**key), suffixmap[key]
# If no suitable suffixes are found within given order borders, or value
# is already within [1, 1000) boundaries, just return rounded value with no suffix
else:
return u"{0}".format(process(val, prec))
return val, ""
def process(val, prec):
def processAmount(val, prec):
"""
Round number.
Round number and return as string.
Keyword arguments:
val -- value to round