def pythonise ( id , encoding = 'ascii' ) : replace = { '-' : '_' , ':' : '_' , '/' : '_' } func = lambda id , pair : id . replace ( pair [ 0 ] , pair [ 1 ] ) id = reduce ( func , replace . iteritems ( ) , id ) id = '_%s' % id if id [ 0 ] in string . digits else id return id . encode ( encoding )
python invalid charater in identifer
Return a Python - friendly id
def pythonise(id, encoding='ascii'):
    """Return a Python-friendly id"""
    replace = {'-': '_', ':': '_', '/': '_'}
    func = lambda id, pair: id.replace(pair[0], pair[1])
    id = reduce(func, replace.iteritems(), id)
    id = '_%s' % id if id[0] in string.digits else id
    return id.encode(encoding)