CodeIgniter Routes Trick – Removing Controller Names from the URI to Keep URLs Short.

ช่วงนี้ บริการจำพวก URL Shortener Service กำลังมาแรง เช่น bit.ly goog.gl  youtu.be fb.me เป็นต้น ผมก็อยากจะลองทำเวบพวกนี้ บน Codeigniter เหมือนกัน สำหรับคนที่เคยใช้ codeigniter จะรู้ว่าระบบ URI ของ CI จะต้องมีรูปแบบดังนี้ http://localhost/controller/function/parameters นั่นหมายถึง มันยาวมากเลย แต่ผมต้องการ http://localhost/parameters เลยอ่ะคับ ไปลอง Google เลยมาโน๊ตเก็บไว้หน่อย เด๋วพอจะใช้แล้วหาไม่เจออีก

เปิดไฟล์ application/config/routes.php

เติมคำสั่งนี้ลงไป

$route['^(?!video|url).*'] = ‘controller/function/$0′;

คำอธิบาย หมายถึงให้ Route URI ที่ไม่ได้ขึ้นต้นด้วยคำว่า video หรือ url ไปที่ๆ ต้องการ

ที่มา http://bizwidgets.biz/web-design/codeigniter-routes-trick-removing-controller-names-from-the-uri-to-keep-urls-short/

[Python] Evolution of a Python programmer.py

เจอผ่านๆ ลูกตา รู้สึกประทับใจ เรยเก็บไว้หน่อย นะๆๆ

1
2
3
4
5
6
7
#Newbie programmer
def factorial(x):
    if x == 0:
        return 1
    else:
        return x * factorial(x - 1)
print factorial(6)

Continue reading this post »

[Apache] Create subdomain on localhost

ปกติแล้ว คนที่ทำเวบ ก็สร้าง subdomain ผ่าน direct admin ง่ายมาก แต่ถ้าอยากลองบน localhost จะทำยังไงดีหละ นั่งหาๆเหมือนกัน เลยจดไว้ดีกว่า กันลืม ลองดูวิธีนี้ละกันนะ ง่ายดี
สำหรับตัวอย่างนี้ สมมติว่าสร้าง subdomain ชื่อว่า sub โดยมีไดเรคทอรี่อยู่ที่ /var/www/sub
เริ่มจาก

$ sudo vi /etc/apache2/sites-enabled/000-default

เติมข้อความนี้ลงไป

ServerAdmin admin@localhost
DocumentRoot /var/www/sub
ServerName sub.localhost

ตามด้วยคำสั่ง

$ sudo vi /etc/hosts

เติมข้อความนี้ลงไป

127.0.0.1    localhost
127.0.0.1    sub.localhost

ลองทดสอบดู ถ้าไม่ได้ให้ restart apache service ก่อนครับ

$ sudo /etc/init.d/apache2 restart

เสร็จแล้วๆๆๆ อิอิ

[PHP] Delete Apache owned files and folders

เนื่องจากผมใช้ Doku wiki แล้ว Apache มันสร้าง cache ขึ้นมาเอง แล้วผมอยากจะลบมัน ปรากฏว่าผมลบไฟล์ไม่ได้ เนื่องจากผมไม่ใช่ Root แล้วเป็นไฟล์ที่ Apache สร้างขึ้นด้วย

นั่งขุดคุ้ยหา script php มาลบให้มันละกัน ทำตามนี้โลด

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?php
function SureRemoveDir($dir) {
      if(!$dh = @<a href="http://www.php.net/opendir">opendir</a>($dir)) return;
      while (($obj = <a href="http://www.php.net/readdir">readdir</a>($dh))) {
            if($obj==.’ || $obj==..) continue;
            if (!@<a href="http://www.php.net/unlink">unlink</a>($dir./.$obj)) {
SureRemoveDir($dir./.$obj);
            } else {
                   $file_deleted++;
            }
      }
      if (@<a href="http://www.php.net/rmdir">rmdir</a>($dir)) $dir_deleted++;
}
?>

วิธีเรียกใช้ก็ง่ายๆ ตามนี้เลยครับ โดยใส่ชื่อ Directory ลงไปแทนคำว่า Test

1
<?php SureRemoveDir ("Test"); ?>

เสร็จแล้ว เย่ๆ ^^

