想法
利用marshal把python bytecode轉成string, 就可以pickled.
把Function轉成Pickled string
1 2 3 4 5 6 7 8 | |
把Pickled string轉成function
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | |
利用marshal把python bytecode轉成string, 就可以pickled.
把Function轉成Pickled string
1 2 3 4 5 6 7 8 | |
把Pickled string轉成function
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | |
I found my chromium in Ubuntu 11.10 can not play mp4 video. after I googling, the solution is to install chromium-codecs-ffmpeg-extra to solve these problems.
$ apt-get install chromium-codecs-ffmpeg-extra
I already use lp-cli to generate reports about one month and think this may benefit someone has the same requirmentation. so here you are.
This document will show
comments are welcome.
Singleton 這東西大家想必不陌生,在Python裡實做的方式滿多, 這邊用這個當例子介紹 Metaclass 可以做什麼。
概念上很簡當, 讓Class的建構子不能產生Instance, 然後再提供一個
Classmethod 能夠取得Instance(而且只能有一個), 在Python你可以用id來檢查兩個物件
是不是相同的。
原則上Singleton我只會用在不用花腦袋就會覺得這個class建出來的instance這個程式執行 時只能有一個。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | |
把你想變成Singleton class 的 metaclass 設成 Singleton 就可以了, 後悔的話,把那一行註解起來,這個class就不是Singleton。
要注意的地方是這class的Singleton特性是可以被繼承的,但這也是為什麼我喜歡用 這種方式的原因,另一個好處是Class的功能跟Design Pattern的耦合度會比較低。 缺點就是會遇到metaclass衝突的狀況,但也不是不能解決。
1 2 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | |
從兩三年前開始,就不斷有人在喊”那會有Python Conference 嗎”?
尤其是去年有Ruby Con TW 2011, PHP Conf TW 2011…
終於,前陣子PyCon TW 2012 官方網站公佈啦! 活動預計在2012 年 6月9日(六)及10日(日) 假中研院人文館舉辦, 目前正在統計可能參與人數, 請幫忙填問卷
用Python的人應該是不少才對, 大家不要在潛水了!
這篇文章提到Template因為用到了Metaclass, 因此改Class或是Instance的delimiter是沒作用的, 因為Template的pattern atrtibute 在 class 建構時, 就已經決定好, 於是你只能建立一個class 來繼承他, 把Template.delimiter Overwrite掉, 這樣 Template().delimter 才是你要的, 可我實在懶得為了改delimiter就去寫一個自定義class, 原文的方式我也覺得有點麻煩, 因為要組出完整的pattern
基本上這很好解, 就runtime時產生一個自定義class就好了 :p
1 2 3 4 5 6 7 8 9 10 | |
最近在寫lp-cli時, 因為stdout已經用來傳遞pickle data到下一個command, 例子如下
# 找出ossug-hychen' 正在處理幾個bug
$ lp-searchbugs people ossug-hychen --status 'In Progress' | lp-print count
所以如果我想要print 一些資訊到terminal 其實是辦不的, 昨日在H4時與Thinker討論時, 他提出了一個辦法
如果要在輸出已被redirect 到時, 想要把字串印到Terminal,可以把訊息寫到 /dev/tty (等於目前的 control terminal)
1 2 3 4 5 6 7 | |
但是這方法仍有缺點:
台北辦公室擴建完畢後, 內部在詢問會議室的命名意見,命名規則很簡單。要有台灣特色且容易翻譯。 於是想當然爾眾人的提案不外乎高山、景點、小吃之類的。
因會議室剛好有午間,有同事提議用台灣五院來命名,而且這樣以後辦公室會有下列有趣的對話….
同事A: 等下在哪開會
同事B: 立法院
當然這個提案,不是很被多數人認可,因為英文名字有點太長…
而我自己的提案是台灣高山湖, 這些湖的共同點就是交通不便,需要親力親為上山後方能領略美景。 至於象徵意含嘛… 我沒想這麼多。
五個會議室名稱分別如下
嘉明湖 - 海拔3310/11公尺, 素有天使的眼淚之稱 七彩湖 - 海拔2980公尺
松蘿湖 - 海拔1300公尺, 素有「十七歲之湖」及「夢幻之湖」之稱
七彩湖 - 海拔2980公尺
白石池 - 海拔2770公尺
翠池 - 海拔3520公尺 , 台灣最高的高山湖泊
註:我還有另一個提案是用台灣36秘境中的5個來命名, 不過目前比較偏好用高山湖泊命名
The web interface of Launchpad is awesome, but it does not provide a function that you can save your search result, or even create a custom bug display list, I believe managers would like this function, especially they want to track each team member status.
I am just a engineer but also have almost the same requirement, here are 3 questions I need to answer in the end of weekend.
It is better to have a way to always show bugs I am interesting for different purpose, so I start to write a script by using python-launchpadlib.
by the way, another needs is to find bugs and modify attribute(s) of each bug.
Launchpad has a very friendly python library, I will not talk this too much, if you want to know the details of it, please check these websites.
lp-cli include several tools which is very small and has only one trivial purpose, that is users can use Unix pipeline to combine small tools to archive a complex task.
All work of process data of Launchpad can be simplified as the following
Create a text file contains bugs I worked is modified this week.
1 2 3 4 | |
the view modified_thisweek means the bug worked by anyone since 7 days before as defined here
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | |
so if you want to know which bugs is really worked by yourself, we need another tool to limit the scope of result which is not done yet.
reassign bugs I fixed in project ABC to another people, some time is QA
1 2 | |
Fetcher, generate a task of searching bugs in launchpadView, add extra args to received task (like lp-searchbugs)Task Action, execute received task and print the resultAction, save STDIN to a filelp-searchbugs people $your-lp-login-idbecause accessing Launchpad takes a lot of time, so the real data fetching
work is only executed in Task Action command in my design.
[update]
Tim Chen told me a cool idea I never think about that you can save STDOUT of lp-searchbugs , lp-mybugs or lp-view to a text file
1
| |
next time when you want to get the result, just type
1
| |
this behavior is similar what lp-view does, so now you can create any view you want!
1 2 3 | |
The idea of lp-cli is inspired by my friend Thinker Li’s image downloading tool
用途: 用來警告 function 或 class method 已過時,如果有指定取代的function的話,在runtime時改用取代的function
1 2 3 4 5 6 7 8 9 | |
1 2 3 4 5 6 | |
執行結果:
1 2 3 4 | |
因為有使用functools.wraps,所以雖然old1 這個變數實際上已繫結在wrap function, 但print時還是會顯示為old1 function
1 2 3 4 5 | |
執行結果
1 2 3 4 | |
注意: 目前還不能對class method 指定 replacement method
重用了Python Wiki - Smart deprecation warnings , Active Code Stack - deprecated 的部份源碼
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 | |
wrapperwrapper 用來產生新的function new_funcnew_func 會先顯示warrning, 最後再把參數傳給要wrapped_func
最後再傳回wrapped_func執行結果wrapped_func 根據decorator wrapper的replacement參數來決定是用來代換的function,
還是原本的function.下面是抽象化後的源碼
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | |