Sean's Note: Python
顯示具有 Python 標籤的文章。 顯示所有文章
顯示具有 Python 標籤的文章。 顯示所有文章

2016年3月20日 星期日

Setting Up Environment for PyQt 5 on Mac with Python 2.7

要用 Python 2.7 來開發 Qt 5,需要安裝下列三樣:
  1. SIP 4.17
  2. PyQt 5.5.1
  3. Qt 5

安裝 sip

  1. 下載 sip-4.17.tar.gz
  2. 解壓縮:tar -xf sip-4.17.tar.gz
  3. 切換目錄到 sip-4.17
  4. python config.py,產生 Makefile
  5. make
  6. make install

安裝 Qt 5

本來是用官網的  Online Installer,但選擇下載 Qt 5.6.1 後,不僅要吃 13.06 GiB,還一直卡在某個進度,只先好作罷。改用 Cakebrew 下載 Qt 5.6.0,而且只要 310.7M。

安裝 PyQt 5.5.1

安裝方式和 sip 一樣
  1. 下載 PyQt-gpl-5.5.1.tar.gz
  2. 解壓縮:tar -xf PyQt-gpl-5.5.1.tar.gz
  3. 切換目錄到 PyQt-gpl-5.5.1
  4. python config.py,產生 Makefile
    • 此時可能會有找不到 qmake 指令的錯誤,qmake 在安裝好的 PyQt 路徑下,設定好路徑就行了。export PATH=/usr/local/Cellar/qt5/5.6.0/bin:$PATH
  5. make  (這裡會需要等一下子)
  6. make install
大功告成! 立馬開啟 PyCharm 來試試第一個程式吧!


Ref: http://www.time-eater.net/?p=302

2016年3月14日 星期一

SetEnvironmentVariable VS getenv

最近發現一個奇怪的現象,
就是在 VS 的 cpp 裡呼叫 SetEnvironmentVariable 設定環境變數,
但從 Python code 裡印出 os.osenviron 卻看不到所設定的變數,
一查才發現,Python 裡的 os.osenviron 是用 getenv實作的。
GetEnvironmentVariable/SetEnvironmentVariable 是 Windows API,
而 setenv/getenv 是 CRT 函式。CRT 在開始的時候便會透過 Windows API GetEnvironmentStrings 將回傳值存入自己的資料結構之下(EX: MSVCR80!environ),
而此後 getenv 便是對這份副本做操作。

結論就是不要混用 SetEnvironmentVariable 和 getenv 啊!!

Ref:
  1. https://bugs.python.org/issue16633
  2. http://blogs.msmvps.com/senthil/2009/10/13/when-what-you-set-is-not-what-you-get-setenvironmentvariable-and-getenv/

2015年8月4日 星期二

[sqlite3] Auto-commit will be inefficient for multiple SQL statements.

我們通常會用下列的程式碼來建立 DB 的連線,並指明是 auto-commit 的。

# Set isolation_level=None to use auto-commit
conn = sqlite3.connect('example.db', isolation_level=None)

來實驗看看執行 10000 次 INSERT 的敘述,

for i in range(0, 10000):
    conn.execute("INSERT INTO stocks VALUES ('2006-01-05','BUY','RHAT',100,35.14)")

居然要 657 秒!! 
因為每一筆 INSERT 都是一筆 Transaction,太花時間了,
所以如果我們只建立一筆 Transaction,
然後把 Statements 都塞到同一個 Transaction 再執行:

conn.execute("BEGIN IMMEDIATE TRANSACTION");
# Insert a row of data
for i in range(0, 10000):
    conn.execute("INSERT INTO stocks VALUES ('2006-01-05','BUY','RHAT',100,35.14)")
conn.execute("COMMIT TRANSACTION");

結論: 發現只要 0.17 秒,差了快 400 倍啊!!

2014年7月16日 星期三

[Python] 跟檔案有關的常用函式

os.path.join('c' 'folder') # 'c\\folder'
os.path.join('c:' 'folder') # 'c:folder'

# Get file name.
os.path.basename('C:\\Windows\\123.txt') # '123.txt'

# Get file path and file extension.
filePath, fileExtension = os.path.splitext('C:\\Windows\\123.txt') # 'C:\\Windows\\123', '.txt'

# Get directory path.
os.path.dirname('C:\\Windows\\123.txt') # 'C:\\Windows'

# Get modify time from epoch time.
os.path.getmtime('C:') # 1409646173.7839031

# Traverse a directory.
for root, subdirs, files in os.walk("C:\\Pictures"):
  print root, subdirs, files 

2014年7月4日 星期五

[Python] How to get the last day of the month

想要知道某個特定月份有幾天,可以直接呼叫 calendar.monthrange(2014, 2),

會回傳第一天是禮拜幾和該月有幾天。

範例:
import calendar
calendar.monthrange(2014, 2)
# (5, 28) # 禮拜六,28天
calendar.monthrange(2014, 3)
# (5, 31) # 禮拜六,31天
calendar.monthrange(2014, 4)
# (1, 30) # 禮拜二,30天

2013年11月20日 星期三

[Python] 前綴字 _ 和 __ 的差別

一個底線 _

def _getName(self):
    ...

name = property(_getName)

代表 _getName 是 private function,不應該被外部呼叫,常常應用於 property 中

,然而在 Python 中,並沒有真的 private function,所以應該說是撰寫上的慣例。

兩個底線 __

class A():

    def __getName(self):
        ....

a = A()
a.__getName # Wrong!!
a._A__getName # OK, but not doing this!

代表 __getName 不允許被 override,而且也不能被外部呼叫。

Python 的實作方式其實就是在方法前加上類別名稱 -> _A__getName。

Ref: http://igorsobreira.com/2010/09/16/difference-between-one-underline-and-two-underlines-in-python.html

2013年1月10日 星期四

Using Pyro to talk to remote object

如同 Java 中的 RMI,Python 也有 Pyro 可以用,

Pyro 是一套程式庫,使我們可以讓物件在網路上互相溝通。

Python 2.5 的使用者,可以安裝 Pyro3 (文件);

Python 2.6 以上的使用者,可以安裝 Pyro4 (文件)。

安裝方法: 可以用 easy_install 來快速安裝 Pyro。