python: 生成guid

其实经常需要生成一个guid,在各种场合使用。。。也简单写个小脚本实现吧。

实现下来发现速度比较慢...

import uuid
import sys

def show_ver():
    print 'guid generator v1.0, by hydonlee'

def show_usage():
    show_ver()
    print '''generate a new guid and printit in stdout.

Usage:
    newguid [option]
    
    options:
    -v      get the version infomation
    -h      show the help infomation like this
    -u,-U   show guid in uppercase, default is uppercase
    -l,-L   show guid in lowercase
'''

if __name__ == "__main__":
    isUpper = True
    act  = 'uuid'
    
    if '-u' in sys.argv: isUpper = True
    if '-U' in sys.argv: isUpper = True
    if '-l' in sys.argv: isUpper = False
    if '-L' in sys.argv: isUpper = False
    if '-v' in sys.argv: act = 'ver'
    if '-h' in sys.argv: act = 'help'

    if (act == 'uuid'):
        result = str(uuid.uuid1())
        if isUpper: print result.upper()
        else: print result
    elif (act == 'ver'):
        show_ver()
    elif (act == 'help'):
        show_usage()
        
原文地址:https://www.cnblogs.com/hydonlee/p/5474708.html