首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > perl python >

python的hash计算,不懂解决办法

2012-03-17 
python的hash计算,不懂Python codedef tesHash(fileName):Returns tes4s two hash values for filenam

python的hash计算,不懂

Python code
def tesHash(fileName):    """Returns tes4's two hash values for filename.    Based on TimeSlips code with cleanup and pythonization."""    root,ext = os.path.splitext(fileName.lower()) #--"bob.dds" >> root = "bob", ext = ".dds"    #--Hash1    chars = map(ord,root) #--'bob' >> chars = [98,11,98]    hash1 = chars[-1] | (0,chars[-2])[len(chars)>2]<<8 | len(chars)<<16 | chars[0]<<24    #--(a,b)[test] is similar to test?a:b in C. (Except that evaluation is not shortcut.)    if   ext == '.kf':  hash1 |= 0x80    elif ext == '.nif': hash1 |= 0x8000    elif ext == '.dds': hash1 |= 0x8080    elif ext == '.wav': hash1 |= 0x80000000    #--Hash2    #--Python integers have no upper limit. Use uintMask to restrict these to 32 bits.    uintMask, hash2, hash3 = 0xFFFFFFFF, 0, 0     for char in chars[1:-2]: #--Slice of the chars array        hash2 = ((hash2 * 0x1003f) + char ) & uintMask    for char in map(ord,ext):        hash3 = ((hash3 * 0x1003F) + char ) & uintMask    hash2 = (hash2 + hash3) & uintMask    #--Done    return (hash2<<32) + hash1 #--Return as uint64


这是一段计算值文件名hash的代码,小弟初学python实在是看不懂,哪位大哥能帮我看看是什么意思么,如果可以的话,写个伪代码或者C代码都行,上面的根据map函数啊,(0,chars[-2])[len(chars)>2]<<8是什么意思都不清楚。

[解决办法]
你的问题在注释里都有回答了:

1. map: chars = map(ord,root) #--'bob' >> chars = [98,11,98]

map(function, list) = [function(list[0]), function(list[1]), ...]

2. 第二个问题相关的代码行下有注释:

#--(a,b)[test] is similar to test?a:b in C. (Except that evaluation is not shortcut.)
[解决办法]
探讨

还有python的优先级是什么呢?先移位后与或非,还是先与或非再移位?

[解决办法]
探讨

还有python的优先级是什么呢?先移位后与或非,还是先与或非再移位?

热点排行