lukasschwab / stackit

StackOverflow queries from the command line
MIT License
314 stars 28 forks source link

Added response when search query returns no SO items #12

Closed grobbie94 closed 9 years ago

grobbie94 commented 9 years ago

@lukasschwab - Screwed up the last rebase so reforked. Here's the functionality recommitted when the search returns no results.

lukasschwab commented 9 years ago

@grobbie94 I think I can simplify this. I talked to the rest of the SB Hacks team, and we would rather an empty query wouldn't drop the user into an interface. This makes it one step more difficult to open the queries in a browser, but if they return no results they will have to be modified anyway.

So, instead of the current functionality, stackit should just return the error message and the query info:

if len(questions) == 0:
    click.echo(
        click.style("Your search \'{0}\' with tags \'{1}\' returned no results.".format(config.term,config.tag),
        fg="red"))
    sys.exit()
WnP commented 9 years ago

no needs to use sys.exit() this is the script's end ;-)

using if not questions is more pythonic than if len(questions) == 0

grobbie94 commented 9 years ago

Fixed it up, thanks for the pointers guys. @WnP is there any advantage to using if not rather than if len(foo) == 0 or is it just convention?

lukasschwab commented 9 years ago

Tested, looks good. Merging!

grobbie94 commented 9 years ago

Cheers!

WnP commented 9 years ago

@grobbie94 yes there's some advantage to use if not foo rather than if len(foo) == 0, lets see this simple benchmark test using timeit module:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
from timeit import timeit

foo = [x for x in range(10000)]

def main():
    """benchmark"""

    print('test using `if len(list) == 0`:')
    print(timeit("if len(foo) == 0: pass", "from __main__ import foo", number=10000))

    print('test using `if not list`:')
    print(timeit("if not foo: pass", "from __main__ import foo", number=10000))

if __name__ == '__main__':
    main()

on my laptop using Python 2.7.9 it outputs:

test using `if len(list) == 0`:
0.00260186195374
test using `if not list`:
0.000842809677124

so if not foo is 3 times faster

and using Python 3.4.2+ it outputs:

test using `if len(list) == 0`:
0.0037013280000337545
test using `if not list`:
0.0006296139999903971

it's close to 6 times faster!