gleitz/howdoi — pull request #29 by zenoamaro (2013-01-12 04:47:40+00:00)
Title: Execute the last query again, or get the next answer

I've added two new options as shortcuts to execute the last query again, and one to get the next answer if the given one has been unsatisfying:

-g, --again: execute the last query again
-n, --next: display the next answer for the last query (implies -g)

To give an example:

$ howdoi python set env variable
import os
os.environ['DEBUSSY'] = '1'
...

$ howdoi --again
> howdoi -p 1 python set env variable
import os
...

$ howdoi staticmethod
>>> class DictSubclass(dict):
... def __repr__(self):
... return "DictSubclass"
...

$ howdoi --next
> howdoi -p 2 staticmethod
class MyClass(object):
@staticmethod
def the_static_method(x):
...

$ howdoi --again -p 5
> howdoi -p 5 staticmethod
class Person(object): #always inherit from object. It's just a good idea...
@staticmethod
def call_person():
print "hello person"
...

$ howdoi --next
> howdoi -p 6 staticmethod
...

`--again` can be mixed with `--pos` freely. `--next` automatically implies `--again` and is mutually exclusive with `--pos`. Giving a query while also specifying `--again` gives priority to the query (though unfortunately argparse doesn't support mutually exclusive groups containing both positionals and options).

Other things could be done, too, such as implying `--again` when using `--pos` without a query, but they'll come in time if they are useful.

The query is cached using a shelved dict in a `.howdoi.cache` dotfile in the home dir. The file path is chosen using `os.path.expanduser` so it should work on windows too. This cached shelf could also lead to caching of already answered queries too, together with a simple expiration mechanism, and maybe blacklists, ratings or bayesian learning (!).

There are a bit of other changes, but mainly changed whitespace as the code evolved, all pretty trivial. Also there aren't any tests in the codebase, but I'll think of adding some when I have time.