發表文章

Tips: PPTP client on Openwrt

Besides from following the openwrt wiki PPTP client page, there are some extra tips required for making PPTP client work on openwrt. Just like on normal linux distribution, special netfilter modules are required: nf_nat_pptp nf_conntrack_pptp nf_conntrack_proto_gre On openwrt, you need to install 'kmod-nf-nathelper-extra' to get theses modules; opkg install kmod-nf-nathelper-extra With this, you should be able to connect to PPTP server without problem. After the connection, you sometimes get these error and the connection drops: Sun Jan 24 19:13:57 2016 daemon.warn pppd[29657]: read returned zero, peer has closed Sun Jan 24 19:13:57 2016 daemon.warn pppd[29657]: read returned zero, peer has closed Sun Jan 24 19:14:17 2016 daemon.info pppd[29655]: Terminating on signal 15 Surprisingly, this is caused by MTU. Setting MTU to 1400 on LUCI fixed this. 

嘸蝦米文翻譯機 網頁版

我很久以前寫了一個 嘸蝦米文翻譯機 ,不過因為是桌面程式所以其實用起來比較不方便。所以今天突然心血來潮就寫了一個網頁版的,網址如下: http://ovff.herokuapp.com/ 使用方式很簡單,左邊輸入無蝦米文後按向右的鍵頭就可以翻成中文。 反之如果在右邊輸入中文按下向左的鍵,就可以翻成嘸蝦米文。

PyTox: Python binding for Project-Tox the Skype Replacement

前些時候注意到了 Project-Tox 這個專案,看了一下覺得非常有趣。Project-Tox 是一個想要取代 Skype 的 IM 網路。他非常的安全,通訊完全P2P及加密,且使用類似 BitTorrent 的 DHT 來進行資訊交換。由其在 PRISM 事件後,大家都非常在意這點。因為 Project-Tox 只是一個 library,所以其每個人都可以實做不同的 Client,目前有的 Client 可以看 這裡 。 我為 libtoxcore 寫了一個 Python 的 binding: PyTox ,所以可以很容易的利用 Python 來寫 Client 或是機器人。PyTox 目前已經支援99%的libtoxcore API,且全部通過自動化測式。後來我又寫了一個機器人來自動同步 Tox 群組聊天和 IRC channel 中的對話: tox-irc-sync  。所以如果你現在到 Freenode 上的 #tox-ontopic 中發訊息,就會在 Tox group chat 中看到一個名為 SyncBot 的機器人幫你同步訊息,而且同步是雙向的! Project-Tox 目前只支援文字聊天,視訊的功能還在內部測試中。如果想要幫忙的人可以到 Freenode IRC的 #tox 及 #tox-dev 裡跟開發著們聊天,他們都很熱心的!

Quciksort in Python: Functional vs Non-functional

最近學了 SML 和 Racket 後,開始對 Functional Programming 很有興趣。 不過在學的過程中,一直有一個疑問: immutable 不會讓程式變很慢嗎?(因為為做很多copy?) 所以想拿個例子分別寫 Functional / Non-functional 的寫法來比較看看速度。就選用最好寫的 Quicksort 好了,程式碼如下: #!/usr/bin/env python from timeit import timeit from random import randint def quicksort_impl(v, start, end): if start + 1 >= end: return pv = v[end - 1] partition = start for i in range(start, end): if v[i] < pv: v[partition], v[i] = v[i], v[partition] partition += 1 v[partition], v[end - 1] = v[end - 1], v[partition] quicksort_impl(v, start, partition) quicksort_impl(v, partition + 1, end) def quicksort(v): quicksort_impl(v, 0, len(v)) return v def quicksort_fp(v): if len(v) == 0: return [] else: return quicksort_fp([x for x in v[:-1] if x < v[-1]]) + \ [v[-1]] + \ quicksort_fp([x for x in v[:-1] if x >= v[-1]]) def quicksort_fp_nl(v): if len(v) ==...

