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>
&nbsp; 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.&nbsp; If a unicode string can be interpreted as a number, it should be acceptable for a numeric field. Van Zyl&#39;s examples ought to be part of the interface.<br>
&nbsp; 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">&lt;<a href="mailto:gerdusvanzyl@gmail.com">gerdusvanzyl@gmail.com</a>&gt;</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>
 &nbsp; &nbsp;def __init__(self, name=None, primary=False,size=255, **kwargs):<br>
 &nbsp; &nbsp; &nbsp; &nbsp;self._size = size<br>
 &nbsp; &nbsp; &nbsp; &nbsp;Unicode.__init__(self, name, primary, **kwargs)<br>
<br>
<br>
 &nbsp; &nbsp;def __set__(self, obj, value):<br>
 &nbsp; &nbsp; &nbsp; &nbsp;if isinstance(value,str):<br>
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;value = unicode(value)<br>
<br>
 &nbsp; &nbsp; &nbsp; &nbsp;if not value == None and len(value) == 0:<br>
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;raise Exception(&quot;Empty string not allowed&quot;)<br>
<br>
 &nbsp; &nbsp; &nbsp; &nbsp;if not value == None and len(value) == self._size:<br>
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;raise Exception(&quot;string longer than &quot; + str(self._size))<br>
 &nbsp; &nbsp; &nbsp; &nbsp;Unicode.__set__(self, obj, value)<br>
<br>
class dcDecimal(Decimal):<br>
 &nbsp; &nbsp;def __init__(self, name=None, primary=False,**kwargs):<br>
 &nbsp; &nbsp; &nbsp; &nbsp;Decimal.__init__(self, name, primary, **kwargs)<br>
<br>
<br>
 &nbsp; &nbsp;def __set__(self, obj, value):<br>
 &nbsp; &nbsp; &nbsp; &nbsp;if isinstance(value,float):<br>
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;value = decimal.Decimal(str(value))<br>
<br>
 &nbsp; &nbsp; &nbsp; &nbsp;if isinstance(value,str):<br>
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;value = decimal.Decimal(value)<br>
<br>
 &nbsp; &nbsp; &nbsp; &nbsp;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 &lt;<a href="mailto:benwilber@gmail.com">benwilber@gmail.com</a>&gt; wrote:<br>
&gt; Hello,<br>
&gt;<br>
&gt; Is there a way to have storm do the type coercion automatically when I<br>
&gt; try to set an attribute? &nbsp;Example:<br>
&gt;<br>
&gt; class MyTable(object):<br>
&gt; &nbsp;__storm_table__ = &quot;my_table&quot;<br>
&gt; &nbsp;id = Int(primary=True)<br>
&gt; &nbsp;num = Int()<br>
&gt; &nbsp;name = Unicode()<br>
&gt;<br>
&gt; m = MyTable()<br>
&gt; m.num = &quot;12345&quot; # Raises exception<br>
&gt; <a href="http://m.name" target="_blank">m.name</a> = 12345 # Raises exception<br>
&gt;<br>
&gt; both values are easily coerced to the appropriate type, but when<br>
&gt; building up a list (ie from a &lt;form&gt; submit), I have to first check<br>
&gt; for safe input, then coerce from str to int, or unicode etc. &nbsp;Which<br>
&gt; means that the methods I use to sanitize form input and put it in the<br>
&gt; database have to know/care what type storm expects. &nbsp;It seems to me<br>
&gt; that this is something that could just be done by storm itself and<br>
&gt; raise a CoercionError or something if the conversion isn&#39;t possible<br>
&gt; like m.num = &quot;bob&quot;<br>
&gt;<br>
&gt; Right now I&#39;m just maintaining a dict of dicts (for each attribute)<br>
&gt; containing all the meta info about each:<br>
&gt;<br>
&gt; MyTableDict = {<br>
&gt; &nbsp;{&quot;num&quot;: {&quot;type&quot;: 0}},<br>
&gt; &nbsp;{&quot;name&quot;: {&quot;type&quot;: u&quot;&quot;, &quot;max&quot;: 25}},<br>
&gt; }<br>
&gt;<br>
&gt; I use the max attribute to know the max length of the field ( can<br>
&gt; storm do this? ) &nbsp;I type check against each &quot;type&quot; value in the dict<br>
&gt; to determine what I should try to coerce the form input to.<br>
&gt;<br>
&gt; This is probably amateur, but I really don&#39;t know of a better way.<br>
&gt;<br>
&gt; Any help would be great.<br>
&gt;<br>
&gt; Thanks!<br>
&gt;<br>
&gt; Ben<br>
&gt;<br>
&gt; --<br>
&gt; storm mailing list<br>
&gt; <a href="mailto:storm@lists.canonical.com">storm@lists.canonical.com</a><br>
&gt; 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>
&gt;<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>