From: W. Trevor King Date: Tue, 10 Aug 2010 12:34:12 +0000 (-0400) Subject: Better None-handling in convert and propertyeditor X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=34b6c0e1270cb344a4d8cc1b03c8e628cdeaf65d;p=hooke.git Better None-handling in convert and propertyeditor --- diff --git a/hooke/ui/gui/panel/propertyeditor.py b/hooke/ui/gui/panel/propertyeditor.py index 3bd571f..5cc4469 100644 --- a/hooke/ui/gui/panel/propertyeditor.py +++ b/hooke/ui/gui/panel/propertyeditor.py @@ -33,7 +33,7 @@ import wx.grid from . import Panel from ....plugin import argument_to_setting -from ....util.convert import ANALOGS +from ....util.convert import ANALOGS, to_string, from_string def props_from_argument(argument, curves=None, playlists=None): @@ -102,12 +102,12 @@ class Property (object): def string_for_value(self, value): """Return a string representation of `value` for loading the table. """ - return str(value) + return to_string(value, 'string') def value_for_string(self, string): """Return the value represented by `string`. """ - return string + return from_string(string, 'string') class StringProperty (Property): @@ -174,7 +174,7 @@ class IntProperty (Property): return wx.grid.GridCellNumberRenderer() def value_for_string(self, string): - return int(string) + return from_string(string, 'int') class FloatProperty (Property): @@ -191,7 +191,7 @@ class FloatProperty (Property): return wx.grid.GridCellFloatRenderer() def value_for_string(self, string): - return float(string) + return from_string(string, 'float') class ChoiceProperty (Property): diff --git a/hooke/util/convert.py b/hooke/util/convert.py index 83ec764..f64bb46 100644 --- a/hooke/util/convert.py +++ b/hooke/util/convert.py @@ -39,6 +39,23 @@ def to_string(value, type, count=1): def from_string(value, type, count=1): """Convert `value` from a string to `type`. + + Examples + -------- + >>> from_string('abcde', type='string') + u'abcde' + >>> from_string('None', type='string') + >>> from_string(None, type='string') + >>> from_string('true', type='bool') + True + >>> from_string('false', type='bool') + False + >>> from_string(None, type='bool') + False + >>> from_string('123', type='int') + 123 + >>> from_string('123', type='float') + 123.0 """ type = ANALOGS.get(type, type) if type in RAW_TYPES: @@ -55,15 +72,19 @@ def from_string(value, type, count=1): return fn(value) def _string_to_string(value): - if len(value) == 0: + if value in [None, 'None'] or len(value) == 0: return None return unicode(value) def _string_to_bool(value): - return value == 'True' + return hasattr(value, 'lower') and value.lower() == 'true' def _string_to_int(value): + if value in [None, 'None']: + return None return int(value) def _string_to_float(value): + if value in [None, 'None']: + return None return float(value)