九月 26, 2010

hoamon's sandbox
hoamon
hoamon's sandbox is about »

tag cloud

» 在 django-based 系統中,將MyISAM 轉換成 InnoDB

詳細請見官方文件

我簡單整理如下:

一、將 settings.py 中的資料庫引擎從 MyISAM(預設值) 換成 InnoDB , 加下以下設定,


DATABASES = {
'default': {
'NAME': 'xxx',
'USER': 'xxx',
'PASSWORD': 'xxx',
'ENGINE': 'mysql',
'OPTIONS': {
"init_command": "SET storage_engine=INNODB",
},
}
}


二、將原來的表格作


mysql> ALTER TABLE ????? ENGINE=INNODB;


至於第二種方法該如何處理,我是使用 mysql 指令手動處理:


mysql> \T /home/hoamon/alter.sql;
mysql> show tables;
mysql> \q;
sh# perl -i -pe 's/| +([^ ]+) +|/ALTER TABLE \1 ENGINE=INNODB;/g' /home/hoamon/alter.sql
sh# mysql -u xxx -pxxx xxx < /home/hoamn/alter.sql


我在將 MyISAM => InnoDB 時,遇到一個問題 MySQL Error code 1071錯誤('Specified key was too long; max key length is 767 bytes')。

因為原本的 models.py 有一個 Model 設定了 unique_together = (('name', 'uplevel_id'), ) ,而它會造成 1071 Error code,因為 name 的原長度是 256 ,而我們又使用 utf8 ,所以它的實際長度為 256 * 3 = 768 ,但在 InnoDB 的索引中,限制為 767 以下,所以我們必須將 name 的長度限制改為 255 才行。

» perl Makefile.PL --bootstrap=~/opt/pl5

Under .zshrc:

eval $(perl -I$HOME/opt/pl5/lib/perl5 -Mlocal::lib)

十二月 11, 2009

hoamon's sandbox
hoamon
hoamon's sandbox is about »

tag cloud

» 要賣 9 本 Perl 的書(再補一本,共 10 本)


請上露天拍賣網址(結標時間:2010-01-0213:56)

書籍近照:

底價定 300 元,只要是郵資費夠,我就賣掉。

為什麼要賣掉呢? 因為我想我不會再研究 Perl 了吧! 過去,我剛入門 Linux 的時候,那時系統管理員最好用的工具語言是 Perl ,所以為了把 Linux 管好,我花了不少心力學它, Perl 幫助我找到人生中的第一份工作。但是後來,約五年前,我開始常寫網頁系統時,卻無法拿它來用,那時還沒流行 Web Framework ,所以我當然不知道有 Catalyst 的存在,於是我學了 PHP ,用它來寫網頁。

那時的我是 PHP/Perl 兩頭玩,直到兩、三年前, Ruby on RailsDjangoTurboGears 的出現,我才開始認真地想把「系統管理」、「網頁設計」及「學術研究」的工作用同一種語言來解決,最後選的是 Python + Django ,因為 Python 當時的數學函式庫比 Ruby 豐富,所以我們老大就說用 Python 好了,雖然那時我是比較想用 Ruby on Rails 的,當時 Rails 的氣焰比較高。

不過,現在看來,選擇 Python 應是比較符合我的個性,因為我個人是比較龜毛,喜歡 SOP (標準作業程序),所以過去用 Perl 時,有太多種表現的手法後,在我個人寫的程式上,風格十分隨便,反正看不懂了,就再寫一個,而這點, Ruby 的特性就有點類似 Perl 了。

也是用了 Python 後,我才發現過去喜愛 Perl 的程度,不如我以為的那樣高。這讓我想到重慶森林裡的廚師莎拉與漢堡薯條的故事:「沒有嘗試過,你如何知道真正喜歡的是什麼」。所以,我想把這些 Perl 相關的書籍留給其他適合的讀者。願意作它們的新主人嗎?

八月 31, 2008
» 統計檔案大小

隨手寫了一個 script,用來統計檔案大小

#!/usr/bin/perl -p
BEGIN { my $total = 0; }
/(\d+)/; $total += $1;
END { print “Total: $total\t” . ($total / 1024) . “M\n”; }

存成 ts 後可以搭配 duls -s 使用,或者是輸出為每一行開頭有數字的任何指令,例如 seqnl

$ seq 5 | ts
$ ls / | nl | ts

當然這只是好玩而已,沒什麼實際用途

突然想到這幾年陸續接觸了幾種不同的程式語言,包括平日使用最多的 python。但日常使用其實很少出現複雜的應用,大多數情況用 perl 處理起來還是簡潔多了 (雖然這個 perl script 寫得有點像 awk script…)

最後附一下 awk 版:

BEGIN { total = 0 }
total += $1
END { print “Total:”, total,”\t”,total/1024,”M” }


» 統計檔案大小

隨手寫了一個 script,用來統計檔案大小

七月 14, 2008
» [tips] rewrite debian/ubuntu 's lighttpd conf script from perl to python

Today I want to port lighttpd on another platform which basically a debian sarge system but without perl and dpkg package system on it. Since it's a debian based platform so I start from porting debian's binary lighttpd package, however I've found there're some perl script lays in /usr/share/lighttpd which are used when lighttpd startup.

