Home

June 2008

S M T W T F S
1234567
891011121314
15161718192021
22232425262728
2930     
Powered by LiveJournal.com

Cute..

I see from this history meme that a lot of people are still using prehistoric shells! Low numbers like 303 or even 24 total times for top execution? Now, my shell knows how to store data properly, so for example it doesn't toss away your entire session if you don't exit it cleanly, and has scaled so far with near-instant searches for a 20000+ history size.


My code:


import os,sys,sqlite3
c = sqlite3.connect(os.path.expanduser('~/.hotwire/state/history.sqlite'))
cmds = map(lambda x: x[0].split(' ', 1)[0], c.execute('SELECT cmd FROM Commands'))
stats = {}
for cmd in cmds:
  count = stats.get(cmd, 0)
  stats[cmd] = count+1
cmds = stats.keys()
cmds.sort(lambda a,b: cmp(stats[b],stats[a]))
for cmd in cmds:
  print "%s: %s" % (cmd, stats[cmd])

Output:


cd: 2280
ls: 2003
ed: 1823
git: 1362
python: 1187
make: 817
svn: 751
sudo: 669
py: 543
cat: 442
rpm: 380
rm: 370
less: 366
cvs: 306

Comments

(Anonymous)

A technicality

Well technically your shell doesn't fit the definition of a shell.

App that simulates a shell, perhaps.

Also, the shell code is much shorter and suitable for command line copy and paste :)

Re: A technicality

Well, what is a shell? I'd say that a shell is an interactive environment for control of the operating system. And Hotwire most definitely fits that definition.

Now, what Hotwire is not is a terminal emulator; if you want to get warped back a window to the late 1970s, Hotwire can embed VTE for that =)

(Anonymous)

Re: A technicality

Something I can put in /etc/passwd is a shell :)


You could shorten that code by doing more with the SQL. Something like "SELECT cmd, count(*) from Commands GROUP BY cmd ORDER BY count(*) DESC".
Well, you'd need to do the split(' ', 1) inside SQL to make that work though, right? The cmd is the full command like "git commit", not just "git".

The code could definitely be improved...here's a version which avoids an intermediate list (might still be a better way to do it):

import os,sys,sqlite3
c = sqlite3.connect(os.path.expanduser('~/.hotwire/state/history.sqlite'))
stats = {}
for result in c.execute('SELECT cmd FROM Commands'):
  cmd = result[0].split(' ', 1)[0]
  count = stats.get(cmd, 0)
  stats[cmd] = count+1
cmds = stats.keys()
cmds.sort(lambda a,b: cmp(stats[b],stats[a]))
for cmd in cmds:
  print "%s: %s" % (cmd, stats[cmd])

(Anonymous)

prehistoric

ed and cvs? that's what I call prehistoric ;-)

(Anonymous)

try

cmds = stats.items()
cmds.sort(key=operator.itemgetter(1), reverse=True)

or if you are still on python2.3,

cmds = [(a,b) for b,a in stats.items()]
cmds.sort()
cmds.reverse()
Ah, that is nicer. I didn't know about operator.itemgetter.

(Anonymous)

A rather interesting list for once! But..

ed: 1823

Now that is just plain sick. :)

on about: what is a shell

I'd suggest you can call hotwire a shell ..
when it takes the meme script and makes it output your output :)