嘸蝦米翻譯機 復活!

圖片
三年多前寫了一個 嘸蝦米翻譯機  後來因為忙碌一直未更新 Windows 安裝檔。最近因為在整理過去的作品,就翻出來順便整理一下XD 最新的 Windows 安裝檔下載:   gOVFF-2.1.0-Installer.exe Github 頁面: govff @ github Enjoy :)

PhantomJS 擷圖 亂碼修正

使用PhantomJS拮取網站擷時,有時會因為網站Server的Content-Type沒有指定 charset 造成亂碼。Github 上有一個 issue  也是同樣的問題還沒解決。後來想到可以用 chardet 配上HTTP Proxy 來改寫Content-Type header。以上是一個簡單的script來解決這樣的問題:

MongoDB as queue

想要把 MongoDB 裡的其中一個 collection 做為 queue, 讓多個 worker 存取來做事,可以利用 MongoDB 的 findAndModify。 findAndModify 是 atomic operation,執行的結果會回傳一個物件並且更新他的狀態。 例如現在有一個 collection 名為 keys, 裡面存著 {"id": id, "queued": false} db.runCommand({"findAndModify": "keys", "query": {"queued": false}, "update": {"$set": {"queued": true}}})

使用 pppd 連接 VPN 時連線凍結

本來都是用學校的 Junos Pulse SSL VPN  連線到學校查論文,不過最近學校的SSL VPN一直壞掉,所以只好自己在 Lab 的電腦上裝 PPTPD。不過問題來了,用 Network Manager 連進去都可以,可是用 pppd 連線時確一直無法上網,不過倒是可以 ping 到 server。 比對了一下 routing table 發現是一樣的,而且我有在/etc/ppp/ip-up.d 底下加上了 01-route.sh,所以會把所有的 traffic route 到 ppp 去 #!/bin/bash route del default wlan0 route add default $1

azhuang.me 網域

買下了 azhuang.me 這個網域,所以 blogger 也一起轉過來了 :P 以後請使用 http://blog.azhuang.me 連到我的網誌。

MongoDB 初體驗

因為 Web Mining final project 我們想做 twitter tweet 的 clustering 及 classification,所以需要從 twitter 下載大量的 Tweet 做為實驗用途。一開始我是把這些 tweet 存成 JSON 在文字檔裡,不過隨著 tweet 量越來越多,用文字檔實在很難處理。由於本來就是存成JSON的格式,所以就想到了 MongoDB。 之前只有玩過try MongoDB, 從來沒有好好用過他,這次剛好有這個機會就來用一下 :P 裝好MongoDB後就可以開始始用了。由於這次 project 是用 ruby 寫的(因為有方便的  Twitter Gem  可以用),所以我用了 Mongo Gem   。Mongo Gem 的用法跟 Mongo shell 裡的用法差不多,簡單的用法如下,更多的說明可以參考 Mongo Gem 的 wiki 。

ActiveRecord like API for Python

之前看到 ActiveRecord 有一個很方便的功能: find_by_COLUMN,而 COLUMN 是那個 Table 裡的 columns。這用 Ruby 的 define_method 很好實做,當然 Python 也可以做到。下面是一段 POC (Prove of Concept) code:

Monkey Patching: Python V.S. Ruby

之前在寫Rails的時候還不是很熟悉Ruby,所以很多Ruby的特性沒有去深究。最近剛考完期末考比較有時間了,所以好好來看了一下Ruby。目前覺得學Ruby的好處就是會讓我想去比較Python,因為我個人還是Python的愛好著,所以看Ruby可以做到的功能就想要看Python可不可以做到。拿Ruby最有名的名能之一來說好了: Monkey Patching,從Wikipedia: A  monkey patch  is a way to extend or modify the  run-time   code  of dynamic languages without altering the original  source code . Ruby 可以很容易的動在舊有的Class/Module中加入新的Class/Method,舉例來說我們可以新增一個  method 到 Array 中: def Array.test puts "This is a singleton method `test' of the Array class" end

