How about including in web2py this
<https://pypi.python.org/pypi/jquery-unparam>module?
Here is my variant returning Storage (original returns dict).
Usage:
post_vars = jquery_unparam_w2p(request.body.read())
# -*- coding: utf-8 -*-
# based on jquery-unparam 2.0.0
import re
try:
from urllib import unquote_plus
except ImportError:
from urllib.parse import unquote_plus
from gluon.tools import Storage
def parse_key_pair(keyval):
keyval_splitted = keyval.split('=', 1)
if len(keyval_splitted) == 1:
key, val = keyval_splitted[0], ''
else:
key, val = keyval_splitted
if key == '':
return Storage()
groups = re.findall(r"\[.*?\]", key)
groups_joined = ''.join(groups)
if key[-len(groups_joined):] == groups_joined:
key = key[:-len(groups_joined)]
for group in reversed(groups):
if group == '[]':
val = [val]
else:
val = Storage({group[1:-1]: val})
return Storage( {key: val})
def merge_two_structs(s1, s2):
if isinstance(s1, list) and \
isinstance(s2, list):
return s1 + s2
if isinstance(s1, dict) and \
isinstance(s2, dict):
retval = Storage( s1.copy())
for key, val in s2.items():
if retval.get(key) is None:
retval[key] = val
else:
retval[key] = merge_two_structs(retval[key], val)
return retval
return s2
def merge_structs(structs):
if len(structs) == 0:
return None
if len(structs) == 1:
return structs[0]
first, rest = structs[0], structs[1:]
return merge_two_structs(first, merge_structs(rest))
def jquery_unparam_unquoted(jquery_params):
pair_strings = jquery_params.split('&')
key_pairs = [parse_key_pair(x) for x in pair_strings]
return merge_structs(key_pairs)
def jquery_unparam_w2p(jquery_params):
return jquery_unparam_unquoted(unquote_plus(jquery_params))
if __name__ == '__main__':
pass
On Tuesday, September 18, 2012 at 8:03:13 AM UTC+3, Osman Masood wrote:
>
> Thanks, Bruno! I actually don't care about the order of rows so
> request.vars.values() worked for me.
>
> Excellent.
>
> On Monday, September 17, 2012 6:32:27 PM UTC-7, rochacbruno wrote:
>>
>>
>> You can do something like this:
>>
>> In [17]: request_vars = {'table[0][]': ['1', '2'], 'table[1][]': ['3',
>> '4']}
>> In [18]: table = request_vars.values()
>> In [19]: table[0]
>> Out[19]: ['1', '2']
>> In [20]: table[1]
>> Out[20]: ['3', '4']
>>
>> or to be sure about the sequence.
>>
>> In [25]: request_vars = {'table[0][]': ['1', '2'], 'table[1][]': ['3',
>> '4']}
>> In [26]: table = [request_vars['table[%s][]' % i] for i in
>> range(len(request_vars))]
>> In [27]: table[0]
>> Out[27]: ['1', '2']
>> In [28]: table[1]
>> Out[28]: ['3', '4']
>>
>>
>>
>>
>>
>>
--
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 [email protected].
For more options, visit https://groups.google.com/d/optout.