DrJohnZoidberg
Honorary Master
There is a little method in an app I'm writing that processes GET parameters sent from a web request for the Jquery plugin Datatables (https://www.datatables.net/).
The parameters it sends through look like this:
etc..
I need to extract these into something Python can understand. If these parameters were not arrays it would be easy (read, I would understand how to do it).
What I've written now works but it relies on the list being sorted correctly and statements being in the correct order:
Here I'm extracting the fields for data, orderable and searchable, but I have specifically check for each one.
How do I write this so it iterates through all of the parameters and extracts them without me telling it which ones to extract? I'm pretty sure this is such a simple thing but I cannot figure it out.
My Python-foo is weak, assistance I need.
TIA
The parameters it sends through look like this:
Code:
columns[0][data]:id
columns[0][name]:
columns[0][searchable]:true
columns[0][orderable]:true
columns[0][search][value]:
columns[0][search][regex]:false
columns[1][data]:date
columns[1][name]:
columns[1][searchable]:true
columns[1][orderable]:true
columns[1][search][value]:
columns[1][search][regex]:false
etc..
I need to extract these into something Python can understand. If these parameters were not arrays it would be easy (read, I would understand how to do it).
What I've written now works but it relies on the list being sorted correctly and statements being in the correct order:
Code:
def process_kwargs(kwargs):
column_parameters = []
for kwarg in sorted(kwargs):
if re.search(r"\[(\w+)\]", kwarg) and kwarg[:7] == 'columns':
parameters = re.findall(r"\[(\w+)\]", kwarg)
array_index = ''
for parameter in parameters:
pass_complete = False
if parameter.isdigit():
array_index = parameter
if parameter == 'data':
data = kwargs.get('columns[' + array_index + '][data]', "")
if parameter == 'orderable':
orderable = kwargs.get('columns[' + array_index + '][orderable]', "")
if parameter == 'searchable':
searchable = kwargs.get('columns[' + array_index + '][searchable]', "")
pass_complete = True
if pass_complete:
row = {'index': int(array_index),
'data': data,
'searchable': searchable,
'orderable': orderable}
column_parameters.append(row)
return sorted(column_parameters, key=lambda i: i['index'])
Here I'm extracting the fields for data, orderable and searchable, but I have specifically check for each one.
How do I write this so it iterates through all of the parameters and extracts them without me telling it which ones to extract? I'm pretty sure this is such a simple thing but I cannot figure it out.
My Python-foo is weak, assistance I need.
TIA
Last edited: