0

I am trying to mix and match everything, and it seems like I can't find the right answer.

I am trying to use filter on Queries. But the result is as follows: if q!=None, it generates a filter. But, if q==None, it would not display any data after using run()

q = self.request.get('q')
if q!=None:
  custQuery = db.Query(Customer)
  custQuery.filter('license = ',q)
elif q==None:
  custQuery = Customer.all()

But, if I replace all of them with just the following code below, it displays all the data.

custQuery = db.Query(Customer)

How do I display data if there is no q/query in an if-else statement?

Dan McGrath
  • 41,220
  • 11
  • 99
  • 130
Franz Noel
  • 1,820
  • 2
  • 23
  • 50
  • have you tried to print out the value of `q` when none of the `if` clauses match? – Rubens Jan 03 '13 at 23:48
  • I have tried to `self.out.response.write(q)`. It displays the right string for `q` (like I said, it filters). If `q==None`, it does not display anything when I `self.out.response.write(q)`. I don't know where `q` goes if I'm using print on gae. – Franz Noel Jan 03 '13 at 23:54
  • 1
    You should use `q is None` and `q is not None` instead of `q==None` and `q!=None`. It'll probably make no difference, but it's [good practice](http://www.python.org/dev/peps/pep-0008/#programming-recommendations). – John Lyon Jan 03 '13 at 23:54
  • Yes. I tried `q is None` and it did not work. Thank you for the advise about good practice. – Franz Noel Jan 03 '13 at 23:57
  • what happens if you simply evaluate it like: `if q: ... else: ...`? – Rubens Jan 03 '13 at 23:58
  • I did `if q!=None: a = "with query"` and `elif q is None: a = "without query"` with right indentions. I get a 500 server error. – Franz Noel Jan 04 '13 at 00:06
  • @FranzNoel and what happens if you do as I said in the former comment? – Rubens Jan 04 '13 at 00:06
  • When I did your first comment. The result is the same as `q is None`. It does not show the query run results.. – Franz Noel Jan 04 '13 at 00:12

2 Answers2

1

I got it!

When receiving request, here are the results below. If the query is '', it will display "with blank". If the query!=None, it displays "with query". But, if the query is None, this never displays because the fact that you are asking for a q/query will always mean that the q/query is blank.

if q is None:
  a = "without query"
  #custQuery = Customer.all()
elif q is '':
  a = "with blank"
elif q!=None:
  a = "with query"

Therefore, to solve the problem, once you receive the data, you declare that the query is None. Like this:

q = self.request.get('q')
if q is '':
  custQuery = db.Query(Customer)
elif q is None:
  q = '' #Force it! (Previously: q is '' - and it works.)
  custQuery = db.Query(Customer)
elif q!=None:
  custQuery = db.Query(Customer)
  custQuery.filter('license = ',q)

Solution: Force it to be 'blank'! All data will only display if the query is 'blank'.

Franz Noel
  • 1,820
  • 2
  • 23
  • 50
  • I don't think this line will do anything: `q is None #Force it!` – Alex L Jan 04 '13 at 07:35
  • Try it. It works for me in Python 2.7.3. By the way, I'm using json rest server. So, I call the data from the html which is connected to this rest server. – Franz Noel Jan 04 '13 at 07:36
  • `is` is used for comparison, not for assignment – Alex L Jan 04 '13 at 07:37
  • Well, thank for telling me. I'm a java guy. But I truly appreciate the constuctive criticism. It also works, though. Not sure why. – Franz Noel Jan 04 '13 at 07:39
  • No worries! You can read the docs on comparison operations [here](http://docs.python.org/2/reference/expressions.html#not-in) – Alex L Jan 04 '13 at 07:40
  • You got me thinking now. It may have been that Guido used it both ways. Because, since python is a high end programming language, `is` may have 2 purposes. Both `==` and `=`. Because both are known to be "is" in the English language. Really weird, in my program, it works. But on the shell, it's an operand. But, I think this is another topic. – Franz Noel Jan 04 '13 at 07:45
  • There's a SO question on `is` that might help too: http://stackoverflow.com/questions/2438667/what-is-the-semantics-of-is-operator-in-python – Alex L Jan 04 '13 at 07:47
1

When you use self.request.get('q'), the default value if 'q' doesn't exist is None. You can test this with a simple dictionary:

>>> d = {'q': 'message'}
>>> print d.get('q')
'message'
>>> d = {'a': 'no q here'}
>>> print d.get('q')
None

Therefore, if you're checking if 'q' is in your request dictionary, you should compare against None (if q is None:).

If you're comparing to see if your query is blank, you could do a few things:

>>> query = ''
>>> print query is None
False
>>> print query == ''
True
>>> print len(query) > 0
False
>>> print bool(query) 
False

>>> query = 'valid'
>>> print query is None
False
>>> print query == ''
False
>>> print len(query) > 0
True
>>> print bool(query)
True

You can use repr() to see what the result actually is: (in this case a blank string)

>>> print repr(d.get('q', ''))
''

Therefore, since self.request.get('q') defaults to '', I would do the following:

q = self.request.get('q')

if q is '': # Blank or non-existent
  custQuery = Customer.all()

else: # Non-blank
  custQuery = db.Query(Customer)
Alex L
  • 8,748
  • 5
  • 49
  • 75
  • We are talking about 'python and gae' not just python, though. Check for yourself the `rev-5` about this problem. This solution above, does not work. You can download the real codes at: https://github.com/fritzdenim/pawnsoftware.git – Franz Noel Jan 04 '13 at 07:58
  • Ah I found the problem! `self.request` isn't a `dict`, it's a GAE Request object. The `.get()` function default is `''`, not `None`. See the [docs](https://developers.google.com/appengine/docs/python/tools/webapp/requestclass#Request_get) – Alex L Jan 04 '13 at 08:02
  • self.request is a dict. The q (inside var search of javascript) has been serialized to become a dict. The main files that you need are templates/index.html and resources.py. – Franz Noel Jan 04 '13 at 08:06
  • Are you sure? Can you print `type(self.request)`? – Alex L Jan 04 '13 at 08:07
  • Where should I type it? But, yes, I'm 90% sure (after you made me doubt my works) :) – Franz Noel Jan 04 '13 at 08:08
  • [L36](https://github.com/fritzdenim/pawnsoftware/blob/master/pawnsoftware-0.0.1/resources.py#L36)? You could also try printing `repr(q)`, to see if it's `''` or `None` (but I think it'll be `''`) – Alex L Jan 04 '13 at 08:10
  • Yes, it runs fine. i still have the json results. No errors or exceptions. What output should you expect? Gives the following output `` – Franz Noel Jan 04 '13 at 08:12
  • 1
    Cool - that means that self.requests is a [Request object](http://webapp-improved.appspot.com/guide/request.html), so you should expect `''` rather than `None`. All sorted! :) – Alex L Jan 04 '13 at 09:04