Christian:
  While Python is type agnostic, most databases are pretty fussy about what
you feed them. I presume that you need to convert the user's data into a
form acceptable to the database.
  You do not specify what tool you are using to write your results to the
database, so I will provide several answers to your question...
  1) If you are writing to the database using Python, and you are using a
fully PEP 249 compliant database module: The database module should attempt
the conversion for you, giving an exception if it cannot. You will need to
do the most obvious conversions yourself such as making a number into a
string, so an "if" "except" construct should do handily.  As far as I know,
only the adodbapi module is both fully PEP 249 compliant and implemented in
IronPython. http://sourceforge.net/projects/adodbapi
  2) You can use Python's "isinstance" built in function to see what the
user sent you:
<code>
impert types
a = theOutputFromYourUser()
if isinstance(a,types.StringType):
  print "it was a string"
if isinstance(a, (types.FloatType, types.IntType)):
  print "it was a number"
</code>
3) You can take advantage of python's "duck type" capability by simply
trying the user's output to see if it will work as a given type...
<code>
numericResult = None
stringResult = None
a = theOutputFromYourUser()
try:
    numericResult = float(a)
except:
   try:
        stringResult = str(a)
    except:
        pass
if numericResult != None:
   # your numeric stuff here
elif stringResult != None:
   # your string stuff here
else:
   # your error handling here.
</code>
4) Use some combination of the above. For example: adodbapi uses a
dictionary dispatch table to chose which conversion to call, which is a
variation on option 2, but then uses exception handling to catch errors
within the specific code for some conversions. YMMV
--
Vernon Cole



On Mon, Oct 19, 2009 at 5:20 AM, Michael Foord <fuzzy...@voidspace.org.uk>wrote:

> Christian Schmidt wrote:
>
>> Hello,
>>
>> we are using IronPython embedded into our application to evaluate user
>> defined expression. The "variables" used in the expressions are strongly
>> typed and the results need to be written back into a database.
>>
>> I know that the return type of a dynamic expression cannot be determined
>> in general. But what would be a pragmatic way to guess the return type
>> of an expression?
>>
>
> Hehe. If you're evaluating arbitrary functions provided by the user then I
> don't know of any way of 'inferring' the return type - beyond parsing it and
> modelling the type flow through the expression (which would be a lot of
> work).
>
> All the best,
>
> Michael
>
>>
>> Thanks,
>> Christian
>>
>>
>>
>> _______________________________________________
>> Users mailing list
>> Users@lists.ironpython.com
>> http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
>>
>
>
> --
> http://www.ironpythoninaction.com/
> http://www.voidspace.org.uk/blog
>
>
>
> _______________________________________________
> Users mailing list
> Users@lists.ironpython.com
> http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
>
_______________________________________________
Users mailing list
Users@lists.ironpython.com
http://lists.ironpython.com/listinfo.cgi/users-ironpython.com

Reply via email to