帮助记忆快捷键的小程序

完成了一个帮助记忆各种快捷键的小程序,有了这个每次学习到新的快捷键的时候就可以记下来了,以后忘记了可以直接查,不用再到网上找了,多好多好!

用python的shelve当数据库进行简单k-v存储,实现了一个简单的查询功能,每次输出最匹配的5个结果。
使用方法是,copy该代码存入shortcut.py,chmod a+x增加可执行权限,按自己的需求修改一下代码里面的用于存储db的storegePath,最后把它扔到/usr/local/bin。

每次的用法为:

usage:
    shortcut.py a/add <key> <a brief description>
        add item
    shortcut.py s/search <search key words>
        search item
    [for debug]
    shortcut.py l/list
        print the current shortcut list
usage example:
    shortcut.py a/add dd vim delete
    shortcut.py s/search vim del

一些测试结果:

zhoutall@ubuntu ~> shortcut.py l
*******************
ctrl-v --> paste
dd --> vim delete
p --> vim paste
yy --> vim copy
cd --> linux change dir
ctrl-a --> select all
ctrl-c --> copy
mv --> linux move
*******************
zhoutall@ubuntu ~> shortcut.py a su linux change user
*******************
add item: su --> linux change user
*******************
zhoutall@ubuntu ~> shortcut.py s pas
*******************
ctrl-v --> paste

p --> vim paste
ctrl-c --> copy
yy --> vim copy
mv --> linux move
*******************
zhoutall@ubuntu ~> shortcut.py s vi pas
*******************
p --> vim paste

ctrl-v --> paste
yy --> vim copy
dd --> vim delete
ctrl-c --> copy
*******************

程序代码(不保证没有意料之外的bug):

#!/usr/bin/env python
#coding: utf-8
import shelve
import sys
import heapq

storegePath = '/home/zhoutall/privateStore/shortcut.db'

def add_shortcut(key, stmtlist):
    if stmtlist == []:
        print 'no shortcut description'
        sys.exit(-1)
    try:
        db = shelve.open(storegePath, 'c')
    except Exception as e:
        print e
        print 'fail to open/create shortcut db'
        sys.exit(-1)
    try: 
        db[key] = stmtlist + ['; '] + db[key]
        print 'add item: %s --> %s' % (sys.argv[2], ' '.join(sys.argv[3:]))
    except KeyError:
        db[key] = stmtlist
        print 'add item: %s --> %s' % (sys.argv[2], ' '.join(sys.argv[3:]))
    except Exception as e:
        print e
        print 'fail to insert shortcut, exit'
    finally:
        db.close()

def get_similarity(a, b):
    bigrams_a = set()
    bigrams_b = set()
    for i in range(len(a)-1):
        bigrams_a.add(a[i: i+2])
    for i in range(len(b)-1):
        bigrams_b.add(b[i: i+2])
    count = len(bigrams_a & bigrams_b)
    modify = 1.0/len(bigrams_b)
    return count+modify

def matcherRes(qlist, shelveDB, topn):
    reslist = []
    for key, stmtlist in shelveDB.iteritems():
        sim = get_similarity(''.join(qlist), ''.join(stmtlist))
        if sim != 0:
            reslist.append((sim, key, ' '.join(stmtlist)))
    return heapq.nlargest(topn, reslist)

def search_shortcut(qlist):
    try:
        db = shelve.open(storegePath, 'r')
    except:
        print 'fail to open shortcut db'
        sys.exit(-1)
    try:
        results = matcherRes(qlist, db, 5)
        for i, item in enumerate(results):
            print item[1], '-->', item[2]
            if i == 0:
                print ''
        if not results:
            print 'Nothing matches'
    except Exception as e:
        print e
        print 'Error when fetching data from db'
    finally:
        db.close()

def list_shortcut():
    try:
        db = shelve.open(storegePath, 'c')
    except Exception as e:
        print e
        print 'fail to open shortcut db'
        sys.exit(-1)
    try:
        for key, stmt in db.items():
            print key, '-->', ' '.join(stmt)
        if not db.items():
            print 'Nothing in shortcut db'
    except Exception as e:
        print e
        print 'Error when fetching data from db'
    finally:
        db.close()

def main():
    """
    usage:
    shortcut.py a/add <key> <a brief description>
    shortcut.py s/search <search key words>
    [for debug]
    shortcut.py l/list: print the current shortcut list
    usage example:
    shortcut.py a/add dd vim delete
    shortcut.py s/search vim delete
    """
    if len(sys.argv) < 2:
        sys.exit(-1)
    print '*******************'
    if sys.argv[1].lower() in ['a', 'add']:
        add_shortcut(sys.argv[2], sys.argv[3:])
    elif sys.argv[1].lower() in ['s', 'search']:
        search_shortcut(sys.argv[2:])
    elif sys.argv[1].lower() in ['l', 'list']:
        list_shortcut()
    else:
        print 'invalid command'
        sys.exit(-1)
    print '*******************'

if __name__ == '__main__':
    main()
Tags :

One thought on “帮助记忆快捷键的小程序”

发表评论

电子邮件地址不会被公开。 必填项已用*标注

Click the right image To submit your comment: