[web2py] Re: Type conversion for args/vars?

2017-05-11 Thread Anthony
In many cases, values are already converted to the appropriate type 
automatically (e.g., when doing database inserts). Aside from that, you 
could write your own convenience function in a few lines of code, and it 
can do exactly what you want (e.g., convert both "True" and "true" to a 
boolean). For example:

import ast
def convert(value):
value = 'True' if value == 'true' else 'False' if value == 'false' else 
value
try:
return ast.literal_eval(value)
except:
pass
return value

Anthony

On Thursday, May 11, 2017 at 3:10:47 AM UTC-4, Brendan Barnwell wrote:
>
> Does web2py provide any kind of utility to convert the request args and 
> vars "intelligently" into regular Python types?  Values passed in the URL 
> or query string will always be received as strings by web2py code, but in 
> many cases they really need to be converted to other types.  In a URL like 
> ".../app/controller/function/this/1?option=True=3.456", it would be 
> nice to have a simple function that could be called on each argument to 
> convert it to the "right" type.  It would be nice if it were slightly 
> smarter than ast.literal_eval, for instance maybe accepting "true" and 
> "false" as well as "True" and "False".
>
> I'm not suggesting anything that would automatically do this, but just a 
> convenience function that could be explicitly called on the arguments you 
> want to convert, e.g.
>
> number = convert(request.args[1])
> option = convert(request.vars['option'])
> # etc.
>
> Is there anything like this?  Would it be a good thing to add?  It seems 
> like the sort of thing I'd expect there to be a general-purpose Python 
> library for, but looking around I wasn't able to find anything.
>
>

-- 
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
--- 
You received this message because you are subscribed to the Google Groups 
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to web2py+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[web2py] Re: Type conversion for args/vars?

2017-05-11 Thread Marcelo Huerta

El jueves, 11 de mayo de 2017, 4:10:47 (UTC-3), Brendan Barnwell escribió:
>
>
> number = convert(request.args[1])
>

number = request.args(1, cast=int)
 

> option = convert(request.vars['option'])
>

I'm not aware of anything in this case.

-- 
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
--- 
You received this message because you are subscribed to the Google Groups 
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to web2py+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.