搜尋此網誌

2008年10月27日 星期一

gnome字典

sudo aptitude install dict-stardic 
這樣local端就會裝一個字典

sudo gnome-dictionary
把沒用的字典來源統統刪掉

字典來源新增一個localhost, 其他內容都不變
使用這個localhost就可以了

這樣就可以用了
不過只有英翻中, 簡體的... XD

安裝Freemind 0.9.0 到 Ubuntu

下載 freemind source檔案解壓縮

sudo apt-get install  install  sun-java6-fonts sun-java6-jdk

sudo apt-get install ant ant-optional

下指令: ant dist
之後就用指令 freemind就可以使用了



Wubi crash ... 要記得備份!!!

偶的Wubi安裝的 Uuntu 8.04.1掛了

掛的方式是這樣的:
按了"休眠"模式, 再把notebook螢幕合上, 就會螢幕叫不起來了
硬把電源關掉, 重開機
GRUB4DOS就找不到ubuntu了
進了windows一看,  挖!!!!!!!!

偶的ubuntu disk通通不見了!!!!

只好重裝

天阿...還是要備份, 真的不能偷懶

2008年10月20日 星期一

Ajax圖片顯示 -- Lightbox2

http://www.huddletogether.com/projects/lightbox2/

了解Django

why use Django?

快速, MVC,
Entire websites are portable and can be use in other websites


Models


django實做python的class: django.db.models.Model
定義在website裡面要用到的data model
data model就是一組定義要被存在資料庫的


Views

python funtions --> build and render web page
每個python function --> 定義至少一個 URL pattern 可以連結到特定的URL (view function)



Templates

讓view function 可以快速的建立web pages



settings.py 設定檔

當建立一個新的專案project
django就會自動建立一個settings.py在根目錄裡
settings.py包含:
database的設定, 已經安裝的applications還有一些其他設定
甚至可以:
from django.conf import settings.py


manage.py

當建立一個新的專案project
django也會自動建立一個manage.py在根目錄裡

manage.py事實上是一個application叫做 django-admin.py的wrapper
用來執行所有管理, 開發,測試的相關工作
以下就是常用的命令:
1. startproject
2. startapp --> 建立project下面的application
3. syncdb --> 將project models的資料同步到DB去
4. runserver
5. shell
6. dbshell --> 執行DB的SQL command-line client

Django Development Server

Admin Model View
Admin Change List View
Admin Change List View


檢查安裝


可以在ipython下:
import django
django.VERSIOM








2008年10月15日 星期三

ubuntu 查看 mht格式檔案

可以在firefox安裝 maf的外掛, 就可以瀏覽mht格式
最後將之轉成pdf就好辦了



Firefox->File->Open File->select xpi file to install

2008年10月7日 星期二

ipython

安裝

我用的是 python 2.5.2 + ubuntu 8.0.4

下載最新板 0.9.1 姐壓縮
進到該目錄 執行 sudo python setup.py install 就可以了


寫的很棒的ipython介紹與使用
http://gfw.appspot.com/jackphil/2007/08/blog-post.html


print 變數   --> 使用 __str__ method
跟 直接變數 的區別   --> 使用 __repr__ method

ipython有兩個變數 In 跟 Out
In是List型態
Out是dictionary型態

ipython具有 tab completion功能

原來的python交談窗也可以有這樣的功能
/>>> import rlcompleter, readline
/>>> readline.parse_and_bind('tab: complete')


magic edit


magic function


 就是前面有加 % 的 , 像是 %cd
打 lsmagic 可以看到所有的magic function

打 % 就會列表

要跳出ipython使用
Exit 或是 Quit 也ok

使用magic function 就是 %列表的命令直接打就可以了

不知道這個magic function要怎麼用
只要在這個magic function 後面加 ?   就會秀出完整用法
像是
In [4]: who ?





Shell execute


只要在前面加上! 就可以了

也可以先設好變數, 用 $變數名  傳到command line裡面去
In [20]: user='parks'

In [21]: process = 'bash'

In [22]: !ps aux | grep $user | grep $process
parks     6097  0.0  0.1   5660  3044 pts/0    Ss   17:09   0:00 bash
parks     8743  0.2  0.1   5660  3032 pts/1    Ss+  17:28   0:00 bash


吼~~ 也可以把結果指定給變數
In [23]: l = !ps aux | grep $user | grep $process

In [24]: l
Out[24]: SList (.p, .n, .l, .s, .grep(), .fields(), sort() available):
0: parks     6097  0.0  0.1   5660  3044 pts/0    Ss   17:09   0:00 bash
1: parks     9094  0.0  0.0   3008   764 pts/0    S+   17:30   0:00 grep bash



對於python而言
/>>> import os
/>>> os.getcwd()
'/home/parks'

使用Ipython的話

In [39]: pwd
Out[39]: '/tmp'

In [40]: cd /home/parks
/home/parks

In [41]: cd -
/tmp




bookmark

In [46]: cd /tmp
/tmp

In [47]: bookmark t
也可以

In [47]: bookmark t /tmp


In [48]: %bookmark -l
Current bookmarks:
t -> /tmp


要刪除bookmark
In [58]: bookmark -d t

把bookmark 統統刪掉
In [58]: bookmark -r



目錄history


In [69]: dhist
Directory history (kept in _dh)
0: /tmp
1: /home/parks
2: /tmp
3: /home/parks
4: /tmp
5: /home/parks
6: /tmp
7: /usr
8: /tmp


使用
In [71]: cd -7
/usr



In [73]: cd -8
/tmp


列出後面幾個切換目錄的紀錄:
n [69]: dhist 5
或是
n [69]: dhist 3 5
就會列出 3, 4 兩個歷史紀錄


range(x)


In [77]: for i in range(10):
   ....:     !date > ${i}.txt
   ....:    

就會造出0.txt ~ 9.txt, 內容都是今天日期的檔案

注意 {current list item} 這樣的用法

python裡面, 若要知道這個變數的type
就可以用
type(變數)



字串存取




重要unix命令:
ps aux
列出目前所有的正在記憶體當中的程序

grep
字串比對, 搜尋檔案中的特定字串
想找出 sneakers.txt 檔案中含有 "coffee" 這個字的地方,您可以輸入
grep coffee sneakers.txt
這個檔案中有找到 "coffee" 這個字的每一行

awk



ipython可以這樣用
In [6]: ps = !ps aux
In [7]: ps.grep('gedit')
Out[7]: SList (.p, .n, .l, .s, .grep(), .fields(), sort() available):
0: parks    22826  0.3  1.2  84640 26056 ?        S    16:54   0:05 gedit


注意out這邊的訊息, 有寫出哪些參數跟method可用

同理, 可以這樣用:
In [9]: file_list = !ls
In [10]: file_list
Out[10]: SList (.p, .n, .l, .s, .grep(), .fields(), sort() available):
0: css_test
1: DOWNLOAD
2: Examples
3: gtk_test
....

檔案查詢的話, 可使用:
import os
然後
In [13]: file_list.grep(os.path.isdir)
或是
In [13]: file_list.grep(os.path.isfile)
來列表

像是ps的話, 可以用 field()來決定要看哪幾個欄位
In [26]: ps.grep('gnome').fields(0,1,8)
Out[26]: SList (.p, .n, .l, .s, .grep(), .fields(), sort() available):
0: parks 5768 14:52
1: parks 5769 14:52
....

可以跟 grep() method混搭

In [28]: ps.fields(0,1).s
Out[28]: 'USER PID root 1 root 2 root 3 root 4 root 5 root 6 root 7 root 8 root 9 root 10 root 11 root 46 root 47...

使用s的話, 就會把結果用空白隔開


Information gathering


如果訊息太多, 沒辦法一頁表示完, 可以使用page
In [1]: p = !ps aux
In [2]: page p

