Python string 模块,rjust() 实例源码

我们从Python开源项目中,提取了以下2个代码示例,用于说明如何使用string.rjust()

项目:chuck    作者:Calysto    | 项目源码 | 文件源码
def hexDump(bytes):
    """Useful utility; prints the string in hexadecimal"""
    for i in range(len(bytes)):
        sys.stdout.write("%2x " % (ord(bytes[i])))
        if (i+1) % 8 == 0:
            print(repr(bytes[i-7:i+1]))

    remainder = len(bytes) % 8
    if(remainder != 0):
        print(string.rjust("", (8-remainder)*3 - 1), 
              repr(bytes[i-len(bytes)%8:i+1]))
项目:python2-tracer    作者:extremecoders-re    | 项目源码 | 文件源码
def browse_menu(selector, host, port):
    list = get_menu(selector, host, port)
    while 1:
        print '----- MENU -----'
        print 'Selector:', repr(selector)
        print 'Host:', host, ' Port:', port
        print
        for i in range(len(list)):
            item = list[i]
            typechar, description = item[0], item[1]
            print string.rjust(repr(i+1), 3) + ':', description,
            if typename.has_key(typechar):
                print typename[typechar]
            else:
                print '<TYPE=' + repr(typechar) + '>'
        print
        while 1:
            try:
                str = raw_input('Choice [CR == up a level]: ')
            except EOFError:
                print
                return
            if not str:
                return
            try:
                choice = string.atoi(str)
            except string.atoi_error:
                print 'Choice must be a number; try again:'
                continue
            if not 0 < choice <= len(list):
                print 'Choice out of range; try again:'
                continue
            break
        item = list[choice-1]
        typechar = item[0]
        [i_selector, i_host, i_port] = item[2:5]
        if typebrowser.has_key(typechar):
            browserfunc = typebrowser[typechar]
            try:
                browserfunc(i_selector, i_host, i_port)
            except (IOError, socket.error):
                print '***', sys.exc_type, ':', sys.exc_value
        else:
            print 'Unsupported object type'

# Browse a text file