เนื้อหาวิชา Computer Architecture

เนื่องจาก รศ.วรา วราวิทย์ ได้สรุปเนื้อหาวิชา Computer Architecture ได้ดีมาก คลิก และได้เผยแพร่เนื้อหาสู่อินเตอร์เนต แต่พบว่า ระบบการเข้ารหัสภาษาที่ใช้(TIS-620) ยังไม่แพร่หลาย และ Browser ส่วนใหญ่ ไม่สามารถ Auto-Detect ได้

ผมเลย ดูดมาทั้งเวบ แล้ว Convert Encoding จาก TIS-620 ให้เป็น UTF-8 ให้หมดครับ สามารถคลิกเปิดอ่านได้ที่ http://www.klainfo.com/comor

ผมเลือกใช้คำสั่ง iconv ในระบบ Unix ครับ

1
2
3
4
5
for file in `ls *.html`
do
    echo $file
    iconv -f TIS-620 -t UTF-8 $file > ../tmp/$file
done

[ComKUCamp] Introduction GPU Programming

[Ubuntu] Just one click to change ubuntu theme by epidermis

คลิกเดียวแค่นั้นแหละ เปลี่ยน Theme ได้แล้ว

โดยปกติแล้ว กว่าจะได้ Theme สวยๆ ต้องเปลี่ยน Metacity, GTK, Icon, Wallpaper, GDM, Cursor, Splash etc. แต่โปรแกรม epidermis จะจัดการให้หมดเลยครับ ลองดู screen shot ได้

Official Site

Download ทดลอง version 0.4.1 ได้ที่ http://launchpad.net/epidermis/0.x/0.4.1/+download/epidermis_0.4.1-0ubuntu0_all.deb

[Python] Optimize what needs optimizing?

ปกติแล้ว เวลาผมเขียนโปรแกรมจะถือคตินี้มาตลอด นั่นคือ

Make it done, make it right, and then make it fast.

blog นี้จะมาบอกว่า เราจะ opimize โปรแกรมที่เขียนด้วยภาษา python ได้อย่างไรบ้าง

อย่างแรก ให้ติดตั้ง Python profiler ก่อน คลิก เพื่อดูว่าแต่ละ function ใช้เวลาในการทำงานนานเท่าไหร่

blog นี้ ผมจะสรุปจาก PerformanceTips ละกันครับ

  1. เลือก Data structure ให้เหมาะสม
  2. ใช้ Sorting ของ Python
    อ้างอิงจาก Comparing the well-known sorting algorithm ซึ่งผมเคยเทียบประสิทธิภาพไปแล้วว่า python sorting เร็วกว่าใครเพื่อน นอกจากนั้น Guido van Rossum ยังแนะนำให้ใช้ comparator
  3. String Concatenation
    หลีกเลี่ยง (เอา string ใน list มา concat กัน)

    s = ""
    for substring in list:
        s += substring

    แนะนำ

    s = "".join(list)

    หลีกเลี่ยง

    out = "" + head + prologue + query + tail + ""

    แนะนำ

    out = "%s%s%s%s" % (head, prologue, query, tail)

    แต่จะดูดีและเร็วขึ้นอีก ถ้าเขียนแบบนี้ (โฮก!! หล่อมาก++)

    out = "%(head)s%(prologue)s%(query)s%(tail)s" % locals()
  4. Loops แนะนำให้ใช้ map-function
    หลีกเลี่ยง (แปลง string ใน list เป็น upper case)

    newlist = []
    for word in oldlist:
        newlist.append(word.upper())

    แนะนำ

    newlist = map(str.upper, oldlist)

    หรือ

    newlist = (s.upper() for s in oldlist)
  5. Avoiding dot…
    หลีกเลี่ยงการใช้ method chaining เช่น ‘AbCDe1234′.lower().count(‘a’) เป็นต้น
  6. Local Variable
    Python สามารถ access local variable ได้เร็วกว่า global variable
  7. Initialize dictionary element
    แนะนำให้ตั้งค่าเริ่มต้นให้กับ dictionary

    wdict = {}
    for word in words:
        if word not in wdict:
            wdict[word] = 0
        wdict[word] += 1

    หรือเขียนได้อีกแบบ

    wdict = {}
    for word in words:
        try:
            wdict[word] += 1
        except KeyError:
            wdict[word] = 1
  8. import statement overhead
    ทุกคำสั่ง import จะมี overhead เสมอ และหลีกเลี่ยงการใช้ import ภายใน function เช่น

    def foo():
        import os
        ...
  9. Data aggregation
    หลีกเลี่ยงการใช้ for loop ไปเรียก function
    หลีกเลี่ยง

    import time
    x = 0
    def doit1(i):
        global x
        x = x + i
     
    list = range(100000)
    t = time.time()
    for i in list:
        doit1(i)
     
    print "%.3f" % (time.time()-t)

    แนะนำ

    import time
    x = 0
    def doit2(list):
        global x
        for i in list:
            x = x + i
     
    list = range(100000)
    t = time.time()
    doit2(list)
    print "%.3f" % (time.time()-t)
  10. use xrange instead of range