就好像是下less指令一樣


In [4]: def myfunc(a,b,c,d):
   ...:     '''傳回something'''
   ...:     return a,b,c,d
   ...:

In [5]: pdef myfunc
 myfunc(a, b, c, d)

In [6]: pdoc myfunc
Class Docstring:
    傳回something
Calling Docstring:
    x.__call__(...) <==> x(...)



pinfo的用法
page p53,p54

psource
就會列出module的source code
In [36]: psource some_module

psearch
找python object
In [40]: psearch a


In [41]: a = 1

In [42]: psearch a
a

In [43]: aa = "one"

In [44]: psearch a
a

In [45]: psearch a*
a
aa
abs
all
any
apply


找出自己定義的
In [53]: psearch -e builtin l*
ll



也可以用變數type來找
In [55]: psearch -e builtin * string
__
___
__name__
aa
ll


who
把現在有給值得有引用的, 都列出來
In [56]: who
a    aa    f    ll    os    some_module   

也可以:

In [57]: who int
a   
In [58]: who str
aa    ll  
 

who_ls很像who, 但是傳回一個list可以讓你用
In [63]: who_ls
Out[63]: ['a', 'aa', 'f', 'll', 'os', 'some_module']

這個list的變數名字叫做 _

可以用:
In [64]: for n in _:
   ....:     print n
   ....:    
   ....:
a
aa
f
ll
os
some_module


whos 會嗅出更仔細的訊息列表
我覺得這個不賴
一樣也可以:
In [66]: whos int
Variable   Type    Data/Info
----------------------------
a          int     1




歷史紀錄查詢


使用magic function: hist
In [74]: hist
35: #?some_module.Foo ?
36: _ip.magic("psource some_module")
....

可以看到顯示成magic function call的樣子
hist -n 不顯示行號
hist -r 不顯示magic function call, 就是實際key in的樣子


不管是python或是 ipython
都可以存取 history of results
In [2]: foo = 'foo_string'

In [3]: _
Out[3]: ''

In [4]: foo
Out[4]: 'foo_string'

In [5]: _
Out[5]: 'foo_string'

In [6]: a=_

In [7]: a
Out[7]: 'foo_string'

利用 _ 來存放result


Automation and shortcuts

macro 用來定義一段code, 可以不管在任何地方(code)接著執行, 好用!

In [2]: dirlist=[]

In [3]: for f in dirlist:
   ...:     print "working on",f
   ...:     print "done with",f
   ...:     print "move %s to %s.done"%(f,f)
   ...:     print "*"*40
   ...:

In [4]: macro procdir 3

把In [3] 的設定成一個macro 叫做 procdir

In [5]: dirlist = ['a.txt','b.txt','c.txt']

In [6]: procdir

也可以呼叫 edit來編輯 macro

In [11]: edit procdir

edit也可以跟hist混搭
就是像是:
 

In [11]: hist
....
In [11]: edit 4:7

edit 也可用 ed 一樣的意思

edit編完之後返回時會執行你編的code
若不想這樣, 那就下 edit -x



store 儲存在 ipython定義的變數
reset 刪除所有定義的變數
run 在ipython裡面執行特定的檔案
save 江特定的input lines存到特定的輸出檔案

rep 重複之前的function動作
可以指定行號 (執行起來有點問題? )
rep 2 重複執行第二行的東西
rep 2-3 重複執行第二行~第三行的東西


2008年10月6日 星期一

python 的 module

python的module

很簡單, 就是命名為   [module的名字].py  裡面包含method或是calss

要用的時候, 只要用 import  [module的名字]

或是[module的名字].method 就可以了
當作是module的時候
有多個global variable 叫做 __name__

注意module 的search path
通常在 PYTHONPATH 這個變數中

unix類型的通常在
/usr/local/lib/python

ubuntu的是在:
/usr/local/lib/python2.5/site-packages