<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>【牧碼志】</title>
	<atom:link href="http://0x3f.org/?feed=rss2" rel="self" type="application/rss+xml" />
	<link>http://0x3f.org</link>
	<description>晴耕雨讀 ...</description>
	<lastBuildDate>Mon, 30 Apr 2012 19:09:34 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>讀完《三體——黑暗森林》</title>
		<link>http://0x3f.org/?p=2213</link>
		<comments>http://0x3f.org/?p=2213#comments</comments>
		<pubDate>Mon, 30 Apr 2012 19:09:34 +0000</pubDate>
		<dc:creator>lenin</dc:creator>
				<category><![CDATA[青梅煮酒]]></category>
		<category><![CDATA[小說]]></category>
		<category><![CDATA[科幻]]></category>
		<category><![CDATA[讀書]]></category>

		<guid isPermaLink="false">http://0x3f.org/?p=2213</guid>
		<description><![CDATA[版權所有：晴耕雨讀@【牧碼志】，如需轉載，請注明出處。 原文地址：http://0x3f.org/?p=2213 凌晨三點，讀完了《三體》的第二部。 咒語，這個東西的確很有意思。第二部比第一部好看，故事情節上完全展開，成功地製造了懸念。如果一部小說，刪掉很多文字仍不影響故事情節，那這部分就是多餘的，可惜在這部書裡這是個很大的問題，所以直到最後三分之一才真正出彩。 Posted via UltraBlog.vim.]]></description>
			<content:encoded><![CDATA[<div style="margin:15px 0px; font-style: italic; background-color:#F3F7FC; padding: 10px;">
版權所有：晴耕雨讀@<a href="http://0x3f.org/">【牧碼志】</a>，如需轉載，請注明出處。<br />
原文地址：<a href="http://0x3f.org/?p=2213">http://0x3f.org/?p=2213</a>
</div>
<p>凌晨三點，讀完了《三體》的第二部。</p>
<p>咒語，這個東西的確很有意思。第二部比第一部好看，故事情節上完全展開，成功地製造了懸念。如果一部小說，刪掉很多文字仍不影響故事情節，那這部分就是多餘的，可惜在這部書裡這是個很大的問題，所以直到最後三分之一才真正出彩。</p>
<p><a href="https://picasaweb.google.com/lh/photo/tt1qOjDU6_6tnnQgdfYtw9MTjNZETYmyPJy0liipFm0?feat=embedwebsite"><img src="https://lh5.googleusercontent.com/-OcSgdVvYZD8/T57gVJYqtyI/AAAAAAAAB4E/C6tOtnZPX28/s800/s4700425.jpg" height="426" width="312" /></a></p>
<p>Posted via <a href="http://0x3f.org/?p=1894">UltraBlog.vim</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://0x3f.org/?feed=rss2&#038;p=2213</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>UltraBlog.vim v3.5.0: 內建瀏覽器</title>
		<link>http://0x3f.org/?p=2211</link>
		<comments>http://0x3f.org/?p=2211#comments</comments>
		<pubDate>Mon, 30 Apr 2012 08:33:38 +0000</pubDate>
		<dc:creator>lenin</dc:creator>
				<category><![CDATA[編程]]></category>
		<category><![CDATA[Plugin]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[UltraBlog.vim]]></category>
		<category><![CDATA[Vim]]></category>
		<category><![CDATA[博客]]></category>

		<guid isPermaLink="false">http://0x3f.org/?p=2211</guid>
		<description><![CDATA[版權所有：晴耕雨讀@【牧碼志】，如需轉載，請注明出處。 原文地址：http://0x3f.org/?p=2211 此前預覽文章是通過調用系統默認的網絡瀏覽器實現的，缺點是這些功能完備的瀏覽器啟動速度不甚理想，尤其是火狐，誰用誰知道。 前兩天看了TuxRadar的一個Podcast，幾分鐘內就可以用pywebkitgtk拼裝出一個五臟俱全的網絡瀏覽器，pywebkitgtk這個東西實在是非常的好用。不過我實測後發現這東西加載頁面的速度比較慢，而且貌似是單線程的，因為圖片都是放在最後才加載，不知道是這個綁定本身的問題，還是有API可以實現的。於是突然想到拿這個東西實現一個簡單的瀏覽器，很適合在UB裡預覽文章。 增加了一個選項“ub_use_ubviewer”，默認值是1，即默認使用這個內建的瀏覽器預覽文章。當這個選項的值被設為0時，仍然使用系統的默認瀏覽器，並且在Vim啟動時不會加載內建的瀏覽器。這樣做一是為了多一種選擇，再一個也可以避開煩人的GTK警告： ** (gvim:13629): WARNING **: Trying to register gtype &#8216;GMountMountFlags&#8217; as enum when in fact it is of type &#8216;GFlags&#8217; ** (gvim:13629): WARNING **: Trying to register gtype &#8216;GDriveStartFlags&#8217; as enum when in fact it is of type &#8216;GFlags&#8217; ** (gvim:13629): WARNING **: Trying to register gtype &#8216;GSocketMsgFlags&#8217; as enum when [...]]]></description>
			<content:encoded><![CDATA[<div style="margin:15px 0px; font-style: italic; background-color:#F3F7FC; padding: 10px;">
版權所有：晴耕雨讀@<a href="http://0x3f.org/">【牧碼志】</a>，如需轉載，請注明出處。<br />
原文地址：<a href="http://0x3f.org/?p=2211">http://0x3f.org/?p=2211</a>
</div>
<p>此前預覽文章是通過調用系統默認的網絡瀏覽器實現的，缺點是這些功能完備的瀏覽器啟動速度不甚理想，尤其是火狐，誰用誰知道。</p>
<p>前兩天看了TuxRadar的一個Podcast，幾分鐘內就可以用pywebkitgtk拼裝出一個五臟俱全的網絡瀏覽器，pywebkitgtk這個東西實在是非常的好用。不過我實測後發現這東西加載頁面的速度比較慢，而且貌似是單線程的，因為圖片都是放在最後才加載，不知道是這個綁定本身的問題，還是有API可以實現的。於是突然想到拿這個東西實現一個簡單的瀏覽器，很適合在UB裡預覽文章。</p>
<p>增加了一個選項<strong>“ub_use_ubviewer”</strong>，默認值是1，即默認使用這個內建的瀏覽器預覽文章。當這個選項的值被設為0時，仍然使用系統的默認瀏覽器，並且在Vim啟動時不會加載內建的瀏覽器。這樣做一是為了多一種選擇，再一個也可以避開煩人的GTK警告：</p>
<blockquote>
<p>** (gvim:13629): WARNING **: Trying to register gtype &#8216;GMountMountFlags&#8217; as enum when in fact it is of type &#8216;GFlags&#8217;</p>
<p>** (gvim:13629): WARNING **: Trying to register gtype &#8216;GDriveStartFlags&#8217; as enum when in fact it is of type &#8216;GFlags&#8217;</p>
<p>** (gvim:13629): WARNING **: Trying to register gtype &#8216;GSocketMsgFlags&#8217; as enum when in fact it is of type &#8216;GFlags&#8217;</p>
</blockquote>
<p>這些警告是GTK或其它一些程序庫的Bug造成的，雖然只在虛擬終端中啟動Vim時會顯示並且不影響使用，但總會有警告恐懼症患者會覺得這種東西很鬧心。如果這樣，就把這個選項的值設成0，用回巨型瀏覽器好了。</p>
<p>本次更新的全部內容如下：</p>
<ul>
<li>Feature: Add a tiny web browser to do previewing, which starts much faster than full-functional browsers like firefox and chromium. The later ones are still supported. The matter that whether or not to use the new previewer is controlled by a new option <strong>ub_use_ubviewer</strong>.</li>
<li>Bugfix: Issue 7: Keywords highlighting is disabled in item lists, even if searches are made manually.</li>
</ul>
<p>關於UB的詳細信息在<a href="http://0x3f.org/?p=1894">這裡</a>。</p>
<p>這是那個Podcast：</p>
<p><embed src='http://player.youku.com/player.php/sid/XMzg5MTc1OTI4/v.swf' quality='high' width='480' height='400' align='middle' allowScriptAccess='sameDomain' type='application/x-shockwave-flash'></embed></p>
<p>Posted via <a href="http://0x3f.org/?p=1894">UltraBlog.vim</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://0x3f.org/?feed=rss2&#038;p=2211</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>UltraBlog.vim v3.4.0: 正則表達式、批量替換和調試模式</title>
		<link>http://0x3f.org/?p=2205</link>
		<comments>http://0x3f.org/?p=2205#comments</comments>
		<pubDate>Sun, 29 Apr 2012 11:45:40 +0000</pubDate>
		<dc:creator>lenin</dc:creator>
				<category><![CDATA[編程]]></category>
		<category><![CDATA[Plugin]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[UltraBlog.vim]]></category>
		<category><![CDATA[Vim]]></category>
		<category><![CDATA[博客]]></category>

		<guid isPermaLink="false">http://0x3f.org/?p=2205</guid>
		<description><![CDATA[版權所有：晴耕雨讀@【牧碼志】，如需轉載，請注明出處。 原文地址：http://0x3f.org/?p=2205 這次的更新主要引入了支持正則表達式的全文檢索、批量替換和調試模式。 正則表達式 我一直覺得原來的全文檢索有一個遺憾，雖然可以通過多個關鍵詞實現較為精確的查詢，但還是不如正則表達式靈活和精確。 這是個蓄謀已久的需求，但直到真正做起來，才發現很多有意思的東西。雖然SQLite3提供支持正則表達式查詢的“REGEX”關鍵詞，但並沒有實際實現這個功能，而是需要寫程序實現並在數據庫接口中註冊這個函數： import sqlite3 &#160; conn = sqlite3.connect&#40;'/tmp/your-database-file.db'&#41; &#160; # 使用正則表達式匹配給定內容的函數，返回布爾類型 def regexp_search&#40;expr, item&#41;: &#34;&#34;&#34;Check if the item has a sub-string which matches the expr&#34;&#34;&#34; reg = re.compile&#40;expr&#41; return reg.search&#40;item&#41; is not None &#160; # 在數據庫中註冊這個函數 conn.create_function&#40;'REGEXP', 2, regexp_search&#41; &#160; cur = conn.execute&#40;'select id,title from post where content REGEXP ?', '\babc\b'&#41; [...]]]></description>
			<content:encoded><![CDATA[<div style="margin:15px 0px; font-style: italic; background-color:#F3F7FC; padding: 10px;">
版權所有：晴耕雨讀@<a href="http://0x3f.org/">【牧碼志】</a>，如需轉載，請注明出處。<br />
原文地址：<a href="http://0x3f.org/?p=2205">http://0x3f.org/?p=2205</a>
</div>
<p>這次的更新主要引入了支持正則表達式的全文檢索、批量替換和調試模式。</p>
<h2>正則表達式</h2>
<p>我一直覺得原來的全文檢索有一個遺憾，雖然可以通過多個關鍵詞實現較為精確的查詢，但還是不如正則表達式靈活和精確。</p>
<p>這是個蓄謀已久的需求，但直到真正做起來，才發現很多有意思的東西。雖然SQLite3提供支持正則表達式查詢的<strong>“REGEX”</strong>關鍵詞，但並沒有實際實現這個功能，而是需要寫程序實現並在數據庫接口中註冊這個函數：</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">import</span> sqlite3
&nbsp;
conn = sqlite3.<span style="color: black;">connect</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'/tmp/your-database-file.db'</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># 使用正則表達式匹配給定內容的函數，返回布爾類型</span>
<span style="color: #ff7700;font-weight:bold;">def</span> regexp_search<span style="color: black;">&#40;</span>expr, item<span style="color: black;">&#41;</span>:
    <span style="color: #483d8b;">&quot;&quot;&quot;Check if the item has a sub-string which matches the expr&quot;&quot;&quot;</span>
    reg = <span style="color: #dc143c;">re</span>.<span style="color: #008000;">compile</span><span style="color: black;">&#40;</span>expr<span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">return</span> reg.<span style="color: black;">search</span><span style="color: black;">&#40;</span>item<span style="color: black;">&#41;</span> <span style="color: #ff7700;font-weight:bold;">is</span> <span style="color: #ff7700;font-weight:bold;">not</span> <span style="color: #008000;">None</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># 在數據庫中註冊這個函數</span>
conn.<span style="color: black;">create_function</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'REGEXP'</span>, <span style="color: #ff4500;">2</span>, regexp_search<span style="color: black;">&#41;</span>
&nbsp;
cur = conn.<span style="color: black;">execute</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'select id,title from post where content REGEXP ?'</span>, <span style="color: #483d8b;">'<span style="color: #000099; font-weight: bold;">\b</span>abc<span style="color: #000099; font-weight: bold;">\b</span>'</span><span style="color: black;">&#41;</span>
row = cur.<span style="color: black;">fetchone</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">print</span> row
&nbsp;
conn.<span style="color: black;">close</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></pre></div></div>

<p>在SQLAlchemy中具體的實現方式是：</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #808080; font-style: italic;"># 註冊函數</span>
dbe = sqlalchemy.<span style="color: black;">create_engine</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;sqlite:///tmp/your-database-file.db&quot;</span><span style="color: black;">&#41;</span>
conn = dbe.<span style="color: black;">connect</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
conn.<span style="color: black;">connection</span>.<span style="color: black;">create_function</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'REGEXP'</span>, <span style="color: #ff4500;">2</span>, regexp_search<span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># 在SQL Expression Language中創建查詢條件</span>
tbl = Post.__table__
cond_1 = tbl.<span style="color: black;">c</span>.<span style="color: black;">title</span>.<span style="color: black;">op</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'regexp'</span><span style="color: black;">&#41;</span><span style="color: black;">&#40;</span>r<span style="color: #483d8b;">'<span style="color: #000099; font-weight: bold;">\b</span>abc<span style="color: #000099; font-weight: bold;">\b</span>'</span><span style="color: black;">&#41;</span>
cond_2 = tbl.<span style="color: black;">c</span>.<span style="color: black;">content</span>.<span style="color: black;">op</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'regexp'</span><span style="color: black;">&#41;</span><span style="color: black;">&#40;</span>r<span style="color: #483d8b;">'<span style="color: #000099; font-weight: bold;">\b</span>abc<span style="color: #000099; font-weight: bold;">\b</span>'</span><span style="color: black;">&#41;</span></pre></div></div>

<p>和普通的全文檢索一樣，正則表達式的全文檢索也支持使用多個表達式作為查詢條件，多個條件之間是與的關係。現在可以這樣查詢所有包含“UltraBlog.vim”但不把推廣代碼算在內的文章了：</p>

<div class="wp_syntax"><div class="code"><pre class="vim" style="font-family:monospace;"><span style="color: #000000;">:</span>UBRegexSearch <span style="color: #000000;">&#91;</span><span style="color: #000000;">^</span>\<span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span>UltraBlog\<span style="color: #000000;">.</span>vim<span style="color: #000000;">&#91;</span><span style="color: #000000;">^</span>\<span style="color: #000000;">&#93;</span><span style="color: #000000;">&#93;</span></pre></div></div>

<h2>批量替換</h2>
<p>在我換過新域名後，我就覺得這個功能很有必要了：</p>

<div class="wp_syntax"><div class="code"><pre class="vim" style="font-family:monospace;"><span style="color: #000000;">:</span>UBReplace https<span style="color: #000000;">://</span>sinolog<span style="color: #000000;">.</span>it https<span style="color: #000000;">://</span>0x3f<span style="color: #000000;">.</span>org</pre></div></div>

<p>包含第一個參數內容並被替換的文章數目會顯示在Vim的命令輸出緩衝區中。</p>
<p>有了前面實現全文檢索支持正則表達式的嘗試，再實現支持正則表達式的批量替換就容易多了：</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #808080; font-style: italic;"># 轉換字符串成raw格式的函數</span>
<span style="color: #ff7700;font-weight:bold;">def</span> raw<span style="color: black;">&#40;</span>text<span style="color: black;">&#41;</span>:
    <span style="color: #483d8b;">&quot;&quot;&quot;Returns a raw string representation of text&quot;&quot;&quot;</span>
    escape_dict=<span style="color: black;">&#123;</span><span style="color: #483d8b;">'<span style="color: #000099; font-weight: bold;">\a</span>'</span>:r<span style="color: #483d8b;">'<span style="color: #000099; font-weight: bold;">\a</span>'</span>, <span style="color: #483d8b;">'<span style="color: #000099; font-weight: bold;">\b</span>'</span>:r<span style="color: #483d8b;">'<span style="color: #000099; font-weight: bold;">\b</span>'</span>, <span style="color: #483d8b;">'<span style="color: #000099; font-weight: bold;">\c</span>'</span>:r<span style="color: #483d8b;">'<span style="color: #000099; font-weight: bold;">\c</span>'</span>, <span style="color: #483d8b;">'<span style="color: #000099; font-weight: bold;">\f</span>'</span>:r<span style="color: #483d8b;">'<span style="color: #000099; font-weight: bold;">\f</span>'</span>, <span style="color: #483d8b;">'<span style="color: #000099; font-weight: bold;">\n</span>'</span>:r<span style="color: #483d8b;">'<span style="color: #000099; font-weight: bold;">\n</span>'</span>,
               <span style="color: #483d8b;">'<span style="color: #000099; font-weight: bold;">\r</span>'</span>:r<span style="color: #483d8b;">'<span style="color: #000099; font-weight: bold;">\r</span>'</span>, <span style="color: #483d8b;">'<span style="color: #000099; font-weight: bold;">\t</span>'</span>:r<span style="color: #483d8b;">'<span style="color: #000099; font-weight: bold;">\t</span>'</span>, <span style="color: #483d8b;">'<span style="color: #000099; font-weight: bold;">\v</span>'</span>:r<span style="color: #483d8b;">'<span style="color: #000099; font-weight: bold;">\v</span>'</span>, <span style="color: #483d8b;">'<span style="color: #000099; font-weight: bold;">\'</span>'</span>:r<span style="color: #483d8b;">'<span style="color: #000099; font-weight: bold;">\'</span>'</span>, <span style="color: #483d8b;">'<span style="color: #000099; font-weight: bold;">\&quot;</span>'</span>:r<span style="color: #483d8b;">'<span style="color: #000099; font-weight: bold;">\&quot;</span>'</span>,
               <span style="color: #483d8b;">'<span style="color: #000099; font-weight: bold;">\0</span>'</span>:r<span style="color: #483d8b;">'<span style="color: #000099; font-weight: bold;">\0</span>'</span>, <span style="color: #483d8b;">'<span style="color: #000099; font-weight: bold;">\1</span>'</span>:r<span style="color: #483d8b;">'<span style="color: #000099; font-weight: bold;">\1</span>'</span>, <span style="color: #483d8b;">'<span style="color: #000099; font-weight: bold;">\2</span>'</span>:r<span style="color: #483d8b;">'<span style="color: #000099; font-weight: bold;">\2</span>'</span>, <span style="color: #483d8b;">'<span style="color: #000099; font-weight: bold;">\3</span>'</span>:r<span style="color: #483d8b;">'<span style="color: #000099; font-weight: bold;">\3</span>'</span>, <span style="color: #483d8b;">'<span style="color: #000099; font-weight: bold;">\4</span>'</span>:r<span style="color: #483d8b;">'<span style="color: #000099; font-weight: bold;">\4</span>'</span>,
               <span style="color: #483d8b;">'<span style="color: #000099; font-weight: bold;">\5</span>'</span>:r<span style="color: #483d8b;">'<span style="color: #000099; font-weight: bold;">\5</span>'</span>, <span style="color: #483d8b;">'<span style="color: #000099; font-weight: bold;">\6</span>'</span>:r<span style="color: #483d8b;">'<span style="color: #000099; font-weight: bold;">\6</span>'</span>, <span style="color: #483d8b;">'<span style="color: #000099; font-weight: bold;">\7</span>'</span>:r<span style="color: #483d8b;">'<span style="color: #000099; font-weight: bold;">\7</span>'</span>, <span style="color: #483d8b;">'<span style="color: #000099; font-weight: bold;">\8</span>'</span>:r<span style="color: #483d8b;">'<span style="color: #000099; font-weight: bold;">\8</span>'</span>, <span style="color: #483d8b;">'<span style="color: #000099; font-weight: bold;">\9</span>'</span>:r<span style="color: #483d8b;">'<span style="color: #000099; font-weight: bold;">\9</span>'</span><span style="color: black;">&#125;</span>
    <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #483d8b;">&quot;&quot;</span>.<span style="color: black;">join</span><span style="color: black;">&#40;</span><span style="color: black;">&#91;</span>escape_dict.<span style="color: black;">get</span><span style="color: black;">&#40;</span>char,char<span style="color: black;">&#41;</span> <span style="color: #ff7700;font-weight:bold;">for</span> char <span style="color: #ff7700;font-weight:bold;">in</span> text<span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># 使用正則表達式替換字符串的函數</span>
<span style="color: #ff7700;font-weight:bold;">def</span> regex_replace<span style="color: black;">&#40;</span><span style="color: #dc143c;">string</span>, expr, repl<span style="color: black;">&#41;</span>:
    <span style="color: #483d8b;">&quot;&quot;&quot;Do substitutions on the string for repls matching the expr&quot;&quot;&quot;</span>
    r = <span style="color: #dc143c;">re</span>.<span style="color: #008000;">compile</span><span style="color: black;">&#40;</span>raw<span style="color: black;">&#40;</span>expr<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">return</span> r.<span style="color: black;">sub</span><span style="color: black;">&#40;</span>repl, <span style="color: #dc143c;">string</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># 在數據庫中註冊這個函數</span>
conn.<span style="color: black;">connection</span>.<span style="color: black;">create_function</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'regex_replace'</span>, <span style="color: #ff4500;">3</span>, regex_replace<span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># 在SQL語句中使用這個函數</span>
sql_replace = <span style="color: #483d8b;">&quot;update post set title=regex_replace(title,:needle,:replacement),content=regex_replace(content,:needle,:replacement)&quot;</span>
conn.<span style="color: black;">execute</span><span style="color: black;">&#40;</span>sql_replace, <span style="color: black;">&#123;</span><span style="color: #483d8b;">'needle'</span>:r<span style="color: #483d8b;">'<span style="color: #000099; font-weight: bold;">\b</span>abc<span style="color: #000099; font-weight: bold;">\b</span>'</span>, <span style="color: #483d8b;">'replacement'</span>:<span style="color: #483d8b;">'xyz'</span><span style="color: black;">&#125;</span><span style="color: black;">&#41;</span></pre></div></div>

<p>最終，這個命令是這樣的：</p>

<div class="wp_syntax"><div class="code"><pre class="vim" style="font-family:monospace;"><span style="color: #000000;">:</span>UBRegexReplace \babc\b xyz</pre></div></div>

<p>實現批量替換容易，但要解決由此引出的一個問題就費周折了，就是批量替換過文章內容後怎樣和博客同步的問題，現在我還沒有好的想法，留作以後實現。</p>
<h2>調試模式</h2>
<p>開啟調試模式可以將所有被執行的SQL語句顯示在Vim的命令輸出緩衝區中，在有異常拋出時，也可以顯示堆棧信息。由於開啟調試模式既不需要修改代碼，也不需要重啟Vim，這可以極大地方便開發時對UltraBlog.vim的調試，也能使用戶提交問題時能反饋更多更詳細的信息。</p>
<p>以下三個命令用於控制調試模式的開啟狀態：</p>
<ul>
<li><code>:UBEnableDebug</code></li>
<li><code>:UBDisableDebug</code></li>
<li><code>:UBToggleDebug</code></li>
</ul>
<h2>其它內容</h2>
<p>本次更新的內容如下：</p>
<ul>
<li>Feature: Add the command <strong>:UBRegexSearch</strong>, doing full-text searches with regular expressions !</li>
<li>Feature: Add the command <strong>:UBReplace</strong>, doing full-text substitutions.</li>
<li>Feature: Add the command <strong>:UBRegexReplace</strong>, doing full-text substitutions with regular expressions.</li>
<li>Feature: Add commands <strong>:UBEnableDebug</strong>, <strong>:UBDisableDebug</strong>, <strong>:UBToggleDebug</strong> and an option <strong>ub_debug</strong>. In debug mode, SQL statements and stack traces of exceptions will be displayed.</li>
<li>Change:  Undo keywords highlighting after executing <strong>:UBList</strong>.</li>
<li>Bugfix:  Exceptions raised when opening the current item under cursor in item lists if the option <strong>ub_hotkey_save_current_item</strong> has not been set. Now this options comes with a default value.</li>
</ul>
<p>關於UB的詳細信息在<a href="http://0x3f.org/?p=1894">這裡</a>。</p>
<p>Posted via <a href="http://0x3f.org/?p=1894">UltraBlog.vim</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://0x3f.org/?feed=rss2&#038;p=2205</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>慎用PHP的fsockopen()連接localhost</title>
		<link>http://0x3f.org/?p=2200</link>
		<comments>http://0x3f.org/?p=2200#comments</comments>
		<pubDate>Tue, 24 Apr 2012 15:00:31 +0000</pubDate>
		<dc:creator>lenin</dc:creator>
				<category><![CDATA[編程]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[性能]]></category>

		<guid isPermaLink="false">http://0x3f.org/?p=2200</guid>
		<description><![CDATA[版權所有：晴耕雨讀@【牧碼志】，如需轉載，請注明出處。 原文地址：http://0x3f.org/?p=2200 使用xdebug跟蹤程序性能時發現fsockopen()函數耗時一秒，單步調試發現實際在這裡超時了，而超時時間設置的就是一秒。 如果把目標地址由原來的“localhost”換成“127.0.0.1”，馬上就可以連上。在PHP的Bug跟蹤系統中找到這個問題： https://bugs.php.net/bug.php?id=50953 看起來像是在5.2.12以後已修改，但我發現在5.2.17中仍存在這個問題。另有說法在5.3中不存在這個問題，我沒有測試。 Posted via UltraBlog.vim.]]></description>
			<content:encoded><![CDATA[<div style="margin:15px 0px; font-style: italic; background-color:#F3F7FC; padding: 10px;">
版權所有：晴耕雨讀@<a href="http://0x3f.org/">【牧碼志】</a>，如需轉載，請注明出處。<br />
原文地址：<a href="http://0x3f.org/?p=2200">http://0x3f.org/?p=2200</a>
</div>
<p>使用xdebug跟蹤程序性能時發現fsockopen()函數耗時一秒，單步調試發現實際在這裡超時了，而超時時間設置的就是一秒。</p>
<p>如果把目標地址由原來的“localhost”換成“127.0.0.1”，馬上就可以連上。在PHP的Bug跟蹤系統中找到這個問題：</p>
<p>https://bugs.php.net/bug.php?id=50953</p>
<p>看起來像是在5.2.12以後已修改，但我發現在5.2.17中仍存在這個問題。另有說法在5.3中不存在這個問題，我沒有測試。</p>
<p>Posted via <a href="http://0x3f.org/?p=1894">UltraBlog.vim</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://0x3f.org/?feed=rss2&#038;p=2200</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>PHP大量常量應集中使用APC定義</title>
		<link>http://0x3f.org/?p=2198</link>
		<comments>http://0x3f.org/?p=2198#comments</comments>
		<pubDate>Tue, 24 Apr 2012 14:26:10 +0000</pubDate>
		<dc:creator>lenin</dc:creator>
				<category><![CDATA[編程]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[性能]]></category>

		<guid isPermaLink="false">http://0x3f.org/?p=2198</guid>
		<description><![CDATA[版權所有：晴耕雨讀@【牧碼志】，如需轉載，請注明出處。 原文地址：http://0x3f.org/?p=2198 用xdebug跟蹤程序性能時，發現一個定義了約七百個常量的文件，include_once()時消耗約六十毫秒的時間。事實上define()的效率比較低，如果需要定義大量常量，使用APC擴展提供的apc_define_constants()效果會好得多。 下面是對這個文件改造前後效率跟蹤結果的對比： define()方案（ms） apc_define_constants()方案（ms） 啟動Web服務和PHP，第一次運行 58 4 不重啟Web服務和PHP，第二次運行 65 1 不重啟Web服務和PHP，第三次運行 66 1 由此可見，apc_define_constants()不但在初次調用時效能就超過define()，而且由於APC自身的緩存功能，在後續調用時效率會有進一步的提高，而define()基本沒有變化。 以下是apc_define_constants()的代碼示例： if&#40;!apc_load_constants&#40;'my_constants'&#41;&#41; &#123; $constants = array&#40; 'ONE' =&#62; 1, 'TWO' =&#62; 2, 'THREE' =&#62; 3, &#41;; apc_define_constants&#40;'my_constants', $constants&#41;; &#125; Posted via UltraBlog.vim.]]></description>
			<content:encoded><![CDATA[<div style="margin:15px 0px; font-style: italic; background-color:#F3F7FC; padding: 10px;">
版權所有：晴耕雨讀@<a href="http://0x3f.org/">【牧碼志】</a>，如需轉載，請注明出處。<br />
原文地址：<a href="http://0x3f.org/?p=2198">http://0x3f.org/?p=2198</a>
</div>
<p>用xdebug跟蹤程序性能時，發現一個定義了約七百個常量的文件，include_once()時消耗約六十毫秒的時間。事實上define()的效率比較低，如果需要定義大量常量，使用APC擴展提供的apc_define_constants()效果會好得多。</p>
<p>下面是對這個文件改造前後效率跟蹤結果的對比：</p>
<table>
<tr>
<th></th>
<th>define()方案（ms）</th>
<th>apc_define_constants()方案（ms）</th>
</tr>
<tr>
<td>啟動Web服務和PHP，第一次運行</td>
<td>58</td>
<td>4</td>
</tr>
<tr>
<td>不重啟Web服務和PHP，第二次運行</td>
<td>65</td>
<td>1</td>
</tr>
<tr>
<td>不重啟Web服務和PHP，第三次運行</td>
<td>66</td>
<td>1</td>
</tr>
</table>
<p>由此可見，apc_define_constants()不但在初次調用時效能就超過define()，而且由於APC自身的緩存功能，在後續調用時效率會有進一步的提高，而define()基本沒有變化。</p>
<p>以下是apc_define_constants()的代碼示例：</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>apc_load_constants<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'my_constants'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$constants</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
        <span style="color: #0000ff;">'ONE'</span>   <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'TWO'</span>   <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'THREE'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">3</span><span style="color: #339933;">,</span>
    <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    apc_define_constants<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'my_constants'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$constants</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Posted via <a href="http://0x3f.org/?p=1894">UltraBlog.vim</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://0x3f.org/?feed=rss2&#038;p=2198</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>UltraBlog.vim v3.3.0：I18N、超時時間和其它</title>
		<link>http://0x3f.org/?p=2193</link>
		<comments>http://0x3f.org/?p=2193#comments</comments>
		<pubDate>Sun, 15 Apr 2012 15:41:46 +0000</pubDate>
		<dc:creator>lenin</dc:creator>
				<category><![CDATA[編程]]></category>
		<category><![CDATA[Plugin]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[UltraBlog.vim]]></category>
		<category><![CDATA[Vim]]></category>
		<category><![CDATA[博客]]></category>

		<guid isPermaLink="false">http://0x3f.org/?p=2193</guid>
		<description><![CDATA[版權所有：晴耕雨讀@【牧碼志】，如需轉載，請注明出處。 原文地址：http://0x3f.org/?p=2193 UB一歲了，這算是個週年紀念版吧。 在這一年裡，因為各種原因，博客寫得沒有以前多了，但每篇都是用這個插件寫的，我覺得很好用。斷斷續續地做了一些修改，因為一切都是一個人在做，所以大的變化不多。這次這個版本裡有些改動還是要感謝荒野無燈童鞋，他的建議很大程度上催生了這個新版本：比如加入保存命令的熱鍵映射選項，這樣可以使用保存普通文件的熱鍵保存UB中的內容，我自己以前也經常習慣性地用錯熱鍵；還有socket超時時間的選項，最近我這裡也經常性地在UB操作博客時報超時的異常，荒童鞋關於自定義socket超時時間的建議一語驚醒了夢中人。 此外，似乎與近期Vim的一次升級有關，在Vim中調用Python接口打印任何內容到標準IO都會導致Vim崩潰，所以這次把所有的輸出都改成了調用Vim的輸出命令來做。 本次修改的內容中，還有一項比較重要的內容就是實現了國際化，目前只提供英文和簡體中文兩種語言，由於Vimball不能處理二進制文件，所以從此以後UB改用zip格式壓縮包打包。 以下是本次更新的詳細內容： Feature: Add i18n support ! Feature: Add a new option ub_hotkey_save_current_item, users can define their own hotkey for :UBSave. Feature: Add a new option ub_socket_timeout, users can customize the timeout period in seconds, useful for slow networks. Bugfix: Exception raised when one event is processed by more than [...]]]></description>
			<content:encoded><![CDATA[<div style="margin:15px 0px; font-style: italic; background-color:#F3F7FC; padding: 10px;">
版權所有：晴耕雨讀@<a href="http://0x3f.org/">【牧碼志】</a>，如需轉載，請注明出處。<br />
原文地址：<a href="http://0x3f.org/?p=2193">http://0x3f.org/?p=2193</a>
</div>
<p>UB一歲了，這算是個週年紀念版吧。</p>
<p>在這一年裡，因為各種原因，博客寫得沒有以前多了，但每篇都是用這個插件寫的，我覺得很好用。斷斷續續地做了一些修改，因為一切都是一個人在做，所以大的變化不多。這次這個版本裡有些改動還是要感謝<a href="http://ihacklog.com/">荒野無燈</a>童鞋，他的<a href="https://github.com/xbot/UltraBlog.vim/issues/3">建議</a>很大程度上催生了這個新版本：比如加入保存命令的熱鍵映射選項，這樣可以使用保存普通文件的熱鍵保存UB中的內容，我自己以前也經常習慣性地用錯熱鍵；還有socket超時時間的選項，最近我這裡也經常性地在UB操作博客時報超時的異常，荒童鞋關於自定義socket超時時間的建議一語驚醒了夢中人。</p>
<p>此外，似乎與近期Vim的一次升級<a href="http://comments.gmane.org/gmane.editors.vim.devel/34092">有關</a>，在Vim中調用Python接口打印任何內容到標準IO都會導致Vim崩潰，所以這次把所有的輸出都改成了調用Vim的輸出命令來做。</p>
<p>本次修改的內容中，還有一項比較重要的內容就是實現了國際化，目前只提供英文和簡體中文兩種語言，由於Vimball不能處理二進制文件，所以從此以後UB改用zip格式壓縮包打包。</p>
<p>以下是本次更新的詳細內容：</p>
<ul>
<li>Feature: Add i18n support !</li>
<li>Feature: Add a new option <strong>ub_hotkey_save_current_item</strong>, users can define their own hotkey for <strong>:UBSave</strong>.</li>
<li>Feature: Add a new option <strong>ub_socket_timeout</strong>, users can customize the timeout period in seconds, useful for slow networks.</li>
<li>Bugfix:  Exception raised when one event is processed by more than one event handlers.</li>
<li>Change:  Echoing messages now uses the command :echoerr instead of python&#8217;s sys.stderr, because Vim crashes on this due to an upgrade recently.</li>
<li>Change:  Change commands <strong>:UBSave</strong>, <strong>:UBSend</strong>, <strong>:UBUpload</strong>, <strong>:UBConv</strong>, <strong>:UBPreview</strong> to be available only in their effective views.</li>
<li>Change:  Lists are set nowrapped.</li>
<li>Bugfix:  Stop complaining &#8216;<strong>_pop from empty list_</strong>&#8216; while doing almost everything.</li>
</ul>
<p>關於UB的詳細信息在<a href="http://0x3f.org/?p=1894">這裡</a>。</p>
<p>Posted via <a href="http://0x3f.org/?p=1894">UltraBlog.vim</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://0x3f.org/?feed=rss2&#038;p=2193</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>在Archlinux下使用thinkfan控制Thinkpad x220的風扇轉速</title>
		<link>http://0x3f.org/?p=2175</link>
		<comments>http://0x3f.org/?p=2175#comments</comments>
		<pubDate>Sat, 14 Apr 2012 11:09:42 +0000</pubDate>
		<dc:creator>lenin</dc:creator>
				<category><![CDATA[計算機]]></category>
		<category><![CDATA[Archlinux]]></category>
		<category><![CDATA[ThinkPad]]></category>

		<guid isPermaLink="false">http://0x3f.org/?p=2175</guid>
		<description><![CDATA[版權所有：晴耕雨讀@【牧碼志】，如需轉載，請注明出處。 原文地址：http://0x3f.org/?p=2175 Archlinux目前對Thinkpad風扇的自動控制效果並不好，氣溫上升後很容易出現一直在三千多轉一直跑的問題。thinkfan是目前用的比較多的自動控制風扇轉速的程序，但Google到的配置thinkfan的文章大多語焉不詳，可能是跟具體的發行版有關，因為貌似有些發行版中安裝了thinkfan後會自動生成缺省的配置文件，但在目前的AUR中的包被安裝後卻沒有生成任何配置文件。 下面是我在Archlinux下配置的步驟，實際效果很好，現在只要不看視頻、不編譯程序、不啟動Chromium，一般是兩千九百轉的速度，安靜多了。 lm_sensors 安裝lm_sensors並執行命令初始化： # 一路回車 sudo sensors-detect 將sensors加入rc.conf中DAEMONS中，開機啟動。 启动sensors服务： sudo rc.d start sensors thinkpad_acpi 修改文件“/etc/modprobe.d/thinkpad_acpi.conf”： options thinkpad_acpi fan_control=1 我這裡雖沒有把thinkpad_acpi加到rc.conf的MODULES中，但lsmod也是可以看到它的，說明還是自動啟用了。要使上面這項配置生效，必須重啟電腦，我這裡重啟thinkpad_acpi模塊時報錯說該模塊正在被使用。 thinkfan 安裝thinkfan並修改文件“/etc/default/thinkfan”： START=yes 修改文件“/etc/thinkfan.conf”： sensor /sys/class/hwmon/hwmon0/temp1_input (0, 0, 55) (1, 48, 60) (2, 50, 61) (3, 52, 63) (4, 56, 65) (5, 59, 66) (7, 63, 32767) 將thinkfan加入rc.conf的DAEMONS中，開機自動啟動。 啟動thinkfan： sudo thinkfan 查看狀態 # [...]]]></description>
			<content:encoded><![CDATA[<div style="margin:15px 0px; font-style: italic; background-color:#F3F7FC; padding: 10px;">
版權所有：晴耕雨讀@<a href="http://0x3f.org/">【牧碼志】</a>，如需轉載，請注明出處。<br />
原文地址：<a href="http://0x3f.org/?p=2175">http://0x3f.org/?p=2175</a>
</div>
<p>Archlinux目前對Thinkpad風扇的自動控制效果並不好，氣溫上升後很容易出現一直在三千多轉一直跑的問題。thinkfan是目前用的比較多的自動控制風扇轉速的程序，但Google到的配置thinkfan的文章大多語焉不詳，可能是跟具體的發行版有關，因為貌似有些發行版中安裝了thinkfan後會自動生成缺省的配置文件，但在目前的AUR中的包被安裝後卻沒有生成任何配置文件。</p>
<p>下面是我在Archlinux下配置的步驟，實際效果很好，現在只要不看視頻、不編譯程序、不啟動Chromium，一般是兩千九百轉的速度，安靜多了。</p>
<h2>lm_sensors</h2>
<p>安裝lm_sensors並執行命令初始化：</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;"># 一路回車</span>
<span style="color: #c20cb9; font-weight: bold;">sudo</span> sensors-detect</pre></div></div>

<p>將sensors加入rc.conf中DAEMONS中，開機啟動。</p>
<p>启动sensors服务：</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">sudo</span> rc.d start sensors</pre></div></div>

<h2>thinkpad_acpi</h2>
<p>修改文件“<strong>/etc/modprobe.d/thinkpad_acpi.conf</strong>”：</p>
<pre>
options thinkpad_acpi fan_control=1
</pre>
<p>我這裡雖沒有把thinkpad_acpi加到rc.conf的MODULES中，但lsmod也是可以看到它的，說明還是自動啟用了。要使上面這項配置生效，必須重啟電腦，我這裡重啟thinkpad_acpi模塊時報錯說該模塊正在被使用。</p>
<h2>thinkfan</h2>
<p>安裝thinkfan並修改文件“<strong>/etc/default/thinkfan</strong>”：</p>
<pre>
START=yes
</pre>
<p>修改文件“<strong>/etc/thinkfan.conf</strong>”：</p>
<pre>
sensor /sys/class/hwmon/hwmon0/temp1_input

(0,  0, 55)
(1, 48, 60)
(2, 50, 61)
(3, 52, 63)
(4, 56, 65)
(5, 59, 66)
(7, 63, 32767)
</pre>
<p>將thinkfan加入rc.conf的DAEMONS中，開機自動啟動。</p>
<p>啟動thinkfan：</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">sudo</span> thinkfan</pre></div></div>

<h2>查看狀態</h2>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;"># 看CPU温度和风扇转速</span>
sensors
&nbsp;
<span style="color: #666666; font-style: italic;"># 看風扇詳細信息</span>
<span style="color: #c20cb9; font-weight: bold;">cat</span> <span style="color: #000000; font-weight: bold;">/</span>proc<span style="color: #000000; font-weight: bold;">/</span>acpi<span style="color: #000000; font-weight: bold;">/</span>ibm<span style="color: #000000; font-weight: bold;">/</span>fan</pre></div></div>

<p>Posted via <a href="http://0x3f.org/?p=1894">UltraBlog.vim</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://0x3f.org/?feed=rss2&#038;p=2175</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>TurboCRM Cluster Express v1.0 釋出</title>
		<link>http://0x3f.org/?p=2171</link>
		<comments>http://0x3f.org/?p=2171#comments</comments>
		<pubDate>Sun, 08 Apr 2012 07:17:33 +0000</pubDate>
		<dc:creator>lenin</dc:creator>
				<category><![CDATA[編程]]></category>
		<category><![CDATA[BASH]]></category>
		<category><![CDATA[CRM]]></category>
		<category><![CDATA[PyGTK]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[雲計算]]></category>

		<guid isPermaLink="false">http://0x3f.org/?p=2171</guid>
		<description><![CDATA[版權所有：晴耕雨讀@【牧碼志】，如需轉載，請注明出處。 原文地址：http://0x3f.org/?p=2171 TurboCRM Cluster Express是TurboCRM的集群實施工具。 去年我曾經寫過一個配置集群的Bash腳本“cluster.sh”，TCE是對這個腳本的圖形界面封裝，通過可視化的操作簡化集群的實施工作。 TCE的最新版本和使用說明可以在這裡找到。 Posted via UltraBlog.vim.]]></description>
			<content:encoded><![CDATA[<div style="margin:15px 0px; font-style: italic; background-color:#F3F7FC; padding: 10px;">
版權所有：晴耕雨讀@<a href="http://0x3f.org/">【牧碼志】</a>，如需轉載，請注明出處。<br />
原文地址：<a href="http://0x3f.org/?p=2171">http://0x3f.org/?p=2171</a>
</div>
<p>TurboCRM Cluster Express是TurboCRM的集群實施工具。</p>
<p><img src="http://pic.yupoo.com/leninlee/BSfJUW1x/medium.jpg" alt="illustration" /></p>
<p>去年我曾經寫過一個配置集群的Bash腳本“cluster.sh”，TCE是對這個腳本的圖形界面封裝，通過可視化的操作簡化集群的實施工作。</p>
<p>TCE的最新版本和使用說明可以在<a href="http://xbot.github.com/TurboCRM-Cluster-Express/">這裡</a>找到。</p>
<p>Posted via <a href="http://0x3f.org/?p=1894">UltraBlog.vim</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://0x3f.org/?feed=rss2&#038;p=2171</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>禁止屏幕在播放視頻時自動關閉</title>
		<link>http://0x3f.org/?p=2167</link>
		<comments>http://0x3f.org/?p=2167#comments</comments>
		<pubDate>Wed, 04 Apr 2012 09:31:59 +0000</pubDate>
		<dc:creator>lenin</dc:creator>
				<category><![CDATA[編程]]></category>
		<category><![CDATA[BASH]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[腳本]]></category>

		<guid isPermaLink="false">http://0x3f.org/?p=2167</guid>
		<description><![CDATA[版權所有：晴耕雨讀@【牧碼志】，如需轉載，請注明出處。 原文地址：http://0x3f.org/?p=2167 貌似Lose系統下看在線視頻的時候不會超時自動關閉屏幕，但是我在Arch下就一直晃鼠標、按鍵盤來著，這嚴重違反了DRY原則。於是昨天終於寫了這個腳本，當全屏播放視頻的時候，關閉屏保和顯示器的節能特性，否則則激活它們。由於是在X的級別實現，理論上應當適合所有桌面環境。 #!/bin/bash # Baby-sitter of the monitor's DPMS &#160; idle_period=60 ss_switch_off=0 ss_is_off=0 &#160; while true; do # Read DPMS state xset -q&#124;grep &#34;DPMS is Disabled&#34; &#62; /dev/null &#38;&#38; ss_is_off=1 &#124;&#124; ss_is_off=0 # Get pid of the current window active_window_id=`xprop -root &#124; grep &#34;_NET_ACTIVE_WINDOW(WINDOW)&#34; &#124; cut -d&#34; &#34; -f5` decimal_id=`xprop -id $active_window_id &#124; grep [...]]]></description>
			<content:encoded><![CDATA[<div style="margin:15px 0px; font-style: italic; background-color:#F3F7FC; padding: 10px;">
版權所有：晴耕雨讀@<a href="http://0x3f.org/">【牧碼志】</a>，如需轉載，請注明出處。<br />
原文地址：<a href="http://0x3f.org/?p=2167">http://0x3f.org/?p=2167</a>
</div>
<p>貌似Lose系統下看在線視頻的時候不會超時自動關閉屏幕，但是我在Arch下就一直晃鼠標、按鍵盤來著，這嚴重違反了DRY原則。於是昨天終於寫了這個腳本，當全屏播放視頻的時候，關閉屏保和顯示器的節能特性，否則則激活它們。由於是在X的級別實現，理論上應當適合所有桌面環境。</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/bin/bash</span>
<span style="color: #666666; font-style: italic;"># Baby-sitter of the monitor's DPMS</span>
&nbsp;
<span style="color: #007800;">idle_period</span>=<span style="color: #000000;">60</span>
<span style="color: #007800;">ss_switch_off</span>=<span style="color: #000000;">0</span>
<span style="color: #007800;">ss_is_off</span>=<span style="color: #000000;">0</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">while</span> <span style="color: #c20cb9; font-weight: bold;">true</span>; <span style="color: #000000; font-weight: bold;">do</span>
    <span style="color: #666666; font-style: italic;"># Read DPMS state</span>
    <span style="color: #c20cb9; font-weight: bold;">xset</span> <span style="color: #660033;">-q</span><span style="color: #000000; font-weight: bold;">|</span><span style="color: #c20cb9; font-weight: bold;">grep</span> <span style="color: #ff0000;">&quot;DPMS is Disabled&quot;</span> <span style="color: #000000; font-weight: bold;">&gt;</span> <span style="color: #000000; font-weight: bold;">/</span>dev<span style="color: #000000; font-weight: bold;">/</span>null <span style="color: #000000; font-weight: bold;">&amp;&amp;</span> <span style="color: #007800;">ss_is_off</span>=<span style="color: #000000;">1</span> <span style="color: #000000; font-weight: bold;">||</span> <span style="color: #007800;">ss_is_off</span>=<span style="color: #000000;">0</span>
    <span style="color: #666666; font-style: italic;"># Get pid of the current window</span>
    <span style="color: #007800;">active_window_id</span>=<span style="color: #000000; font-weight: bold;">`</span>xprop <span style="color: #660033;">-root</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">grep</span> <span style="color: #ff0000;">&quot;_NET_ACTIVE_WINDOW(WINDOW)&quot;</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">cut</span> <span style="color: #660033;">-d</span><span style="color: #ff0000;">&quot; &quot;</span> -f5<span style="color: #000000; font-weight: bold;">`</span>
    <span style="color: #007800;">decimal_id</span>=<span style="color: #000000; font-weight: bold;">`</span>xprop <span style="color: #660033;">-id</span> <span style="color: #007800;">$active_window_id</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">grep</span> PID <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">cut</span> <span style="color: #660033;">-d</span><span style="color: #ff0000;">&quot; &quot;</span> -f3<span style="color: #000000; font-weight: bold;">`</span>
    <span style="color: #666666; font-style: italic;"># Traverse all libflashplayer.so</span>
    <span style="color: #000000; font-weight: bold;">for</span> pid <span style="color: #000000; font-weight: bold;">in</span> <span style="color: #000000; font-weight: bold;">`</span><span style="color: #c20cb9; font-weight: bold;">ps</span> <span style="color: #660033;">-ef</span><span style="color: #000000; font-weight: bold;">|</span><span style="color: #c20cb9; font-weight: bold;">grep</span> <span style="color: #660033;">-v</span> <span style="color: #c20cb9; font-weight: bold;">grep</span><span style="color: #000000; font-weight: bold;">|</span><span style="color: #c20cb9; font-weight: bold;">grep</span> libflashplayer.so<span style="color: #000000; font-weight: bold;">|</span><span style="color: #c20cb9; font-weight: bold;">awk</span> <span style="color: #ff0000;">'{print $2}'</span><span style="color: #000000; font-weight: bold;">`</span>; <span style="color: #000000; font-weight: bold;">do</span>
        <span style="color: #666666; font-style: italic;"># If the current window is libflashplayer.so</span>
        <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$pid</span>&quot;</span> <span style="color: #660033;">-eq</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$decimal_id</span>&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span>
            <span style="color: #007800;">ss_switch_off</span>=<span style="color: #000000;">1</span>
            <span style="color: #7a0874; font-weight: bold;">break</span>
        <span style="color: #000000; font-weight: bold;">else</span>
            <span style="color: #007800;">ss_switch_off</span>=<span style="color: #000000;">0</span>
        <span style="color: #000000; font-weight: bold;">fi</span>
    <span style="color: #000000; font-weight: bold;">done</span>
    <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #007800;">$ss_switch_off</span> <span style="color: #660033;">-eq</span> <span style="color: #000000;">1</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span>
        <span style="color: #666666; font-style: italic;"># Turn off DPMS</span>
        <span style="color: #7a0874; font-weight: bold;">echo</span> Turn off DPMS
        <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #007800;">$ss_is_off</span> <span style="color: #660033;">-eq</span> <span style="color: #000000;">0</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span>
            <span style="color: #7a0874; font-weight: bold;">echo</span> Action
            <span style="color: #c20cb9; font-weight: bold;">xset</span> s off
            <span style="color: #c20cb9; font-weight: bold;">xset</span> <span style="color: #660033;">-dpms</span>
        <span style="color: #000000; font-weight: bold;">fi</span>
    <span style="color: #000000; font-weight: bold;">else</span>
        <span style="color: #666666; font-style: italic;"># Turn on DPMS</span>
        <span style="color: #7a0874; font-weight: bold;">echo</span> Turn on DPMS
        <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #007800;">$ss_is_off</span> <span style="color: #660033;">-eq</span> <span style="color: #000000;">1</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span>
            <span style="color: #7a0874; font-weight: bold;">echo</span> Action
            <span style="color: #c20cb9; font-weight: bold;">xset</span> +dpms
            <span style="color: #c20cb9; font-weight: bold;">xset</span> s on
        <span style="color: #000000; font-weight: bold;">fi</span>
    <span style="color: #000000; font-weight: bold;">fi</span>
    <span style="color: #c20cb9; font-weight: bold;">sleep</span> <span style="color: #007800;">$idle_period</span>
<span style="color: #000000; font-weight: bold;">done</span></pre></div></div>

<p>似乎對非全屏播放的情況沒有什麼好方法。</p>
<p>Posted via <a href="http://0x3f.org/?p=1894">UltraBlog.vim</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://0x3f.org/?feed=rss2&#038;p=2167</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>關於PyGTK.CellRenderer的回調方法中聚焦異常的問題</title>
		<link>http://0x3f.org/?p=2164</link>
		<comments>http://0x3f.org/?p=2164#comments</comments>
		<pubDate>Sun, 25 Mar 2012 15:40:45 +0000</pubDate>
		<dc:creator>lenin</dc:creator>
				<category><![CDATA[編程]]></category>
		<category><![CDATA[PyGTK]]></category>
		<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://0x3f.org/?p=2164</guid>
		<description><![CDATA[版權所有：晴耕雨讀@【牧碼志】，如需轉載，請注明出處。 原文地址：http://0x3f.org/?p=2164 遇到這樣一個問題： 在pygtk.TreeView中，需要在一個Cell的值被修改後做一個校驗，如果不合法，則重新聚焦該Cell並選中其中的內容。 示意代碼如下： # pygtk.CellRenderer的edited事件的回調方法 def onCellEdited&#40;self, cell, path, newText, userData&#41;: store,colNum = userData # 使用事件驅動的設計理念，創建一個自定義的事件 evt = ServerTypeChangedEvent&#40;self, cell, usrData&#41; &#160; # 事件入隊列 EventQueue.fireEvent&#40;evt&#41; # 處理事件隊列，如果截獲異常，提示並重新聚焦Cell try: EventQueue.processEvents&#40;&#41; except Exception,e: self.alert&#40;e.message&#41; col = self.treeView.get_column&#40;colNum-1&#41; # 聚焦Cell，並選中Cell中的內容 self.treeView.set_cursor_on_cell&#40;path, col, cell, True&#41; 關鍵的代碼在最後一行，通過調用TreeView的set_cursor_on_cell方法聚焦Cell並選中其內容。但就是這一步出了問題，報如下警告： GtkWarning: _gtk_tree_view_column_start_editing: assertion `tree_column->editable_widget == NULL&#8217; failed 此後整個TreeView的行為表現不正常，表現為可直接修改其它Cell的內容，且原Cell一直處於聚焦狀態。 此問題的原因是：edited事件在Cell的輸入控件被銷毀前就發出了，這時在事件的回調方法中重新聚焦該Cell就導致了這個問題（詳見這裡）。 解決方法是借助glib.idle_add函數，在PyGTK空閒的時候再調用set_cursor_on_cell方法，由於此時原控件已被銷毀，所以不會有問題。 修改後的代碼如下： [...]]]></description>
			<content:encoded><![CDATA[<div style="margin:15px 0px; font-style: italic; background-color:#F3F7FC; padding: 10px;">
版權所有：晴耕雨讀@<a href="http://0x3f.org/">【牧碼志】</a>，如需轉載，請注明出處。<br />
原文地址：<a href="http://0x3f.org/?p=2164">http://0x3f.org/?p=2164</a>
</div>
<p>遇到這樣一個問題：</p>
<p>在pygtk.TreeView中，需要在一個Cell的值被修改後做一個校驗，如果不合法，則重新聚焦該Cell並選中其中的內容。</p>
<p>示意代碼如下：</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #808080; font-style: italic;"># pygtk.CellRenderer的edited事件的回調方法</span>
<span style="color: #ff7700;font-weight:bold;">def</span> onCellEdited<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, cell, path, newText, userData<span style="color: black;">&#41;</span>:
    store,colNum = userData
    <span style="color: #808080; font-style: italic;"># 使用事件驅動的設計理念，創建一個自定義的事件</span>
    evt = ServerTypeChangedEvent<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, cell, usrData<span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;"># 事件入隊列</span>
    EventQueue.<span style="color: black;">fireEvent</span><span style="color: black;">&#40;</span>evt<span style="color: black;">&#41;</span>
    <span style="color: #808080; font-style: italic;"># 處理事件隊列，如果截獲異常，提示並重新聚焦Cell</span>
    <span style="color: #ff7700;font-weight:bold;">try</span>:
        EventQueue.<span style="color: black;">processEvents</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">except</span> <span style="color: #008000;">Exception</span>,e:
        <span style="color: #008000;">self</span>.<span style="color: black;">alert</span><span style="color: black;">&#40;</span>e.<span style="color: black;">message</span><span style="color: black;">&#41;</span>
        col = <span style="color: #008000;">self</span>.<span style="color: black;">treeView</span>.<span style="color: black;">get_column</span><span style="color: black;">&#40;</span>colNum-<span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span>
        <span style="color: #808080; font-style: italic;"># 聚焦Cell，並選中Cell中的內容</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">treeView</span>.<span style="color: black;">set_cursor_on_cell</span><span style="color: black;">&#40;</span>path, col, cell, <span style="color: #008000;">True</span><span style="color: black;">&#41;</span></pre></div></div>

<p>關鍵的代碼在最後一行，通過調用TreeView的set_cursor_on_cell方法聚焦Cell並選中其內容。但就是這一步出了問題，報如下警告：</p>
<blockquote>
<p>GtkWarning: _gtk_tree_view_column_start_editing: assertion `tree_column->editable_widget == NULL&#8217; failed</p>
</blockquote>
<p>此後整個TreeView的行為表現不正常，表現為可直接修改其它Cell的內容，且原Cell一直處於聚焦狀態。</p>
<p>此問題的原因是：edited事件在Cell的輸入控件被銷毀前就發出了，這時在事件的回調方法中重新聚焦該Cell就導致了這個問題（<em>詳見<a href="http://www.gtkforums.com/viewtopic.php?t=4619">這裡</a></em>）。</p>
<p>解決方法是借助glib.idle_add函數，在PyGTK空閒的時候再調用set_cursor_on_cell方法，由於此時原控件已被銷毀，所以不會有問題。</p>
<p>修改後的代碼如下：</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">def</span> onCellEdited<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, cell, path, newText, userData<span style="color: black;">&#41;</span>:
    store,colNum = userData
    evt = ServerTypeChangedEvent<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, cell, usrData<span style="color: black;">&#41;</span>
&nbsp;
    EventQueue.<span style="color: black;">fireEvent</span><span style="color: black;">&#40;</span>evt<span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">try</span>:
        EventQueue.<span style="color: black;">processEvents</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">except</span> <span style="color: #008000;">Exception</span>,e:
        <span style="color: #008000;">self</span>.<span style="color: black;">alert</span><span style="color: black;">&#40;</span>e.<span style="color: black;">message</span><span style="color: black;">&#41;</span>
        col = <span style="color: #008000;">self</span>.<span style="color: black;">treeView</span>.<span style="color: black;">get_column</span><span style="color: black;">&#40;</span>colNum-<span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span>
        <span style="color: #808080; font-style: italic;">#self.treeView.set_cursor_on_cell(path, col, cell, True)</span>
        <span style="color: #ff7700;font-weight:bold;">import</span> glib
        glib.<span style="color: black;">idle_add</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">treeView</span>.<span style="color: black;">set_cursor_on_cell</span>, path, col, cell, <span style="color: #008000;">True</span><span style="color: black;">&#41;</span></pre></div></div>

<p>Posted via <a href="http://0x3f.org/?p=1894">UltraBlog.vim</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://0x3f.org/?feed=rss2&#038;p=2164</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

