0

How would I go about grabbing the values from the URL, a and b, then passing them to the add function? The result should come out to be: {"c": x}, where x is the sum of a and b.

The code:

op = 'add'
a = random.randint(0,10)
b = random.randint(0,10)
/%s?a=%s&b=%s' % (op, a, b)

result = res.json()

if op=='add':
    assert a+b == result['c']

The function:

def add():
    import json
    return json.dumps({'c': ???})
the1
  • 47
  • 1
  • 8
  • Possible duplicate of [URL parsing in Python - normalizing double-slash in paths](http://stackoverflow.com/questions/8925938/url-parsing-in-python-normalizing-double-slash-in-paths) – Serdmanczyk Apr 12 '16 at 03:57
  • Not at all, @Serdmanczyk, since the user doesn't even know about `urlparse`, apparently... – icedwater Apr 12 '16 at 03:58
  • 1
    Sorry, was trying to find a decent duplicate answer and accidentally hit enter on the wrong one. Is there a way to take back flags? Answer below makes sense. – Serdmanczyk Apr 12 '16 at 03:59
  • This might be a good time to look through meta.stackoverflow.com for posts like http://meta.stackoverflow.com/questions/252155/add-ability-to-cancel-flags :) – icedwater Apr 12 '16 at 04:01
  • Wow! Thanks for the overwhelming response! Your comments are appreciated, but none were what I was looking for. I filtered out a lot of the codes, so I guess my question altered. I just edit my question.. This should make sense now. – the1 Apr 12 '16 at 04:33

3 Answers3

1

Use urlparse, a standard library module designed for tasks like these!

urlparse.parse_qs(qs[, keep_blank_values[, strict_parsing]])

Parse a query string given as a string argument (data of type application/x-www-form-urlencoded). Data are returned as a dictionary. The dictionary keys are the unique query variable names and the values are lists of values for each name.

The optional argument keep_blank_values is a flag indicating whether blank values in percent-encoded queries should be treated as blank strings. A true value indicates that blanks should be retained as blank strings. The default false value indicates that blank values are to be ignored and treated as if they were not included.

The optional argument strict_parsing is a flag indicating what to do with parsing errors. If false (the default), errors are silently ignored. If true, errors raise a ValueError exception.

Example:

>>> urlparse.parse_qs('a=1&b=1') # raw query string
{'a': ['1'], 'b': ['1']}

Note that you can parse an entire URL into its components (including a query string) using other functions in urlparse as well.

Akshat Mahajan
  • 9,543
  • 4
  • 35
  • 44
  • Do you have an example of how this can be used? If you feel this might be a sort of "do my homework for me" question, feel free to point it out, but answering should be done properly as well. – icedwater Apr 12 '16 at 03:57
  • 1
    @icedwater I was in the process of adding an example right as you commented. :) – Akshat Mahajan Apr 12 '16 at 03:57
  • Thanks! I think this is a good hint without giving the answer outright. – icedwater Apr 12 '16 at 03:59
0
from urlparse import parse_qs, urlparse
import json

def add(a, b):
    return json.dumps({'c': a+b})

url = 'add?a=1&b=1'

q_dict = parse_qs(urlparse(url).query, keep_blank_values=True)
# q_dict = {'a': ['1'], 'b': ['1']}

print add(a=q_dict['a'][0], b=q_dict['b'][0])
Mani
  • 933
  • 6
  • 15
0

I figured out how to solve my problem! I appreciate all the help, but none of the solutions provided here applied to my question. This is entirely my fault because I worded the question totally different.

the1
  • 47
  • 1
  • 8