Decompressing vmlinux from vmlinuz

Find the gzip signature: # od -A d -t x1 vmlinuz | grep '1f 8b 08' 0018016 48 8d 83 e0 c7 2e 00 ff e0 1f 8b 08 00 00 00 00 Decompress data after that: # dd if=vmlinuz bs=1 skip=18025 | zcat > vmlinux Note that 18025 = 18016 + 9 Reference: http://www.codeguru.com/forum/showthread.php?t=415186

SQL Job Queue的寫法

最近網站要Deploy到MySQL上才發現之前寫得很多都有問題或是效率不好。 之前對於Job Queue的寫法是這樣寫: def pop(): stamp = time() db.query(Table).update({'stamp': stamp}) return db.query(Table).filter_by(stamp=stamp).all() 後來發現原來MySQL有SELECT .. FOR UPDATE的用法(是不是該修一修Database了XD) 加上了FOR UPDATE後,目前被選到的row就會被Lock起來。而其他的Process如果選到了 同樣的row,就必需等到commit後才能繼續。 所以就可以寫成: def pop(): records = db.query(Table).with_lockmode('update').filter(Table.status == 'NEW').all() db.query(Table).filter(Table.id.in_([x.id for x in records])).update('status': 'QUEUED') db.commit() return records 由於我們把status設成了'QUEUED',而在select時選擇Status為'NEW'的row,所以並不會造成conflict或是lock wait。 這樣的寫法好多了,而且在MySQL裡也比較不會出問題。

Script for notifying process termination

圖片
When I'm working, I usually have a lot of terminal open with different tasks running in different terminal. When I run a process that takes a long time(e.g. compiling, file prcoessing), I usually switch to another terminal. The annoying thing is that I need to constantly switch back to the terminal to check the progress of the process. Because of this, I wrote a script that monitors processes specified, and notify you when the process is finished via 'notify-send'.

Show GIT status on bash prompt

圖片
It's been a while since I last post, so I'm quite surprised by the new blogger interface. Guess it's all because of Google+ integration :p I've been using git for quite a while, and it's by far the best VCS I've ever used. The thing is, when you have a lot of branches in you project,it's really hard to keep track of what branch you are on. It would be nice to have a indicator or something that tell you just that. I've seem people using Fish shell . With Fish shell, you can simply write a ruby script to customize the prompt text, showing git status is just one of the things you can do. But since I never get used to Fish shell and at that time my projects never went over two branches, I didn't think it's necessary. Recently I am working on some projects that requires 3 to 4 branches, so I sometimes commit/edit on the wrong branch. That's when I decided to add git status to my prompt.

Turning off discrete graphics for Acer Aspire 4750G

OPTIMUS on Linux has always been a pain in the ass for Linux users. It drains your battery power but you can't use it! Here is a way to turn it off on Linux, enjoy. Install acpi_call From github git clone https://github.com/mkottman/acpi_call.git cd acpi_call make sudo cp acpi_call.ko /lib/modules/`uname -r`/kernel/drivers/acpi From AUR If you are using Arch Linux, it's available at here

Embedded System Final Project

圖片
Embedded System Fianl Project, utilizing 1. Qt-Embedded 2. libchewing (big thanks to Jserv) 3. SQLite 4. QtWebkit 5. ibus-boshiamy Sources can be found at github Here's a video which I demonstrated on our final project meeting. (Sorry for the bad quality)

嵌入系統實驗PXA300 Lab3

圖片
Ubuntu on ARM Machine? No way. 其實是嵌入實驗做的數位相框XD

資結HW2

讓我想到我小時候在哈電族上寫的文字編輯器XD 那時候寫的是有多個Mode的編輯器,有Insert和Normal Mode 所以那時候也要寫像這次資結的東西,移動遊標啊,插入,刪除等... 還因為要輸入中文,所以要偵測雙位元字元... 真的好懷念啊XD