2013年6月18日 星期二

在Blogger上Highlight程式碼 (Blogger CSS設定, Pygments產生html, Appkit Paste Buffer)

動機:
  • 想在Blogger上放上程式碼,不想使用syntax highlight plugins,在Mac上可以直接使用程式碼轉成html貼上部落格上。
使用工具:
步驟:
  1. Blogger「範本設計工具」新增CSS。
  2. Pygments 產生程式碼有Highlight的html。
  3. 使用PyObjC Appkit的剪貼簿複製Buffer。
  4. 程式碼貼到Blogger上。
Blogger「範本設計工具」新增CSS
from pygments.formatters import HtmlFormatter

print HtmlFormatter().get_style_defs('.highlight')
把印出來的CSS貼到Blogger的範本設計工具(範本>自定>進階>自定CSS)


Pygments 產生程式碼有Highlight的html
from pygments import highlight
from pygments.lexers import PythonLexer
from pygments.formatters import HtmlFormatter

code = u'print "Hello World!!"'
print highlight(code, PythonLexer(), HtmlFormatter())
會印出html這樣就可以貼上Blogger了!

使用PyObjC Appkit的剪貼簿複製Buffer

但~~每次都要去複製print出來的HTML實在非常麻
煩,如果程式碼很長你就要複製更多的HTML,身為一個程式設計師當然要找到解決的solution.
Solution就是用PyObjC的AppKit啦!!Apple的一些Framework接到python library!
以下就是複製與貼上的程式碼啦!:
from AppKit import *

def pbcopy(input_string):
    "Copy string argument to clipboard"
    newStr = Foundation.NSString.stringWithString_(input_string).nsstring()
    newData = newStr.dataUsingEncoding_(Foundation.NSUTF8StringEncoding)
    board = NSPasteboard.generalPasteboard()
    board.declareTypes_owner_([NSStringPboardType], None)
    board.setData_forType_(newData, NSStringPboardType)

def pbpaste():
    "Returns contents of clipboard"
    board = NSPasteboard.generalPasteboard()
    content = board.stringForType_(NSStringPboardType)
    return content

程式碼貼到Blogger上

最後就是在Blogger上打Command + v!打完收工。

P.S.上述程式碼都還滿容易的,有需要完整程式碼可以寫信給我!

2012年10月28日 星期日

Coin3D & QT on Ubuntu12.10

這篇內容是如何建構開發環境!

OS: ubuntu 12.10 32bit

1. 安裝開發套件:

sudo apt-get install build-essential
sudo apt-get install libqt4-dev
sudo apt-get install libcoin60
sudo apt-get install libsoqt4-dev

2. 撰寫sample code

#include <Inventor/Qt/SoQt.h>
#include <Inventor/Qt/viewers/SoQtExaminerViewer.h>
#include <Inventor/nodes/SoBaseColor.h>
#include <Inventor/nodes/SoCone.h>
#include <Inventor/nodes/SoSeparator.h>

int main(int argc, char ** argv)
    {
    printf("hello");
    // Initializes SoQt library (and implicitly also the Coin and Qt
    // libraries). Returns a top-level / shell Qt window to use.
    QWidget * mainwin = SoQt::init(argc, argv, argv[0]);

    // Make a dead simple scene graph by using the Coin library, only
    // containing a single yellow cone under the scenegraph root.
    SoSeparator * root = new SoSeparator;
    root->ref();

    SoBaseColor * col = new SoBaseColor;
    col->rgb = SbColor(1, 1, 0);
    root->addChild(col);

    root->addChild(new SoCone);

    // Use one of the convenient SoQt viewer classes.
    SoQtExaminerViewer * eviewer = new SoQtExaminerViewer(mainwin);
    eviewer->setSceneGraph(root);
    eviewer->show();

    // Pop up the main window.
    SoQt::show(mainwin);
    // Loop until exit.
    SoQt::mainLoop();

    // Clean up resources.
    delete eviewer;
    root->unref();
    SoQt::done();

    return 0;
}

3.compile

產生project (QT.pro)
qmake -project
產生Makefile
qmake
compile
make
這時候會產生make error
是因為qmake產生Makefile沒有去做SoQT 和 Coin3D的動態連結

4 修改Makefile

在 LIBS後面加 -lSoQt -lCoin
LIBS = $(SUBLIBS)  -L/usr/lib/i386-linux-gnu -lQtGui -lQtCore -lpthread -lSoQt -lCoin

5 finish

make
./QT

2012年9月25日 星期二

Design Pattern

在 「Design Patterns入門實作班」與「模式入門第一堂課:30 分鐘寫出一個模式」課程中,Teddy提到一個pattern的六個基本元素。這裡補充說明一下這六個基本元素分別對應到GoF Design Patterns格式的哪些結構。Name->Name,Problem->Intent, Context->Applicability, Forces->Motivation, Solution->Participants + Structure + Collaborations + Implementation, Resulting Context -> Consequences。

2012年7月21日 星期六

Tkinter

這兩天再玩Python原本內建的GUI library Tkinter, 
真的很easy &有趣! 先記錄看過的網頁再來寫一些文章。

Google搜尋到的第一個網頁,但似乎有些語法已經有修改了,看python.org的document有些parameter是用dict傳入。
http://www.pythonware.com/library/tkinter/introduction/

2012年6月25日 星期一

OSX: 如何把程式碼貼到Keynote?

要把程式碼貼到Keynote或者power point上,
把程式碼轉成rtf格式就可以貼了(微軟出的格式)
在OSX有兩個tool可以用,
一個是highlight 一個是source-highlight

highlight:

  1. brew install highlight
  2. highlight -O rtf code.c | pbcopy
  3. 在Keynote貼上

source-highlight

  1. brew install source-highlight
  2. source-highlight -d -i code.c -o STDOUT | textutil -convert rtf -stdin -stdout | pbcopy
  3. 在Keynote貼上

P.S.

  • source-highlight 好像python語法支援比較完整。
  • pbcopy其實是把stdout變成剪貼布,真是個好東西XD

















參考網站:
http://kristensen.org/post/20059991688/code-examples-in-keynote

2012年3月20日 星期二

Python import modules: UliPad

最近程式上遇到大問題,
就是在Embedded板子上import modules速度很慢,
只要code超過兩千行,import會需要大概一秒的時間。
我想這是python本身的限制,
畢竟他是直譯式的語言。

上網爬文查了一些資料,
有人建議去看UliPad這個用Python寫的IDE,
因為其實如果純用Python寫的話,
等的時間問題點就會在import modules。

UliPad是用wxPython寫出來的,
所以首先要裝wxPython。
要順利在Lion上跑起UliPad,
然後下載ulipad.4.1.zip
最後解壓縮執行./UliPad.pyw

UliPad真的速度滿快的,
要好好研究一下他是怎麼寫的,
能夠讓import modules 延後import。


=========後記===========
原來它是用psyco這個module來增加import速度,
但這個project已經沒有在繼續maintain了,
而且只支援32bit的CPU。

有新的project在運行,
一個psyco作者另開的project PyPy
而另一個是unladen-swallow


參考資料:http://fcamel-fc.blogspot.com/2010/01/python.html