While I can easily dump the result of perl script into a textfile,
and then startup my lighttpd correctly, I thought "maybe port it to python is not a bad idea." (since my target platform has python!), so here is the effort:
create-mime.assign.py

#!/usr/bin/python
#
# This script directly translate from debian's lighttpd perl script:
# create-mime.assign.pl
#
# Author: timchen119.at.nospam.gmail.com
# License: Public Domain
#
import sys

try:
f = open("/etc/mime.types",'r')
extensions = {}
print "mimetype.assign = ("
for line in f:
line = line.strip()
if line.startswith('#'): continue
if line != "":
splitlist = line.split()
if len(splitlist) < 2: continue
mime = splitlist[0]
for ext in splitlist[1:]:
if ext in extensions.keys(): continue
extensions[ext] = 1
print '".%s" => "%s",' % (ext,mime)
f.close()
print ")"
except Exception,e:
print e
sys.exit(1)


include-conf-enabled.py
#!/usr/bin/python
#
# This script directly translate from debian's lighttpd perl script:
# include-conf-enabled.pl
#
# Author: timchen119.at.nospam.gmail.com
# License: Public Domain
#

import os,glob

confdir = "/etc/lighttpd/"
enabled = "conf-enabled/*.conf"

os.chdir(confdir)

for file in sorted(glob.glob(enabled)):
print 'include "%s"' % file

use-ipv6.py
#!/usr/bin/python
#
# This script directly translate from ubuntu's lighttpd perl script:
# use-ipv6.pl
#
# Author: timchen119.at.nospam.gmail.com
# License: Public Domain
#

import socket

##this sometimes not accurate. (like in vserver mode)
#if socket.has_ipv6:
#

try:
if socket.socket(socket.AF_INET6,socket.SOCK_STREAM,0):
print 'server.use-ipv6 = "enable"'
except:
pass

All of these files can be found in http://kalug.linux.org.tw/~tim/lighttpd-debian-python-script/
Well something quite interesting happened when I port the debian's create-mime.assign.pl into python, It's that my python script's final result is not equivalent to perl one and has more mime types than its :
--- perlmime.txt    2008-07-14 15:29:23.000000000 +0800
+++ pymime.txt 2008-07-14 15:29:33.000000000 +0800
@@ -114,6 +114,11 @@
".dvi" => "application/x-dvi",
".rhtml" => "application/x-httpd-eruby",
".flac" => "application/x-flac",
+".pfa" => "application/x-font",
+".pfb" => "application/x-font",
+".gsf" => "application/x-font",
+".pcf" => "application/x-font",
+".pcf.Z" => "application/x-font",
".mm" => "application/x-freemind",
".gnumeric" => "application/x-gnumeric",
".sgf" => "application/x-go-sgf",
@@ -193,6 +198,11 @@
".pk" => "application/x-tex-pk",
".texinfo" => "application/x-texinfo",
".texi" => "application/x-texinfo",
+".~" => "application/x-trash",
+".%" => "application/x-trash",
+".bak" => "application/x-trash",
+".old" => "application/x-trash",
+".sik" => "application/x-trash",
".t" => "application/x-troff",
".tr" => "application/x-troff",
".roff" => "application/x-troff",
@@ -282,6 +292,7 @@
".tgf" => "chemical/x-mdl-tgf",
".mcif" => "chemical/x-mmcif",
".mol2" => "chemical/x-mol2",
+".b" => "chemical/x-molconn-Z",
".gpt" => "chemical/x-mopac-graph",
".mop" => "chemical/x-mopac-input",
".mopcrt" => "chemical/x-mopac-input",

So I start to dig why this happened, and I've found a strange perl regex filter all these mimetypes out, I believe it's a minor bug in original perl program. (or it does implicitly doing something meaningful? well I can't figure it out.)
--- create-mime.assign.pl    2008-07-14 15:35:58.000000000 +0800
+++ create-mime.assign.pl.new 2008-07-14 15:36:07.000000000 +0800
@@ -7,7 +7,7 @@
chomp;
s/\#.*//;
next if /^\w*$/;
- if(/^([a-z0-9\/+-.]+)\s+((?:[a-z0-9.+-]+[ ]?)+)$/) {
+ if(/^([A-Za-z0-9\/+-.~%]+)\s+((?:[A-Za-z0-9.+-~%]+[ ]?)+)$/) {
foreach(split / /, $2) {
# mime.types can have same extension for different
# mime types

replace this line and this will produce same results as mine.

usage:
just copy these py scripts to /usr/share/lighttpd
and change these lines if you're using debian based system
#### external configuration files
## mimetype mapping
#include_shell "/usr/share/lighttpd/create-mime.assign.pl"
include_shell "/usr/share/lighttpd/create-mime.assign.py"

## load enabled configuration files,
## read /etc/lighttpd/conf-available/README first
#include_shell "/usr/share/lighttpd/include-conf-enabled.pl"
include_shell "/usr/share/lighttpd/include-conf-enabled.py"

biggo.com.tw

A Django site.