find(self, *args, **kwargs) :Parameters: - `spec` (optional): a SON object specifying elements which must be present for a document to be included in the result set bson.son class SON(__builtin__.dict) SON data. A subclass of dict that maintains ordering of keys and provides a few extra niceties for dealing with SON. SON objects can be converted to and from BSON. __init__(self, data=None, **kwargs)
所以若要保留順序,就要直接用 SON 物件來當 find 的參數。建立的方法跟
dict.items() 傳回來的東西一樣,是 “list of D’s (key, value) pairs, as
2-tuples”
1 2 3 4 5 6
>>> from bson import son >>> a = son.SON([('a', 1), ('b', 1), ('c', 1), ('d', 1)]) >>> a SON([('a', 1), ('b', 1), ('c', 1), ('d', 1)]) >>> a.to_dict() {'a': 1, 'c': 1, 'b': 1, 'd': 1}
WXR Parser 可用來分析
WordPress.com 匯出的 XML 檔案,是為了這次要搬出來然後用
Wintersmith 靜態產生 blog 頁面寫的,目前可以匯
出適合 Wintersmith 使用的目錄結構。不過因為設計上 parser 跟 backend 分
開,所以要擴充來產生其他格式也蠻簡單。
A simple WXR parser written in Python to parse the XML export from
WordPress and store the information it in in Python’s basic data
structures, i.e. dictionaries and lists. It also goes with a backend
to export it in Markdown syntax suitable for
Wintersmith. In its current form, it can
simplify the migration from WordPress to Wintersmith, but it’s easy to
be extended to export more formats.
It’s created because the author failed to find a simple one to use.
這陣子想簡化一切事情,譬如 blog 也該算是個人知識管理的一部分,不能快速搜尋翻找就不太對,而且最好是這類東西都有個共通界面去紀錄,Evernote 就是蠻好的想法,配上 postach.io 當 blog 就不錯,但真的不適合我。之前稍微嘗試過,但我的記事情方法是森林狀的,然後他整個散落各處,用筆記本跟 tag 去分反而找不到而且覺得超討厭。猜想這個跟另一種思維方式的人會很配,也說明為何他這麼紅吧。Anyway, 以前不會很在意在這類東西之間換來換去,但現在越來越覺得用熟悉的工具去盡量節省時間是很重要的。(這又牽涉到一些研發上的思考,一直以來常在換技術換語言之類的,專注某領域然後深耕是不是也很重要,然後這領域究竟該是像 web programming 這種很具象的領域還是說應該是某種抽象的「平行處理」之類的領域呢?似乎該是後者,但問題是我沒有後者,這就尷尬了…)
The TrackPoint on a ThinkPad laptop is one of the main reasons why I kept buying laptops from them. However, as a heavy laptop user who refused to carry around one extra mouse, the constant usage of TrackPoint can cause a significant amount of pain to my fingertip, which is very unpleasant. An once-in-a-while quick movement is okay, but scrolling up/down takes longer and requires more pressure, which is the main cause of discomfort.
As a possible solution, I’ve tried to enable TouchPad and two-finger scrolling. However, for some reason it didn’t work by setting it in gnome-control-center, so instead I used the old faithful xinput. From the output of xinput list-props "SynPS/2 Synaptics TouchPad", it’s easy to spot “Synaptics Two-Finger Scrolling”, and the two parameters can enable vertical and horizontal scrolling, respectively.
The default setting of two-finger scrolling was far from intuitive – normally people would expect it to work just like the way it works on touch devices: you touch the screen, then you can push the document up by moving your finger up. That means scrolling down when your finger is moving up. But on my laptop, moving two fingers up means scrolling up, which causes the document to move down!
To make this problem worse, there is no setting in gnome-control-center to inverse this. Again, this can be solved with xinput by simply changing the values of “Synaptics Scrolling Distance” from 100 100 to -100 -100.
Pencil is simply the best (open) sketchy mockup solution on Ubuntu. However, as described on its download page, you cannot install it as a Firefox extension because it’s not compatible with the Firefox 18.x that comes with Ubuntu 12.04. The standalone version runs okay but some features such as export as PNG fails silently because of the same compatibility issue.
Turns out you don’t need to install the deb package provided on its website. Simply download the latest tar ball from the download page on google code, then download xulrunner (Mozilla runtime) from ftp.mozilla.org. I used the latest supported version: 16.0.2.
Now extract these to a preferred place, then add a file called pencil anywhere in your $PATH with the following content:
<<判斷作法>>= void condition(constchar *list[], constint len) { int i; for (i = 0; i < len; i++) { if (i != 0) printf(", "); printf("%s", list[i]); } puts(""); } @
python-ropemac is really useful for developing python in emacs, and pylint is also very handy as a analyzer. However, they both don’t work very well with virtualenv, especially because I always run emacs in server mode, and the server instance is usually not under virtualenv.
Here is how to make things work:
Edit .ropeproject/config.py:
1 2 3
# You can extend python path for looking up modules prefs.add('python_path', '/your-virtualenv-dir/lib/python2.7/site-packages/')
For pylint, generate (by pylint --generate-rcfile) or copy your default pylintrc to project root dir. Edit it:
1 2 3
# Python code to execute, usually for sys.path manipulation such as # pygtk.require(). init-hook='this_file="/your-virtialenv-dir/bin/activate_this.py";execfile(this_file, dict(__file__=this_file))'
To me, variable sum is similiar to ‘instance variable’, in Object-oriented’s terminology. However, in Python, things can be quite different.
1 2 3 4 5 6 7 8 9 10 11 12
defadder(): sum = 0
deff(x): sum += x return sum return f
defmain(): pos, neg = adder(), adder() for i in xrange(0, 10): print pos(i), neg(-2 * i)
The code looks roughly the same, but it will raise the following exception:
1 2 3 4 5 6 7 8
Traceback (most recent calllast): File"closure.py", line 17, in main() File"closure.py", line 13, inmain print pos(i), neg(-2 * i) File"closure.py", line 5, in f sum += x UnboundLocalError: localvariable'sum'referencedbefore assignment
This is because if sum is to be modified, Python must decide which variable to change. **sum += x** is the same as **sum = sum + x**, and **sum = ** suggests it’s a local variable, since all variable is by default local in Python. Given that, expression **sum + x** can not be evaluated because sum, as a local variable, is still undefined here.
If the **sum += x** line is removed, and **sum + x** is returned directly, the result will be:
It runs okay, but the result is wrong. Where does function f get the value of sum? If Python cannot find a variable in locals(), it will try to find it from the scope above it, i.e. function adder, and sum is indeed defined in it. The real Python equivelent of the Go program above will be:
defmain(): pos, neg = adder(), adder() for i in xrange(0, 10): print pos(i), neg(-2 * i)
if __name__ == '__main__': main()
Functions are already first class objects in Python. Here we create a class that its instance behaves like a function, so it is a function because of duck typing.