โอ้ 10 ข้อแหนะ ถ้าทำได้หมดแล้วยังไม่เร็วขึ้น แนะนำให้ใช้ Python Psyco ละครับครับ

[Python] Identify the bottlenecks by Python profilers

Python Profiler package เป็น package ไว้สำหรับ วิเคราะห์ประสิทธิภาพของโปรแกรมที่เขียนด้วยภาษา python เพื่อหา bottleneck หรือปัญหาคอขวดของโปรแกรมของเราว่าอยู่ตรงไหน

Minimum-requirement: Python 2.6 or later [use profiler and pstats package]

ถ้าเครื่องไหนไม่สามารถใช้งานได้ ให้ลงก่อน ด้วยคำสั่งดังกล่าว

$ sudo aptitude install python-profiler

ปกติแล้ว Python profilers จะมี standard library ชื่อว่า profile กับ cProfile ผมแนะนำให้ใช้ cProfile ดีกว่าครับ เพราะค่อนข้าง low-level กว่าและมี overhead ต่ำกว่าด้วยครับ

ตัวอย่างการใช้งานทั่วๆไป

import cProfile
import pstats
 
def foo():
    ...
 
if __name__ == '__main__':
    cProfile.run('foo()')

วิธีใช้งานคือ เรียกคำสั่ง cProfile.run(‘ ชื่อฟังก์ชั่น ‘) แค่นั้นเองครับ ง่ายมาก

ตัวอย่างการใช้งานเมื่อมีการส่ง parameter ให้ function

import cProfile
import pstats
 
def foo(bar):
    ...
 
if __name__ == '__main__':
    cProfile.run('foo()',[bar])

วิธีใช้งานคือ เรียกคำสั่ง cProfile.run(‘ ชื่อฟังก์ชั่น ‘, list ของ parameter )

ตัวอย่างของผลลัพท์

      2706 function calls (2004 primitive calls) in 4.504 CPU seconds
 
Ordered by: standard name
 
ncalls  tottime  percall  cumtime  percall filename:lineno(function)
     2    0.006    0.003    0.953    0.477 pobject.py:75(save_objects)
  43/3    0.533    0.012    0.749    0.250 pobject.py:99(evaluate)
 ...

2706 function calls (2004 primitive calls) in 4.504 CPU seconds

หมายถึง มีการเรียก function 2706 ครั้ง ภายในเวลา 4.504 วินาที สำหรับ primitive calls หมายถึง จำนวนการเรียกฟังก์ชั่นแบบไม่ recursive นั่นคือ ถ้ามีการ recursive primitive จะนับแค่ 1 ครั้งเท่านั้น

ncalls หมายถึง จำนวนครั้งที่เรียก function นั้นๆๆ

tottime หมายถึง ปริมาณเวลาทั้งหมด ที่ใช้ในการเรียก function ทั้งหมด

percall หมายถึง สัดส่วน ncall/tottime

cumtime (cumulative time) เวลาที่ใช้ใน function นั้นๆ

percall หมายถึง สัดส่วน primitive call/cumulative time

filename:lineno(function) ระบุ ชื่อไฟล์ บรรทัดที่? และชื่อ function นั้นๆ

สำหรับรายละเอียดเพิ่มเติมสามารถดูได้จาก http://docs.python.org/library/profile.html

[Happy New Year 2010] Twitter Countdown

มาดู Twitter Trend สำหรับปีใหม่ปีนี้ดีกว่า

คนทั่วโลก Tweet หนักมาก #fail