It shows how little I have actually used storm, and different database api engines, that I am surprised at this request. Adodbapi spends a lot of code doing conversions like this internally, so does the pywin32 COM interface that adodbapi calls. Even then I wish for better. IMHO an interface that runs on the level of storm should accept almost anything and do the work of encoding it into the format the database wants. If a unicode string can be interpreted as a number, it should be acceptable for a numeric field. Van Zyl's examples ought to be part of the interface. Are we talking about storm version 2 here? -- Vernon Cole
On Sat, Feb 14, 2009 at 3:20 PM, Gerdus van Zyl <[email protected]>wrote: > I subclass the storm properties for this purpose. > eg: > class dcUnicode(Unicode): > def __init__(self, name=None, primary=False,size=255, **kwargs): > self._size = size > Unicode.__init__(self, name, primary, **kwargs) > > > def __set__(self, obj, value): > if isinstance(value,str): > value = unicode(value) > > if not value == None and len(value) == 0: > raise Exception("Empty string not allowed") > > if not value == None and len(value) == self._size: > raise Exception("string longer than " + str(self._size)) > Unicode.__set__(self, obj, value) > > class dcDecimal(Decimal): > def __init__(self, name=None, primary=False,**kwargs): > Decimal.__init__(self, name, primary, **kwargs) > > > def __set__(self, obj, value): > if isinstance(value,float): > value = decimal.Decimal(str(value)) > > if isinstance(value,str): > value = decimal.Decimal(value) > > Decimal.__set__(self, obj, value) > > > > > On Sat, Feb 14, 2009 at 7:22 PM, Ben Wilber <[email protected]> wrote: > > Hello, > > > > Is there a way to have storm do the type coercion automatically when I > > try to set an attribute? Example: > > > > class MyTable(object): > > __storm_table__ = "my_table" > > id = Int(primary=True) > > num = Int() > > name = Unicode() > > > > m = MyTable() > > m.num = "12345" # Raises exception > > m.name = 12345 # Raises exception > > > > both values are easily coerced to the appropriate type, but when > > building up a list (ie from a <form> submit), I have to first check > > for safe input, then coerce from str to int, or unicode etc. Which > > means that the methods I use to sanitize form input and put it in the > > database have to know/care what type storm expects. It seems to me > > that this is something that could just be done by storm itself and > > raise a CoercionError or something if the conversion isn't possible > > like m.num = "bob" > > > > Right now I'm just maintaining a dict of dicts (for each attribute) > > containing all the meta info about each: > > > > MyTableDict = { > > {"num": {"type": 0}}, > > {"name": {"type": u"", "max": 25}}, > > } > > > > I use the max attribute to know the max length of the field ( can > > storm do this? ) I type check against each "type" value in the dict > > to determine what I should try to coerce the form input to. > > > > This is probably amateur, but I really don't know of a better way. > > > > Any help would be great. > > > > Thanks! > > > > Ben > > > > -- > > storm mailing list > > [email protected] > > Modify settings or unsubscribe at: > https://lists.ubuntu.com/mailman/listinfo/storm > > > > -- > storm mailing list > [email protected] > Modify settings or unsubscribe at: > https://lists.ubuntu.com/mailman/listinfo/storm >
-- storm mailing list [email protected] Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/storm
