def test_GetUnitNames(): ## note: not testing all of them ## either all types or all names names = unit_conversion.GetUnitNames('Length') print names assert "meter" in names assert "foot" in names assert not "feet" in names names = unit_conversion.GetUnitNames('Area') print names assert "square foot" in names assert "acre" in names assert "square meter" in names assert not "feet" in names
def __init__(self, parent, id, UnitType): wx.Panel.__init__(self, parent, id, wx.DefaultPosition, style=wx.SUNKEN_BORDER) self.UnitType = UnitType Units = UC.GetUnitNames(UnitType) Units.sort(NoCaseCompare) self.FromUnits = wx.Choice(self, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, Units) self.FromUnits.SetSelection(0) self.ToUnits = wx.Choice(self, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, Units) self.ToUnits.SetSelection(0) self.InputBox = wx.TextCtrl(self, wx.ID_ANY, "", wx.DefaultPosition, (100, -1)) self.OutBox = wx.TextCtrl(self, wx.ID_ANY, "", wx.DefaultPosition, (100, -1), style=wx.TE_READONLY) Grid = wx.FlexGridSizer(6, 2, 5, 20) Grid.Add( wx.StaticText(self, -1, "Convert From:", wx.DefaultPosition, wx.DefaultSize), 0, wx.ALIGN_LEFT) Grid.Add((20, 1), 1) # adding a spacer Grid.Add(self.InputBox, 0, wx.ALIGN_LEFT) Grid.Add(self.FromUnits, 0, wx.ALIGN_RIGHT) Grid.Add((20, 20), 1) # adding a spacer Grid.Add((20, 20), 1) # adding a spacer Grid.Add( wx.StaticText(self, -1, "Convert To:", wx.DefaultPosition, wx.DefaultSize), 0, wx.ALIGN_LEFT) Grid.Add((20, 1), 1) # adding a spacer Grid.Add(self.OutBox, 0, wx.ALIGN_LEFT) Grid.Add(self.ToUnits, 0, wx.ALIGN_RIGHT) Grid.Layout() OuterBox = wx.BoxSizer(wx.VERTICAL) OuterBox.Add((20, 20), 0) Label = wx.StaticText(self, -1, UnitType, wx.DefaultPosition, wx.DefaultSize) of = Label.GetFont() Font = wx.Font(int(of.GetPointSize() * 2), of.GetFamily(), wx.NORMAL, wx.NORMAL) Label.SetFont(Font) IconBox = wx.BoxSizer(wx.HORIZONTAL) IconBox.Add(wx.StaticBitmap(self, bitmap=icons.NUCOS64.GetBitmap()), 0, wx.ALIGN_LEFT | wx.LEFT, 30) IconBox.Add((1, 1), 1, wx.EXPAND) IconBox.Add(Label, 0, wx.ALIGN_CENTER) IconBox.Add((1, 1), 1, wx.EXPAND) IconBox.Add(wx.StaticBitmap(self, bitmap=icons.NOAA64.GetBitmap()), 0, wx.ALIGN_RIGHT | wx.RIGHT, 30) OuterBox.Add(IconBox, 0, wx.EXPAND) OuterBox.Add(Grid, 0, wx.ALIGN_CENTER | wx.ALL, 30) OuterBox.Layout() self.SetAutoLayout(True) self.SetSizer(OuterBox) self.Fit() self.InputBox.Bind(wx.EVT_TEXT, self.Recalculate) self.FromUnits.Bind(wx.EVT_CHOICE, self.Recalculate) self.ToUnits.Bind(wx.EVT_CHOICE, self.Recalculate)
def _valid_units(unit_name): 'convenience function to get all valid units accepted by unit_conversion' _valid_units = uc.GetUnitNames(unit_name) _valid_units.extend( chain(*[val[1] for val in uc.ConvertDataUnits[unit_name].values()])) return tuple(_valid_units)
def __init__(self, *args, **kwargs): kwargs['style'] = wx.SUNKEN_BORDER wx.Panel.__init__(self, *args, **kwargs) DensityUnits = UC.GetUnitNames("Density") DensityUnits.sort(key=str.lower) VolUnits = UC.GetUnitNames("Volume") VolUnits.sort(key=str.lower) MassUnits = UC.GetUnitNames("Mass") MassUnits.sort(key=str.lower) OilTypeNames = sorted(OilTypes.keys(), key=str.lower) self.OilTypeLabel = wx.StaticText(self, label="Oil Type:") self.OilType = wx.Choice(self, choices=OilTypeNames) self.OilType.SetStringSelection(u"Unknown") self.DensityLabel = wx.StaticText(self, label="Oil Density:") self.DensityBox = wx.TextCtrl(self, value="") self.DensityUnits = wx.Choice(self, choices=DensityUnits) self.DensityUnits.SetStringSelection("API degree") OilTypeSizer = wx.FlexGridSizer(7, 3, 5, 5) OilTypeSizer.AddGrowableCol(1) OilTypeSizer.Add(self.OilTypeLabel, 0) OilTypeSizer.Add(self.OilType, 0) OilTypeSizer.Add((1, 1), 1) OilTypeSizer.Add(self.DensityLabel, 0) OilTypeSizer.Add(self.DensityBox, 1, wx.EXPAND) OilTypeSizer.Add(self.DensityUnits, 0) OilTypeSizer.Add((20, 20), 0) OilTypeSizer.Add((20, 20), 0) OilTypeSizer.Add((20, 20), 0) MassLabel = wx.StaticText(self, label="Mass:") self.MassBox = wx.TextCtrl(self) self.MassUnits = wx.Choice(self, choices=MassUnits) self.MassUnits.SetStringSelection("metric ton (tonne)") VolLabel = wx.StaticText(self, label="Volume:") self.VolBox = wx.TextCtrl(self) self.VolUnits = wx.Choice(self, choices=VolUnits) self.VolUnits.SetStringSelection("barrel (petroleum)") OilTypeSizer.Add(MassLabel, 0) OilTypeSizer.Add(self.MassBox, 0, wx.EXPAND) OilTypeSizer.Add(self.MassUnits, 0, wx.EXPAND) OilTypeSizer.Add(VolLabel, 0) OilTypeSizer.Add(self.VolBox, 0, wx.EXPAND) OilTypeSizer.Add(self.VolUnits, 0, wx.EXPAND) VertBox = wx.BoxSizer(wx.VERTICAL) Label = wx.StaticText(self, label="Oil Quantity") of = Label.GetFont() Font = wx.Font(int(of.GetPointSize() * 2), of.GetFamily(), wx.NORMAL, wx.NORMAL) Label.SetFont(Font) IconBox = wx.BoxSizer(wx.HORIZONTAL) IconBox.Add(wx.StaticBitmap(self, bitmap=icons.NUCOS64.GetBitmap()), 0, wx.ALIGN_LEFT | wx.RIGHT | wx.LEFT, 20) IconBox.Add((1, 1), 1, wx.EXPAND) IconBox.Add(Label, 0, wx.ALIGN_CENTER) IconBox.Add((1, 1), 1, wx.EXPAND) IconBox.Add(wx.StaticBitmap(self, bitmap=icons.NOAA64.GetBitmap()), 0, wx.ALIGN_RIGHT | wx.RIGHT | wx.LEFT, 20) VertBox.Add((20, 20), 1) VertBox.Add(IconBox, 0, wx.EXPAND) VertBox.Add((20, 20), 1) VertBox.Add(OilTypeSizer, 0, wx.ALIGN_LEFT | wx.ALL, 10) VertBox.Add((20, 20), 1) OuterBox = wx.BoxSizer(wx.HORIZONTAL) OuterBox.Add((5, 5), 1) OuterBox.Add(VertBox, 0) OuterBox.Add((5, 5), 1) self.OilType.Bind(wx.EVT_CHOICE, self.OnOilType) self.DensityBox.Bind(wx.EVT_TEXT, self.OnDensity) self.DensityUnits.Bind(wx.EVT_CHOICE, self.OnDensityUnits) self.VolBox.Bind(wx.EVT_TEXT, self.OnVolume) self.VolUnits.Bind(wx.EVT_CHOICE, self.Calculate) self.MassBox.Bind(wx.EVT_TEXT, self.OnMass) self.MassUnits.Bind(wx.EVT_CHOICE, self.Calculate) self.SetSizerAndFit(OuterBox) self.LastTyped = "Mass" # could be "Mass" or "Volume"