Python 板


LINE

參考了 http://www.kix.in/blog/2009/12/goo-gl/ 這裡面的作法 寫了一個 Python 版本 - http://gist.github.com/347719 另外有個問題想請教一下... Javascript 作位元運算的時候會轉換成 Signed 32-bit integers 還有二的補數問題... 可是在 Python(我用2.6) 裡面超出 32-bit 他就會自己把 int 轉成 long... 所以我才自己寫了 function __b() 去強制把超出 32-bit 的去掉 和判斷補數... 我想問的是 有沒有更好的寫法 ?? =============== 順便貼上 code 的分隔線 ============= import urllib import re # decimal to binary __a = lambda s: str(s) if s<=1 else __a(s>>1) + str(s&1) # Bitwise Operators : Signed 32-bit integers # https://developer.mozilla.org/En/Core_JavaScript_1.5_Reference/Operators/Bitwise_Operators#Signed_32-bit_integers def __b(d): foo, bar = (d < 0) and (__a(2**32 - d), -1) or (__a(d), 1) return bar * (foo[-32:-31] == '1' and [-(2 ** 31 - int(foo[-31:], 2))] or [int(foo[-32:], 2)])[0] # stolen from toolbar.js in the Google Toolbar extension for Firefox def __c(*args): l = 0 for arg in args: l += __b(arg & 4294967295) return __b(l) def __d(l): m = l = str(l > 0 and l or l + 4294967296) o = 0 n = False for p in m[::-1]: q = int(p) if n: q *= 2 o += q / 10 + q % 10 else: o += q n = not n m = int(o) % 10 o = 0 if m != 0: o = 10 -m if len(l) % 2 == 1: if o % 2 == 1: o += 9 o /= 2 m = str(o) m += l return m def __e(l): m = 5381 for o in l: m = __c(__b(m << 5), m, ord(o)) return m def __f(l): m = 0 for o in l: m = __c(ord(o), __b(m << 6), __b(m << 16), -m); return m def __g(b): i = __e(b) i = i >> 2 & 1073741823; i = i >> 4 & 67108800 | i & 63; i = i >> 4 & 4193280 | i & 1023; i = i >> 4 & 245760 | i & 16383; j = '7' h = __f(b) k = __b((i >> 2 & 15) << 4) | h & 15 k |= __b((i >> 6 & 15) << 12) | __b((h >> 8 & 15) << 8) k |= __b((i >> 10 & 15) << 20) | __b((h >> 16 & 15) << 16) k |= __b((i >> 14 & 15) << 28) | __b((h >> 24 & 15) << 24) j += __d(k) return '[email protected]&url=%s&auth_token=%s' % (urllib.quote_plus(b), j) def googl(url): api_url = 'http://goo.gl/api/url' res = urllib.urlopen(api_url, __g(url)).read() return re.search(r'http:/\/goo.gl/\w+', res).group(0) # for test print googl('http://www.google.com/') # http://goo.gl/fbsS print googl('https://www.youtube.com/') # http://goo.gl/NlKL --



※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 124.137.27.2







like.gif 您可能會有興趣的文章
icon.png[問題/行為] 貓晚上進房間會不會有憋尿問題
icon.pngRe: [閒聊] 選了錯誤的女孩成為魔法少女 XDDDDDDDDDD
icon.png[正妹] 瑞典 一張
icon.png[心得] EMS高領長版毛衣.墨小樓MC1002
icon.png[分享] 丹龍隔熱紙GE55+33+22
icon.png[問題] 清洗洗衣機
icon.png[尋物] 窗台下的空間
icon.png[閒聊] 双極の女神1 木魔爵
icon.png[售車] 新竹 1997 march 1297cc 白色 四門
icon.png[討論] 能從照片感受到攝影者心情嗎
icon.png[狂賀] 賀賀賀賀 賀!島村卯月!總選舉NO.1
icon.png[難過] 羨慕白皮膚的女生
icon.png閱讀文章
icon.png[黑特]
icon.png[問題] SBK S1安裝於安全帽位置
icon.png[分享] 舊woo100絕版開箱!!
icon.pngRe: [無言] 關於小包衛生紙
icon.png[開箱] E5-2683V3 RX480Strix 快睿C1 簡單測試
icon.png[心得] 蒼の海賊龍 地獄 執行者16PT
icon.png[售車] 1999年Virage iO 1.8EXi
icon.png[心得] 挑戰33 LV10 獅子座pt solo
icon.png[閒聊] 手把手教你不被桶之新手主購教學
icon.png[分享] Civic Type R 量產版官方照無預警流出
icon.png[售車] Golf 4 2.0 銀色 自排
icon.png[出售] Graco提籃汽座(有底座)2000元誠可議
icon.png[問題] 請問補牙材質掉了還能再補嗎?(台中半年內
icon.png[問題] 44th 單曲 生寫竟然都給重複的啊啊!
icon.png[心得] 華南紅卡/icash 核卡
icon.png[問題] 拔牙矯正這樣正常嗎
icon.png[贈送] 老莫高業 初業 102年版
icon.png[情報] 三大行動支付 本季掀戰火
icon.png[寶寶] 博客來Amos水蠟筆5/1特價五折
icon.pngRe: [心得] 新鮮人一些面試分享
icon.png[心得] 蒼の海賊龍 地獄 麒麟25PT
icon.pngRe: [閒聊] (君の名は。雷慎入) 君名二創漫畫翻譯
icon.pngRe: [閒聊] OGN中場影片:失蹤人口局 (英文字幕)
icon.png[問題] 台灣大哥大4G訊號差
icon.png[出售] [全國]全新千尋侘草LED燈, 水草

請輸入看板名稱,例如:Gossiping站內搜尋

TOP