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.<br>
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.<br>
Are we talking about storm version 2 here?<br>--<br>Vernon Cole<br><br><div class="gmail_quote">On Sat, Feb 14, 2009 at 3:20 PM, Gerdus van Zyl <span dir="ltr"><<a href="mailto:gerdusvanzyl@gmail.com">gerdusvanzyl@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">I subclass the storm properties for this purpose.<br>
eg:<br>
class dcUnicode(Unicode):<br>
def __init__(self, name=None, primary=False,size=255, **kwargs):<br>
self._size = size<br>
Unicode.__init__(self, name, primary, **kwargs)<br>
<br>
<br>
def __set__(self, obj, value):<br>
if isinstance(value,str):<br>
value = unicode(value)<br>
<br>
if not value == None and len(value) == 0:<br>
raise Exception("Empty string not allowed")<br>
<br>
if not value == None and len(value) == self._size:<br>
raise Exception("string longer than " + str(self._size))<br>
Unicode.__set__(self, obj, value)<br>
<br>
class dcDecimal(Decimal):<br>
def __init__(self, name=None, primary=False,**kwargs):<br>
Decimal.__init__(self, name, primary, **kwargs)<br>
<br>
<br>
def __set__(self, obj, value):<br>
if isinstance(value,float):<br>
value = decimal.Decimal(str(value))<br>
<br>
if isinstance(value,str):<br>
value = decimal.Decimal(value)<br>
<br>
Decimal.__set__(self, obj, value)<br>
<div><div></div><div class="Wj3C7c"><br>
<br>
<br>
<br>
On Sat, Feb 14, 2009 at 7:22 PM, Ben Wilber <<a href="mailto:benwilber@gmail.com">benwilber@gmail.com</a>> wrote:<br>
> Hello,<br>
><br>
> Is there a way to have storm do the type coercion automatically when I<br>
> try to set an attribute? Example:<br>
><br>
> class MyTable(object):<br>
> __storm_table__ = "my_table"<br>
> id = Int(primary=True)<br>
> num = Int()<br>
> name = Unicode()<br>
><br>
> m = MyTable()<br>
> m.num = "12345" # Raises exception<br>
> <a href="http://m.name" target="_blank">m.name</a> = 12345 # Raises exception<br>
><br>
> both values are easily coerced to the appropriate type, but when<br>
> building up a list (ie from a <form> submit), I have to first check<br>
> for safe input, then coerce from str to int, or unicode etc. Which<br>
> means that the methods I use to sanitize form input and put it in the<br>
> database have to know/care what type storm expects. It seems to me<br>
> that this is something that could just be done by storm itself and<br>
> raise a CoercionError or something if the conversion isn't possible<br>
> like m.num = "bob"<br>
><br>
> Right now I'm just maintaining a dict of dicts (for each attribute)<br>
> containing all the meta info about each:<br>
><br>
> MyTableDict = {<br>
> {"num": {"type": 0}},<br>
> {"name": {"type": u"", "max": 25}},<br>
> }<br>
><br>
> I use the max attribute to know the max length of the field ( can<br>
> storm do this? ) I type check against each "type" value in the dict<br>
> to determine what I should try to coerce the form input to.<br>
><br>
> This is probably amateur, but I really don't know of a better way.<br>
><br>
> Any help would be great.<br>
><br>
> Thanks!<br>
><br>
> Ben<br>
><br>
> --<br>
> storm mailing list<br>
> <a href="mailto:storm@lists.canonical.com">storm@lists.canonical.com</a><br>
> Modify settings or unsubscribe at: <a href="https://lists.ubuntu.com/mailman/listinfo/storm" target="_blank">https://lists.ubuntu.com/mailman/listinfo/storm</a><br>
><br>
<br>
--<br>
storm mailing list<br>
<a href="mailto:storm@lists.canonical.com">storm@lists.canonical.com</a><br>
Modify settings or unsubscribe at: <a href="https://lists.ubuntu.com/mailman/listinfo/storm" target="_blank">https://lists.ubuntu.com/mailman/listinfo/storm</a><br>
</div></div></blockquote></div><br>