Mark
Mark

Reputation: 2135

Correct way to transform python request parameters

I'm using Pyramid web framework to build a web app. There are many times that I find myself doing this:

result = request.params.get('abc', None)
if result:
  result = simplejson.loads(result)
else:
  result = {}

The thing is, sometimes, 'abc' request parameter is not present and the value of "result" would be None. Hence I always have to check if it's None before I perform a simplejson.loads operation or else I would get a TypeError: expected string or buffer exception.

Is there a better/more "pythonic" way of doing this?

Upvotes: 1

Views: 88

Answers (1)

MostafaR
MostafaR

Reputation: 3695

Try this:

result = simplejson.loads(request.params.get('abc', '{}'))

Upvotes: 3

Related Questions