Export to GitHub

hotwire-shell - issue #202

uniq builtin


Posted on Jun 27, 2008 by Happy Kangaroo

A uniq builtin would often be useful. I've written one.

  • It only works on strings as it uses a dictionary internally which needs immutable types as keys.
  • It can use a string from a property, in which case the property string is passed on, not the whole object.
  • It can count (-c option) the instances. In this case the output is CountedObject (maybe this should be renamed) and there is a corresponding renderer.

So, if history is made to work again (issue #192) then we could do

history | uniq -c | sort -r count | head

instead of writing a 10 line script ;-)

Attachments

Comment #1

Posted on Jun 27, 2008 by Happy Kangaroo

The history pipe should of course be

history | py-map "it.split()[0]" | uniq -c | sort -r count | head

Comment #2

Posted on Jun 29, 2008 by Happy Lion

CountedObject seems a bit overkill to me; can we just return a list of tuples?

The dictionary should also work on immutable sets, tuples, an integers too, no? So the documentation should basically say that only immutable data types can be used.

Also you should probably change the copyright on the file to yourself and not my name since it's a nontrivial patch (> 10 lines).

Thanks for the patch! If you agree with all the above feel free to go ahead and commit.

Comment #3

Posted on Jul 4, 2008 by Happy Kangaroo

We can return a list of tuples, but the display is not so good that way - have a play and see what you think. I've changed it to a tuple for now. I can always change it again later. And I've changed the copyright and doc string.

Sending hotwire/builtin.py Adding hotwire/builtins/uniq.py Sending hotwire/test_command.py Transmitting file data ... Committed revision 1282.

Status: Fixed

Labels:
Type-Enhancement Priority-Medium