Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

KeyError if parameter is empty #83

Closed
billdesk opened this issue Jan 10, 2017 · 3 comments
Closed

KeyError if parameter is empty #83

billdesk opened this issue Jan 10, 2017 · 3 comments

Comments

@billdesk
Copy link

import os
uri = os.environ['REQUEST_URI']

from furl import furl
f = furl(uri)
print f.args['product']
print f.args['category']

If current uri is /product.py?product=12&category=2

Then it prints 12 and 2

But if current uri is /product.py?product=12
it throws KeyError

@gruns
Copy link
Owner

gruns commented Jan 12, 2017

Unless I misunderstand you, this is the expected behavior of furl.args.

furl.args is a dictionary (an orderedmultidict to be specific), and just like a
standard Python dictionary, KeyError is raised on a missing key.

>>> d = {1:1}
>>> d[2]
Traceback (most recent call last):
  ...
KeyError: 2
>>>
>>> from furl import furl
>>> f = furl('http://www.google.com/?1=1')
>>> dict(f.args)
{'1': '1'}
>>> f.args[2]
Traceback (most recent call last):
  ...
KeyError: 2

To avoid raising a KeyError on a missing key, use the get() method, just like
the get() method of standard Python dictionaries.

>>> from furl import furl
>>> f = furl('http://www.google.com/?1=1')
>>> dict(f.args)
{'1': '1'}
>>> print(f.args.get(2))
None

Or, in your example

import os
uri = os.environ['REQUEST_URI']

from furl import furl
f = furl(uri)
print f.args.get('product')
print f.args.get('category')

Does that answer your question?

@gruns
Copy link
Owner

gruns commented Jan 22, 2017

Little bump -- does my previous question answer your question, @billdesk?

@gruns
Copy link
Owner

gruns commented Jan 25, 2017

Closing due to inactivity.

Please reopen if your query was unanswered above.

@gruns gruns closed this as completed Jan 25, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants