Friday, February 24, 2006

Singleton using python under ms-windows

From ASPN python cookbook
Submitter: Dragan Jovelic

URL:
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/474070
-------------------------------------------------------
from win32event import CreateMutex
from win32api import CloseHandle, GetLastError
from winerror import ERROR_ALREADY_EXISTS

class singleinstance:
""" Limits application to single instance """

def __init__(self):
self.mutexname = "testmutex_{D0E858DF-985E-4907-B7FB-8D732C3FC3B9}"
self.mutex = CreateMutex(None, False, self.mutexname)
self.lasterror = GetLastError()

def aleradyrunning(self):
return (self.lasterror == ERROR_ALREADY_EXISTS)

def __del__(self):
if self.mutex:
CloseHandle(self.mutex)


#---------------------------------------------#
# sample usage:
#

from singleinstance import singleinstance
from sys import exit

# do this at beginnig of your application
myapp = singleinstance()

# check is another instance of same program running
if myapp.aleradyrunning():
print "Another instance of this program is already running"
exit(0)

# not running, safe to continue...
print "No another instance is running, can continue here"

Wednesday, February 22, 2006

離職攻防戰I

今天我拿team leader C簽好的離職單給單位處長A簽名,
A:「老闆教我不能簽,要叫C跟老闆溝通完,我才能簽」
隨即,他播電話給C,也就是我的team leader
A:「C啊,我不能簽啊,你要先跟老闆講」

A掛掉電話後,把離職單還給我

A:「等溝通完了你在拿來。人要走,我留你也是沒意思啊,
但是還是要請C先去跟老闆說。」

我回到位置上,撥了通電話到國防部訓儲小組,跟他們說我有按照離職程序無法,但是公司不給離開。

國防部訓儲小組就打電話到公司國防訓儲關心。人事部主任對於我打電話去國防部求助的事情很不開心,他找了我team leader去談,跟他抱怨說我很不聽話,居然跑去國防部告狀,這樣他會考慮用合約內容跟我求償。訓儲小組也打電話給我,跟我說他們瞭解後的結果。

國:「因為你有跟公司簽約,這部分我們沒辦法插手,你要自己去找人問那份合約是不是有法律效益,因為我們不清楚那份合約的法律效益,所以不能貿然的幫助你註銷資格,讓你回去服役……」

他說了很多,主要是說因為我有跟公司簽至少工作兩年的合約,所以不能幫我,要我自己先解決好合約的問題,如果公司不按照程序送公文,他可以幫忙催。

我當初簽的合約,沒有一式兩份,我這裡連副本都沒有留存。所以一點印象都沒有,於是我問了兩個都確定要離職的同事。

同B:這個合約有阿,大家都嘛有簽這個我以前問過法律顧問耶,他說除非像是航空空司那種花錢培訓你,你沒有用勞力付出。但是我們有付出相當勞力換取薪水,所以那種合約無效。
同D:拜託,我提離職的時候,A也是跟我講一模一樣的話。

我:是喔.....:'

我 之前跟Team Leader談的時候,他有開一些工作項目,說如果我弄完,他就簽名。我也是很任份的把工作做好,前幾天demo給他看,他給了一些意見後以及多了幾個小 地方要在做之外,我也努力達到他的需求。我覺得離職就是這樣啊,你說的條件我做到了,為什麼你還要這樣刁難我呢?在國防部關心完之後,team leader找我單獨談。
C:「你這樣我很失望,你要找我先談啊」

這是我的離職攻防戰I

Monday, February 20, 2006

Slidy

Make your presentations in pure HTML+ javascript (option).

try:
http://www.w3.org/Talks/Tools/Slidy/

Friday, February 17, 2006

id3 tags

java:
jaudiotagger
de vdheide java mp3 library
jid3

python:
id3-py
eyeD3
PyID3
pyid3tag
PyMedia
pytagger

ID3.org

python websites

There are several good python-related websites. Here are below:
Python daily: http://www.pythonware.com/daily/
Dirtsimple: http://dirtsimple.org/
gmane.comp.python.announce: http://permalink.gmane.org/gmane.comp.python.announce

Wednesday, February 15, 2006

Rhino

Mozillz Rhino is an open-source implementation of JavaScript written entirely in Java.

URL:
JSR 223 in Project Mustang.

javadoc: javax.script (since j2sdk1.6)

JFrame.show()

JFrame.show() ==>Deprecated. As of JDK version 1.5, replaced by setVisible(boolean)

javax.swing.SpringLayout ==>add from 1.4. SpringLayout is a very flexible layout manager that can emulate many of the features of other layout managers.

URL:
How to Use SpringLayout

A concatenative language

A concatenative language is one where every function has a single argument (a stack) and returns a single value (a new stack).

FYI:
Programming language Forth
Programming language JOY
Unimperative Reborn as a Concatenative Language
Joy compared with other functional languages

Tuesday, February 14, 2006

I love div

有時候在javascript要出現提示的訊息,會直接把要顯示的東西當作一個變數或是字串,往往美工相關人員的編輯以及翻譯並不會去看HTML原始碼,有時候會漏翻了這些句子。
在不使用任何高超的技術下(純粹用HTML與簡單的javascript),我採用以下的方法,讓美編人員可以在編輯時期,看到那些要顯示出來的語句。


<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=big5">
<title>test</title>
<script language="javascript" type="text/javascript">
function initHTML()
{
var divObjs = new Array(document.getElementById('actionPrompt'));
var i = 0;
for(i=0;i<divObjs.length;i++)
{
if( doesObjExists(divObjs[i]) )
setHiddent(divObjs[i]);
}
}
function setHiddent(obj)
{
obj.style.visibility = 'hidden';
}
function doesObjExists(obj)
{
var result = false;
result = obj&& typeof(obj) != 'undefined'
return result;
}
function mainProgress()
{
var actPrompt = document.getElementById('actionPrompt');
var promptText = 'Are you sure to delete all of them?';
if(doesObjExists(actPrompt))
{
promptText = actPrompt.innerHTML;
}
return confirm(promptText);
}
</script>
</head>
<body onLoad="javascript:initHTML();">
<form name="test" onSubmit="javascript:mainProgress();">
<input type="submit" name="Submit" value="按我">
</form>
<div id="actionPrompt">您確定要刪除嗎?</div>
</body>
</html>

Thursday, February 09, 2006

sqlite3 bugs?

使用:sqlite 3.2.7
schema:
CREATE TABLE msginfo(msgnum INTEGER PRIMARY KEY,msgtype INTEGER NOT NULL DEFAULT 0,msgts INTEGER NOT NULL,msgbtime INTEGER NOT NULL,msgtime INTEGER DEFAULT NULL,linenum INTEGER NOT NULL,msgtsbegin INTEGER NOT NULL DEFAULT 0,callid VARCHAR(255) DEFAULT '',dialstr VARCHAR(255) DEFAULT '',ownerid INTEGER NOT NULL,msgmemo TEXT DEFAULT NULL,user_type INTEGER,archived INTEGER DEFAULT NULL,source INTEGER DEFAULT NULL,orgidx INTEGER DEFAULT NULL,UNIQUE (msgts,linenum,ownerid));
CREATE INDEX Idx_comp on msginfo (ownerid,msgts);
CREATE INDEX Idx_msgts on msginfo (msgts);
CREATE INDEX idx_msgbtime ON msginfo(msgbtime);
CREATE INDEX idx_msgfile ON msginfo (msgts,linenum);
CREATE INDEX idx_msgtime ON msginfo(msgtime);
CREATE INDEX idx_msgtsbegin ON msginfo(msgtsbegin);
CREATE INDEX idx_ownerid ON msginfo (ownerid);

import pysqlite2.dbapi2 as sqlite
con = sqlite.connect('test.db')
cur = con.cursor()
pagelen = 50
cond = 'ownerid=20 AND msgtype=0'
cur.execute('SELECT COUNT(*) FROM msginfo WHERE %s' % cond)
total = cur.fetchone()[0]
pages = (total + pagelen - 1) //pagelen
print 'Total:%d'% total
for i in range(pages):
offset = i * pagelen
cur.execute('select * from msginfo where %s order by msgts desc limit 50 offset %d'% (cond, offset))
a = cur.fetchall()
begin = i*pagelen
sizeofa = len(a)
print 'offset = %d and data =%d (%d~%d)'% (offset,sizeofa, begin, begin + sizeofa)

---------------------------------------------------------------------------------
Total: 1978
offset = 0 and data =50 (0~50)
offset = 50 and data =50 (50~100)
offset = 100 and data =50 (100~150)
offset = 150 and data =50 (150~200)
offset = 200 and data =50 (200~250)
offset = 250 and data =50 (250~300)
offset = 300 and data =50 (300~350)
offset = 350 and data =50 (350~400)
offset = 400 and data =50 (400~450)
offset = 450 and data =50 (450~500)
.....
.....
offset = 1750 and data =50 (1750~1800)
offset = 1800 and data =50 (1800~1850)
offset = 1850 and data =50 (1850~1900)
offset = 1900 and data =50 (1900~1950)
offset = 1950 and data =31 (1950~1981)
------------------------------------------
紅色部分應該是28,因為1978 % 50 = 28

檢查SQL語法以及使用的Index:
bash-2.03# idxchk backupMsgInfo.db
SELECT COUNT(*) FROM msginfo WHERE ownerid = 20 AND msgtype = 0
table index(es) column(s)
------- ----------- ------------------------------
msginfo
msginfo idx_ownerid (ownerid)

bash-2.03# idxchk backupMsgInfo.db
SELECT * FROM msginfo WHERE ownerid = 20 AND msgtype = 0 ORDER BY msgts DESC LIMIT 50 OFFSET 1950
table index(es) column(s)
------- ---------- ------------------------------
msginfo
msginfo Idx_comp (ownerid,msgts)

使用的Index不同。現在強制取消使用Index:
sqlite3 backupMsgInfo.db
>SELECT * FROM msginfo WHERE +ownerid = 20 AND +msgtype = 0 ORDER BY msgts DESC LIMIT 50 OFFSET 1950;
219|0|948670669|73655|73749|2|948670615||1|20|||5||27
211|0|948670558|73505|73558|2|948670505||1|20|||5||26
203|0|948670447|73314|73407|2|948670394||1|20|||5||25
...
...
共28筆

結局:
資料庫Index不同步。
(到底是什麼鬼原因讓資料庫的Index不同步....)
解決方法:使用VACCUM

Tuesday, February 07, 2006

刪除某行

import os
ip = '192.168.1.123'
ip = ip.replace('.','\\.')
SourceFile = '/root/.ssh/known_hosts'
UpdateFile = '/root/.ssh/known_hosts'
params = (SourceFile, ip, UpdateFile)
result = os.system("cp %s /tmp/known_hosts.$$ ;awk '$1!~/^%s/ {print ; }' /tmp/known_hosts.$$ > %s;rm -rf /tmp/known_hosts.$$"% params) == 0

Monday, February 06, 2006

Openwebmail malfunction

The openwebmail is suddenly malfunction after someone upgrade perl without notifing.

Here is the solution below:

portupgrade -f perl-5.8.\* -m "ENABLE_SUIDPERL=yes"

rehash

use.perl port

portupgrade -m "-DFORCE_PKG_REGISTER" -rf perl-5.8.\* -x perl-5.8.\* -l /tmp/portupgrade.log



The original post

Audacity

Audacity, a free, corss-plantform audio editor. The LAME library helps a lot to generate mp3 files.