2018年3月6日 星期二
2017年12月10日 星期日
[Python] Eric IDE For Python On Ubuntu16.04
- 安装QT5
Ref : http://smlie-blog.blogspot.tw/2017/11/qtqt5ubuntu1604.html - 安装相關套
- pip3
#apt-get install python3-pip
#pip3 install --upgrade pip - PyQt5
#pip3 install PyQt5 - Mercurial
#apt-get install mercurial - QScintilla
#pip3 install QScintilla - Eric
#hg clone hg clone https://die-offenbachs.homelinux.org/hg/eric
#cd eric
#python3 install.py
[1] https://eric-ide.python-projects.org/index.html
[2]Install Eric 6 for Python (Windows/Ubuntu)
2015年11月24日 星期二
[Python] 在Ubuntu 14.04,使用Python撰寫MySql程式
- 安裝Python 套件
- Sample Code
#apt-get install python-mysqldb
import MySQLdb
try :
db = MySQLdb.connect("host", "username", "password", "db_name", charset='utf-8')
sql = "SELECT field1, field2 FROM table"
cursor = db.cursor()
cursor.execute(sql)
results = cursor.fetchall()
for record in results:
col1 = record[0]
col2 = record[1]
print "%s, %s" % (col1, col2)
db.close()
except MySQLdb.Error as e:
print "Error %d: %s" % (e.args[0], e.args[1])
try :
db = MySQLdb.connect("host", "username", "password", "db_name", charset='utf-8')
sql = "SELECT field1, field2 FROM table"
cursor = db.cursor()
cursor.execute(sql)
results = cursor.fetchall()
for record in results:
col1 = record[0]
col2 = record[1]
print "%s, %s" % (col1, col2)
db.close()
except MySQLdb.Error as e:
print "Error %d: %s" % (e.args[0], e.args[1])
Reference :
[1] 實作Python 連接MySQL 資料庫
2015年5月27日 星期三
[Python] getattr 的用途與應用
getattr(object, name[, default])
Reference:
[1] Python 3.1 快速導覽 - 內建函數 getattr()
- 回傳物件裡的屬性
- Python的內建函數
- Example1
class Demo:
def __init__(self):
self.x = "demo_x"
self.y = "demo_y"
def test(self):
print "demo_test"
demo = Demo()
print getattr(demo, "__init__")
print getattr(demo, "x")
print getattr(demo, "y")
def __init__(self):
self.x = "demo_x"
self.y = "demo_y"
def test(self):
print "demo_test"
demo = Demo()
print getattr(demo, "__init__")
print getattr(demo, "x")
print getattr(demo, "y")
執行結果
- Example2
class WebService:
def __init__(self):
self.demos = []
def register(self, demo)
self.demos.append(demo)
def exe(self, func):
for demo in demos.values():
function = getattr(demo, func)
return function(parms)
class Demo():
def _exe1(self, parms):
print "_exe1: %s " % parms
def _exe2(self, parms):
print "_exe2: %s" % parms
ws = WebService()
ws.register(Demo())
ws.exe('_exe1')
Reference:
[1] Python 3.1 快速導覽 - 內建函數 getattr()
2015年4月24日 星期五
[Python] 使用Python實作一個類似Java Synchronized之用法
Java synchronized 範例如下:
Python 範例如下:
synchronized 使用方式:
Reference:
[1] Synchronized in Python
public class Sync {
public synchronized void execute(String funcName) {
System.out.println(funcName)
}
}
public synchronized void execute(String funcName) {
System.out.println(funcName)
}
}
Python 範例如下:
import time
import threading from threading
import Thread
# 實作 synchronized
def synchronized(func):
func.__lock__ = threading.Lock()
def synced_func(*args, **kws):
with func.__lock__:
return func(*args, **kws)
return synced_func
import threading from threading
import Thread
# 實作 synchronized
def synchronized(func):
func.__lock__ = threading.Lock()
def synced_func(*args, **kws):
with func.__lock__:
return func(*args, **kws)
return synced_func
synchronized 使用方式:
# 需要執行synchronized的函數
@synchronized
def execute(func_name):
print func_name
time.sleep(2)
@synchronized
def execute(func_name):
print func_name
time.sleep(2)
Reference:
[1] Synchronized in Python
2015年4月23日 星期四
[Python] Decorator 用法
Python 有一個很特殊語法,是可以在Function中可以定義Function,用法如下
而基於這個方法,可在搭配Decorator,做出類似Callback Function
1. 一般呼叫方式
輸出結果 :
2. 使用 Decorator @ --> 是Python語言的特殊字,稱之Decorator ,用法如下
Reference :
[1][python] decorator 之很難理解的快速理解法
def function1():
print "execute function1"
def inner_function1():
print "execute inner_function1"
print "execute function1"
def inner_function1():
print "execute inner_function1"
而基於這個方法,可在搭配Decorator,做出類似Callback Function
def callback(user_func):
def _callback(*args, **kwargs):
print '''執行預先處理 '''
# 執行使用者函數
return user_func(*args, **kwargs)
return _callback
def _callback(*args, **kwargs):
print '''執行預先處理 '''
# 執行使用者函數
return user_func(*args, **kwargs)
return _callback
1. 一般呼叫方式
def cb(x):
print "計算 x+x"
return x+x
result = callback(cb)
print result(5)
print "計算 x+x"
return x+x
result = callback(cb)
print result(5)
輸出結果 :
執行預先處理
計算 x+x
10
計算 x+x
10
2. 使用 Decorator @ --> 是Python語言的特殊字,稱之Decorator ,用法如下
@callback
def cb(x):
print "計算 x+x"
return x+x
print cb(5)
def cb(x):
print "計算 x+x"
return x+x
print cb(5)
Reference :
[1][python] decorator 之很難理解的快速理解法
[Python] list 轉換成 dict
Example :
x = [ "a", "1", "b", "2" ]
期望轉成
x = {"a":"1", "b":"2"}
語法:
x = [ "a", "1", "b", "2" ]
期望轉成
x = {"a":"1", "b":"2"}
語法:
hash = dict(x[i:i+2] for i in range(0, len(x), 2))
訂閱:
文章 (Atom)