<?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>DailyHckr</title>
	<atom:link href="http://hylom.net/feed/" rel="self" type="application/rss+xml" />
	<link>http://hylom.net</link>
	<description>Hack, Hacker, Hackest</description>
	<lastBuildDate>Mon, 16 Jan 2012 13:32:56 +0000</lastBuildDate>
	<language>ja</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2</generator>
		<item>
		<title>Emacsのキーバインディング設定</title>
		<link>http://hylom.net/2012/01/16/emacs-keybinding-configs/</link>
		<comments>http://hylom.net/2012/01/16/emacs-keybinding-configs/#comments</comments>
		<pubDate>Mon, 16 Jan 2012 11:06:30 +0000</pubDate>
		<dc:creator>hylom</dc:creator>
				<category><![CDATA[未分類]]></category>

		<guid isPermaLink="false">http://hylom.net/?p=1521</guid>
		<description><![CDATA[　Emacsの設定ファイル（.emacsなど）でキーバインディングを設定する場合の作法はググると色々なものが出てくるのだが、最近ではkbd関数を利用するのが一般的なようだ。コントロールキー同時押しや特殊キーに関数を割り当てる場合、一昔前は分かりにくい表記で書いていたが、kbd関数を使うと割と簡潔にキーを指定できる。 　たとえばCtrl＋Shift＋Insertというキーに「add-anchor」関数を割り当てる場合は下記のようにする。 (global-set-key (kbd "C-S-&#60;insert&#62;") 'add-anchor) 　あと、global-set-key関数を使う以外に、define-key関数を「global-map」引数付きを使う人もいるようだが、こちらについてはどちらも意味的には同じ。たとえば上記の例をdefine-keyを使って書くと、次のようになる。 (define-keyglobal-map (kbd "C-S-&#60;insert&#62;") 'add-anchor) 　「global-map」の代わりに「current-global-map」を使っても、基本的には同じ。Emacsのマニュアル中、キーマップ周りは「Commands for Binding Keys」にあるのでこちらもご参照を。]]></description>
			<content:encoded><![CDATA[<p>　Emacsの設定ファイル（.emacsなど）でキーバインディングを設定する場合の作法はググると色々なものが出てくるのだが、最近では<a href="http://www.gnu.org/software/emacs/manual/html_node/elisp/Key-Sequences.html">kbd関数</a>を利用するのが一般的なようだ。コントロールキー同時押しや特殊キーに関数を割り当てる場合、一昔前は分かりにくい表記で書いていたが、kbd関数を使うと割と簡潔にキーを指定できる。</p>
<p>　たとえばCtrl＋Shift＋Insertというキーに「add-anchor」関数を割り当てる場合は下記のようにする。</p>
<pre>
(global-set-key (kbd "C-S-&lt;insert&gt;") 'add-anchor)
</pre>
<p>　あと、global-set-key関数を使う以外に、define-key関数を「global-map」引数付きを使う人もいるようだが、こちらについてはどちらも意味的には同じ。たとえば上記の例をdefine-keyを使って書くと、次のようになる。</p>
<pre>
(define-keyglobal-map (kbd "C-S-&lt;insert&gt;") 'add-anchor)
</pre>
<p>　「global-map」の代わりに「current-global-map」を使っても、基本的には同じ。Emacsのマニュアル中、キーマップ周りは「<a href="http://www.gnu.org/software/emacs/manual/html_node/elisp/Key-Binding-Commands.html">Commands for Binding Keys</a>」にあるのでこちらもご参照を。</p>
]]></content:encoded>
			<wfw:commentRss>http://hylom.net/2012/01/16/emacs-keybinding-configs/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>iMac（Early 2009、20インチ）の光学ドライブを交換する</title>
		<link>http://hylom.net/2011/12/13/changing-superdrive-on-imac-early-2009/</link>
		<comments>http://hylom.net/2011/12/13/changing-superdrive-on-imac-early-2009/#comments</comments>
		<pubDate>Tue, 13 Dec 2011 06:45:15 +0000</pubDate>
		<dc:creator>hylom</dc:creator>
				<category><![CDATA[Hacks]]></category>
		<category><![CDATA[hack]]></category>
		<category><![CDATA[hardware]]></category>
		<category><![CDATA[mac]]></category>

		<guid isPermaLink="false">http://hylom.net/?p=1514</guid>
		<description><![CDATA[　自宅のメインマシンは20インチiMac（Early 2009）なのだが、2年前くらいから光学ドライブの調子が悪い。このiMacは整備済み製品として購入したもので、HDDとメモリが増量されている。整備済み製品はときどき記載されているスペックよりも上のものが届くことがあってラッキー感があるのだが、1年間の特別保証が切れた直後あたりに調子が悪いことに気付いてしまった。 　現象としては、CDを挿入しても認識されず、ウンウンうなったあとで排出されるというもの。ちょうど手持ちの音楽CDをすべてリッピングしようと思ってたときで、1日に10枚ペースで200枚くらいのCDをリッピングしたのが良くなかったような気がする。しかも、ときどきは認識されるからタチが悪い。認識されても、リッピングされた音源にノイズが載るという現象が出るため実質的に使えない状態。 　有償修理に出すことも考えたのだが、お値段が数万円コースとなるため放置、外付けのドライブを付けてごまかしてたのだが、来年で購入してから3年目になるということもあり、買い換えを視野に入れて交換を行うことにした。 　作業手順はASCII.jpに載っているとおり。iFixitも写真が多くて参考になる。実際に作業してみて感じたコツだが、フレームや液晶の取り外しは本体を寝かせた状態ではなく、立てた状態で作業をしたほうがやりやすい。寝かせると自重で本体がやや歪むためのようだ。 　交換に使用したのは、Sony OptiarcのAD-7640S。元々搭載されているのはAD-5670Sというモデルだが、OEM専用なので入手が難しい。AD-7640SはVintage ComputerでもiMac用に販売されているし、2011年12月現在国内でも入手しやすい。自分は九十九電機で購入。 　スロットイン型のモデルだが、ベゼルがついているので取り外して使用する。また、左側にイジェクトスイッチが付いているが、内蔵で使う場合不要。自分ははさみで切り取ってしまった。単純なスイッチなので動作上は問題ないと思われる。 　さて、換装自体は無難に完了したのだが、どうもうまく動かなかった。ブート直後、丸いインジケータが表示されるまではディスクをセットできるのだが、OSの起動後はディスクを挿入しようとしてもドライブが吸い込んでくれない。OS上からドライブは正しく認識されているのだが。 　再度ばらしたり組み直したりと色々試行錯誤した結果、電源投入時にディスクをセットしておくと、起動後も問題なく動作するということが判明。また、電源投入後で動作しなくなった場合、ディスクをドライブに突っ込んだ状態でスリープ状態から復帰させるとディスクを吸い込み、その後利用できるようになることも分かった。 ちなみに純正のドライブでは、スリープからの復帰時にドライブの音が鳴らないようになっているが、本ドライブでは鳴る。どうもファームウェアの問題のような気がしているのだが、Appleが提供するSuperDriveファームウェア・アップデート3.0は適用できなかった。 ということで問題の解決に向けて依然情報収集中である。]]></description>
			<content:encoded><![CDATA[<p>　自宅のメインマシンは<a href="http://support.apple.com/kb/SP507?viewlocale=ja_JP">20インチiMac（Early 2009）</a>なのだが、2年前くらいから光学ドライブの調子が悪い。このiMacは<a href="http://store.apple.com/jp/browse/home/specialdeals/mac">整備済み製品</a>として購入したもので、HDDとメモリが増量されている。整備済み製品はときどき記載されているスペックよりも上のものが届くことがあってラッキー感があるのだが、1年間の特別保証が切れた直後あたりに調子が悪いことに気付いてしまった。</p>
<p>　現象としては、CDを挿入しても認識されず、ウンウンうなったあとで排出されるというもの。ちょうど手持ちの音楽CDをすべてリッピングしようと思ってたときで、1日に10枚ペースで200枚くらいのCDをリッピングしたのが良くなかったような気がする。しかも、ときどきは認識されるからタチが悪い。認識されても、リッピングされた音源にノイズが載るという現象が出るため実質的に使えない状態。</p>
<p>　有償修理に出すことも考えたのだが、お値段が数万円コースとなるため放置、外付けのドライブを付けてごまかしてたのだが、来年で購入してから3年目になるということもあり、買い換えを視野に入れて交換を行うことにした。</p>
<p>　作業手順は<a href="http://ascii.jp/elem/000/000/059/59034/">ASCII.jp</a>に載っているとおり。<a href="http://www.ifixit.com/Teardown/iMac-Intel-20-Inch-EMC-2266-Teardown/658/1">iFixit</a>も写真が多くて参考になる。実際に作業してみて感じたコツだが、フレームや液晶の取り外しは本体を寝かせた状態ではなく、立てた状態で作業をしたほうがやりやすい。寝かせると自重で本体がやや歪むためのようだ。</p>
<p>　交換に使用したのは、Sony OptiarcのAD-7640S。元々搭載されているのはAD-5670Sというモデルだが、OEM専用なので入手が難しい。AD-7640Sは<a href="http://www.vintagecomp.com/shop/shopdisplayproducts.asp?id=626&#038;cat=%8C%F5%2FiMac+Intel+%83V%83%8B%83o%81%5B%3Cbr%3E%0D%0AEarly+2009%2FLate+2009+%3Cbr%3E%0D%0A12%2E7mm%2FSATA">Vintage Computer</a>でもiMac用に販売されているし、2011年12月現在国内でも入手しやすい。自分は九十九電機で購入。</p>
<p>　スロットイン型のモデルだが、ベゼルがついているので取り外して使用する。また、左側にイジェクトスイッチが付いているが、内蔵で使う場合不要。自分ははさみで切り取ってしまった。単純なスイッチなので動作上は問題ないと思われる。</p>
<p>　さて、換装自体は無難に完了したのだが、どうもうまく動かなかった。ブート直後、丸いインジケータが表示されるまではディスクをセットできるのだが、OSの起動後はディスクを挿入しようとしてもドライブが吸い込んでくれない。OS上からドライブは正しく認識されているのだが。</p>
<p>　再度ばらしたり組み直したりと色々試行錯誤した結果、電源投入時にディスクをセットしておくと、起動後も問題なく動作するということが判明。また、電源投入後で動作しなくなった場合、ディスクをドライブに突っ込んだ状態でスリープ状態から復帰させるとディスクを吸い込み、その後利用できるようになることも分かった。</p>
<p>ちなみに純正のドライブでは、スリープからの復帰時にドライブの音が鳴らないようになっているが、本ドライブでは鳴る。どうもファームウェアの問題のような気がしているのだが、Appleが提供する<a href="http://support.apple.com/kb/DL892?viewlocale=ja_JP">SuperDriveファームウェア・アップデート3.0</a>は適用できなかった。</p>
<p>ということで問題の解決に向けて依然情報収集中である。</p>
]]></content:encoded>
			<wfw:commentRss>http://hylom.net/2011/12/13/changing-superdrive-on-imac-early-2009/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>VirtualBoxでGNOME Shellを使う（Fedora 16）</title>
		<link>http://hylom.net/2011/12/06/fedora16_on_virtualbox_with_gnome_shell/</link>
		<comments>http://hylom.net/2011/12/06/fedora16_on_virtualbox_with_gnome_shell/#comments</comments>
		<pubDate>Mon, 05 Dec 2011 16:31:57 +0000</pubDate>
		<dc:creator>hylom</dc:creator>
				<category><![CDATA[Hacks]]></category>
		<category><![CDATA[fedora]]></category>
		<category><![CDATA[gnome]]></category>
		<category><![CDATA[virtualbox]]></category>

		<guid isPermaLink="false">http://hylom.net/?p=1498</guid>
		<description><![CDATA[　VirtualBoxでFedora 16のデスクトップを使う場合、デフォルトだとGNOME Shellが利用できない。GNOME Shellの利用にはハードウェアアクセラレーションが必要なためだ。VirtualBoxの場合、Guest Additionをインストールすればハードウェアアクセラレーションが利用可能になり、GNOME Shellが利用可能になる。 VirtualBox Guest Additionのインストール 　基本的な設定はInstall Fedora 16 VirtualBox Guest Additions and Get Working Gnome Shell Inside Virtual Machineという記事にある通り。 1. 仮想マシンの設定の「ディスプレイ」項目で、ビデオメモリを128MBに設定、「3Dアクセラレーションを有効化」にチェックを入れる。 2. Guest Additionのインストールにはカーネルモジュールのビルドが必要なので、カーネルヘッダーや開発ツールをインストールしておく。 # yum install kernel-devel kernel-headers dkms gcc-c++ 3. VirtualBoxの「デバイス」−「Guest Additionsのインストール」を選択してGuest Additionsをマウントする。 4. 下記を実行 # cd /media/VBOXADDITIONS_4.1.6_74713/ # ./VBoxLinuxAdditions.sh 5. SELinuxの設定変更 　インストールされるVirtualBox Guest Addition関連ファイルのSELinuxラベルが不適切なので、変更する。 # cd /opt/VBoxGuestAdditions-4.1.6/lib/ # [...]]]></description>
			<content:encoded><![CDATA[<p>　VirtualBoxでFedora 16のデスクトップを使う場合、デフォルトだとGNOME Shellが利用できない。GNOME Shellの利用にはハードウェアアクセラレーションが必要なためだ。VirtualBoxの場合、Guest Additionをインストールすればハードウェアアクセラレーションが利用可能になり、GNOME Shellが利用可能になる。</p>
<h4>VirtualBox Guest Additionのインストール</h4>
<p>　基本的な設定は<a href="http://www.sysprobs.com/install-fedora-16-virtualbox-guest-additions-get-working-gnome-shell-inside-virtual-machine">Install Fedora 16 VirtualBox Guest Additions and Get Working Gnome Shell Inside Virtual Machine</a>という記事にある通り。</p>
<h5>1. 仮想マシンの設定の「ディスプレイ」項目で、ビデオメモリを128MBに設定、「3Dアクセラレーションを有効化」にチェックを入れる。</h5>
<div id="attachment_1509" class="wp-caption aligncenter" style="width: 410px"><a href="/img/blog/2011/12/vm_config1.png"><img src="/img/blog/2011/12/vm_config1-400x215.png" alt="VirtualBoxの設定" title="VirtualBoxの設定" width="400" height="215" class="size-medium wp-image-1509" /></a><p class="wp-caption-text">VirtualBoxの設定</p></div>
<h5>2. Guest Additionのインストールにはカーネルモジュールのビルドが必要なので、カーネルヘッダーや開発ツールをインストールしておく。</h5>
<pre>
# yum install kernel-devel kernel-headers dkms gcc-c++
</pre>
<h5>3. VirtualBoxの「デバイス」−「Guest Additionsのインストール」を選択してGuest Additionsをマウントする。</h5>
<h5>4. 下記を実行</h5>
<pre>
# cd /media/VBOXADDITIONS_4.1.6_74713/
# ./VBoxLinuxAdditions.sh
</pre>
<h5>5. SELinuxの設定変更</h5>
<p>　インストールされるVirtualBox Guest Addition関連ファイルのSELinuxラベルが不適切なので、変更する。</p>
<pre>
# cd /opt/VBoxGuestAdditions-4.1.6/lib/
# /sbin/restorecon -v *.so
</pre>
<p>　実行後、ls -Zでラベルが「textrel_shlib_t」になっていることを確認する</p>
<pre>
$ ls -Z
drwxr-xr-x. root root unconfined_u:object_r:usr_t:s0   VBoxGuestAdditions
-rwxr-xr-x. root root unconfined_u:object_r:textrel_shlib_t:s0 VBoxOGLarrayspu.so
-rwxr-xr-x. root root unconfined_u:object_r:textrel_shlib_t:s0 VBoxOGLcrutil.so
-rwxr-xr-x. root root unconfined_u:object_r:textrel_shlib_t:s0 VBoxOGLerrorspu.so
-rwxr-xr-x. root root unconfined_u:object_r:textrel_shlib_t:s0 VBoxOGLfeedbackspu.so
-rwxr-xr-x. root root unconfined_u:object_r:textrel_shlib_t:s0 VBoxOGLpackspu.so
-rwxr-xr-x. root root unconfined_u:object_r:textrel_shlib_t:s0 VBoxOGLpassthroughspu.so
-rwxr-xr-x. root root unconfined_u:object_r:textrel_shlib_t:s0 VBoxOGL.so
</pre>
<h5>6. ログアウトして再ログインする</h5>
<p>　以上で作業完了。</p>
<div id="attachment_1507" class="wp-caption aligncenter" style="width: 410px"><a href="/img/blog/2011/12/vm_fedora1.png"><img src="/img/blog/2011/12/vm_fedora1-400x319.png" alt="VirtualBox上のFedoraでGNOME Shellが動作している" title="VirtualBox上のFedoraでGNOME Shellが動作している" width="400" height="319" class="size-medium wp-image-1507" /></a><p class="wp-caption-text">VirtualBox上のFedoraでGNOME Shellが動作している</p></div>
]]></content:encoded>
			<wfw:commentRss>http://hylom.net/2011/12/06/fedora16_on_virtualbox_with_gnome_shell/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>CentOS 5.6上でXenを使って仮想環境を作るための基本設定</title>
		<link>http://hylom.net/2011/05/26/centos-5-xen-install/</link>
		<comments>http://hylom.net/2011/05/26/centos-5-xen-install/#comments</comments>
		<pubDate>Thu, 26 May 2011 10:43:11 +0000</pubDate>
		<dc:creator>hylom</dc:creator>
				<category><![CDATA[Docs]]></category>
		<category><![CDATA[centos]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[virtualization]]></category>
		<category><![CDATA[xen]]></category>

		<guid isPermaLink="false">http://hylom.net/?p=1390</guid>
		<description><![CDATA[　先日MSDNを契約、テスト用に自由にOSを使えるようになったので、テスト用のCore i7マシンにXenを導入して仮想マシン上でテスト環境を作ろう、という話。いままではテスト環境が必要になったら実環境上にインストール→不要になったら削除、を繰り返していたんだけど、仮想環境を使えばスナップショットも取れるし便利だろう、ということで。 　ベースの環境はCentOS 5.6。CentOS 5.6上にXen環境を構築する解説はネット上にたくさんあるわけですが、その多くがGUIを使ったセットアップ方法を解説していて、コンソールベースのCentOS環境でインストールする方法の解説は少なかったので以下に手順をメモしておきます。 　まず、仮想化関連パッケージを導入。 # yum groupinstall Virtualization # yum install kernel-xen 　Xenを動かすには専用カーネルが必要なので、/boot/grub/menu.lstを確認してXen対応カーネル（2.6.xx-xxx.x.x.el5xen）でブートされるように設定してリブート。また、xendおよびxendomainsサービスを起動するように設定しておく。 # /sbin/chkconfig xend on # /sbin/chkconfig xendomains on # /sbin/service xend start # /sbin/sercice xendomains start 　仮想マシンの作成とOSインストールは「virt-install」コマンドで行える。Fedora 15をインストールするなら下記のような感じに。 # /usr/sbin/virt-install --nographics --prompt Would you like a fully virtualized guest (yes or no)? This will allow you to run unmodified operating [...]]]></description>
			<content:encoded><![CDATA[<p>　先日MSDNを契約、テスト用に自由にOSを使えるようになったので、テスト用のCore i7マシンにXenを導入して仮想マシン上でテスト環境を作ろう、という話。いままではテスト環境が必要になったら実環境上にインストール→不要になったら削除、を繰り返していたんだけど、仮想環境を使えばスナップショットも取れるし便利だろう、ということで。</p>
<p>　ベースの環境はCentOS 5.6。CentOS 5.6上にXen環境を構築する解説はネット上にたくさんあるわけですが、その多くがGUIを使ったセットアップ方法を解説していて、コンソールベースのCentOS環境でインストールする方法の解説は少なかったので以下に手順をメモしておきます。</p>
<p>　まず、仮想化関連パッケージを導入。</p>
<pre>
# yum groupinstall Virtualization
# yum install kernel-xen
</pre>
<p>　Xenを動かすには専用カーネルが必要なので、/boot/grub/menu.lstを確認してXen対応カーネル（2.6.xx-xxx.x.x.el5xen）でブートされるように設定してリブート。また、xendおよびxendomainsサービスを起動するように設定しておく。</p>
<pre>
# /sbin/chkconfig xend on
# /sbin/chkconfig xendomains on
# /sbin/service xend start
# /sbin/sercice xendomains start
</pre>
<p>　仮想マシンの作成とOSインストールは「virt-install」コマンドで行える。Fedora 15をインストールするなら下記のような感じに。</p>
<pre>
# /usr/sbin/virt-install --nographics --prompt
Would you like a fully virtualized guest (yes or no)? This will allow you to run unmodified operating systems. no
 What is the name of your virtual machine? fedora
 How much RAM should be allocated (in megabytes)? 1024
 What would you like to use as the disk (file path)? /var/lib/xen/images/fedora.img
 How large would you like the disk (/var/lib/xen/images/fedora.img) to be (in gigabytes)? 10
 What is the install URL? http://ftp-srv2.kddilabs.jp/Linux/distributions/fedora/releases/15/Fedora/i386/os/
　：
　：
インストールを開始しています...
ファイル .treeinfo を読出中...                             |  906 B     00:00
ファイル vmlinuz-PAE を読出中...                           | 3.7 MB     00:00     TA
ファイル initrd-PAE.img を読出中...                        |  94 MB     00:40     TA
ストレージファイルを作成中...                         |  10 GB     00:00
ドメインを作成中...                                        |    0 B     00:04
Connected to domain fedora
エスケープ文字は  ^] です
[    0.000000] Reserving virtual address space above 0xf5800000
[    0.000000] Initializing cgroup subsys cpuset
　：
　：
</pre>
<p>　コンソール上で仮想環境上のコンソールが表示され、インストールを行える。</p>
<div id="attachment_1392" class="wp-caption aligncenter" style="width: 410px"><a href="/img/blog/2011/05/xen_f15_inst.png"><img src="/img/blog/2011/05/xen_f15_inst-400x278.png" alt="コンソール上でFedoraのインストーラを操作する" title="コンソール上でFedoraのインストーラを操作する" width="400" height="278" class="size-medium wp-image-1392" /></a><p class="wp-caption-text">コンソール上でFedoraのインストーラを操作する</p></div>
<p>　インストール完了語は、Ctrl-]でコンソールを抜けられる。稼働中の仮想マシンは「xm list」コマンドで確認可能。</p>
<pre>
# /usr/sbin/xm list
Name                                      ID Mem(MiB) VCPUs State   Time(s)
Domain-0                                   0     2012     8 r-----    780.4
fedora                                     2     1024     1 -b----      9.9
</pre>
<p>　仮想マシンのコンソールに接続するには、「xm console ＜仮想マシン名＞」を実行すればよい。コンソール接続の終了は先と同様Crtl-]。</p>
<pre>
# /usr/sbin/xm console fedora
</pre>
]]></content:encoded>
			<wfw:commentRss>http://hylom.net/2011/05/26/centos-5-xen-install/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>nuttcpでネットワークスループットを測る</title>
		<link>http://hylom.net/2011/05/18/nuttcp_thruput_benchmarking/</link>
		<comments>http://hylom.net/2011/05/18/nuttcp_thruput_benchmarking/#comments</comments>
		<pubDate>Wed, 18 May 2011 09:40:48 +0000</pubDate>
		<dc:creator>hylom</dc:creator>
				<category><![CDATA[Docs]]></category>

		<guid isPermaLink="false">http://hylom.net/?p=1386</guid>
		<description><![CDATA[　ネットワークのスループットを測るツールには色々ある。SourceForge.JP Magazineの翻訳記事（ネットワークのベンチマーク・ツールを試す &#8211; nepim、LMbench、nuttcp）でいくつか紹介されているのだが、その中でも自分が比較的よく使っているのがnuttcpだ。ということで簡易的な使い方メモ。 インストール 　ソースコードからビルドしても良いが、nuttcpのダウンロードサイトにはRPMパッケージもあるので、こちらを利用するのが楽である。 ベンチマークの実行 　nuttcpはクライアント/サーバー型のベンチマークツールである。つまり、帯域測定をしたいネットワークの片側でサーバーを動かし、もう片側でクライアントを実行してベンチマークを行う。 　サーバー側では「-S」オプション付きでnuttcpを実行しておく。 # nuttcp -S 　クライアント側では、サーバーのIPアドレス付きでnuttcpを実行する。上り速度を測定するには「-B」を、下り速度を測定するには「-D」オプションを使う。「-i＜数字＞」オプションを付けると、数字で指定した間隔で途中経過を表示する。「-v」や「-vv」で経過や結果の詳細表示。 　次の例はクライアントからサーバーへの上り速度を測定する場合。 $ nuttcp -B -i1 183.181.28.64 7.1875 MB / 1.00 sec = 60.2683 Mbps 0 retrans 7.1875 MB / 1.00 sec = 60.2960 Mbps 0 retrans 7.6875 MB / 1.00 sec = 64.4898 Mbps 0 retrans 7.7500 MB / 1.00 sec = [...]]]></description>
			<content:encoded><![CDATA[<p>　ネットワークのスループットを測るツールには色々ある。SourceForge.JP Magazineの翻訳記事（<a href="http://sourceforge.jp/magazine/08/08/22/0159234">ネットワークのベンチマーク・ツールを試す &#8211; nepim、LMbench、nuttcp</a>）でいくつか紹介されているのだが、その中でも自分が比較的よく使っているのが<a href="http://www.wcisd.hpc.mil/nuttcp/Nuttcp-HOWTO.html">nuttcp</a>だ。ということで簡易的な使い方メモ。</p>
<h4>インストール</h4>
<p>　ソースコードからビルドしても良いが、<a href="http://www.lcp.nrl.navy.mil/nuttcp/">nuttcpのダウンロードサイト</a>にはRPMパッケージもあるので、こちらを利用するのが楽である。</p>
<h4>ベンチマークの実行</h4>
<p>　nuttcpはクライアント/サーバー型のベンチマークツールである。つまり、帯域測定をしたいネットワークの片側でサーバーを動かし、もう片側でクライアントを実行してベンチマークを行う。</p>
<p>　サーバー側では「-S」オプション付きでnuttcpを実行しておく。</p>
<p># nuttcp -S</p>
<p>　クライアント側では、サーバーのIPアドレス付きでnuttcpを実行する。上り速度を測定するには「-B」を、下り速度を測定するには「-D」オプションを使う。「-i＜数字＞」オプションを付けると、数字で指定した間隔で途中経過を表示する。「-v」や「-vv」で経過や結果の詳細表示。</p>
<p>　次の例はクライアントからサーバーへの上り速度を測定する場合。</p>
<pre>
$ nuttcp -B -i1 183.181.28.64
    7.1875 MB /   1.00 sec =   60.2683 Mbps     0 retrans
    7.1875 MB /   1.00 sec =   60.2960 Mbps     0 retrans
    7.6875 MB /   1.00 sec =   64.4898 Mbps     0 retrans
    7.7500 MB /   1.00 sec =   65.0148 Mbps     0 retrans
    8.0000 MB /   1.00 sec =   67.1111 Mbps     0 retrans
    8.1250 MB /   1.00 sec =   68.0925 Mbps     0 retrans
    7.8750 MB /   1.00 sec =   66.0627 Mbps     0 retrans
    7.6875 MB /   1.00 sec =   64.4892 Mbps     0 retrans
    7.5000 MB /   1.00 sec =   62.9117 Mbps     0 retrans
    8.1875 MB /   1.00 sec =   68.6899 Mbps     0 retrans

   77.6250 MB /  10.07 sec =   64.6675 Mbps 0 %TX 6 %RX 0 retrans 9.35 msRTT
</pre>
<p>　下り速度の測定は次のような感じ。</p>
<pre>
$ nuttcp -D -i1 183.181.28.64
    6.1250 MB /   1.00 sec =   51.3391 Mbps     0 retrans
    7.5625 MB /   1.00 sec =   63.4470 Mbps     0 retrans
    7.3750 MB /   1.00 sec =   61.8740 Mbps     0 retrans
    7.3125 MB /   1.00 sec =   61.3443 Mbps     0 retrans
    6.9375 MB /   1.00 sec =   58.1962 Mbps     0 retrans
    7.1875 MB /   1.00 sec =   60.2891 Mbps     0 retrans
    7.3750 MB /   1.00 sec =   61.8149 Mbps     0 retrans
    7.7500 MB /   1.00 sec =   65.0144 Mbps     0 retrans
    7.5625 MB /   1.00 sec =   63.4413 Mbps     0 retrans
    7.5625 MB /   1.00 sec =   63.4363 Mbps     0 retrans

   73.2500 MB /  10.07 sec =   60.9941 Mbps 0 %TX 5 %RX 0 retrans 9.11 msRTT
</pre>
]]></content:encoded>
			<wfw:commentRss>http://hylom.net/2011/05/18/nuttcp_thruput_benchmarking/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Apacheチューニング：プロセス設定を変更</title>
		<link>http://hylom.net/2011/03/04/apache-prefork-config/</link>
		<comments>http://hylom.net/2011/03/04/apache-prefork-config/#comments</comments>
		<pubDate>Fri, 04 Mar 2011 11:00:48 +0000</pubDate>
		<dc:creator>hylom</dc:creator>
				<category><![CDATA[Docs]]></category>
		<category><![CDATA[apache]]></category>
		<category><![CDATA[httpd]]></category>
		<category><![CDATA[tuning]]></category>

		<guid isPermaLink="false">http://hylom.net/?p=1383</guid>
		<description><![CDATA[　hylom.netがだいぶ遅くなっていたのでちょっとチューニングした。原因としてはメモリ不足、DBが遅い、I/Oが遅い、ネットワークが遅いなどがあるが、さくらVPSのコントロールパネルを見た限りではCPUやネットワーク、I/Oには余裕がある感じだったので、メモリ不足ではないかと検討をつける。ということで、各プロセスのメモリ使用状況をチェック。 # ps aux --sort -rss&#124; lv USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND apache 28879 0.1 6.6 269404 33728 ? S Mar01 9:40 /usr/sbin/httpd apache 28966 0.1 6.5 269072 33484 ? S Mar01 9:54 /usr/sbin/httpd apache 30532 0.1 6.5 272696 33412 ? S Mar02 4:48 /usr/sbin/httpd apache 30542 0.1 6.2 [...]]]></description>
			<content:encoded><![CDATA[<p>　hylom.netがだいぶ遅くなっていたのでちょっとチューニングした。原因としてはメモリ不足、DBが遅い、I/Oが遅い、ネットワークが遅いなどがあるが、さくらVPSのコントロールパネルを見た限りではCPUやネットワーク、I/Oには余裕がある感じだったので、メモリ不足ではないかと検討をつける。ということで、各プロセスのメモリ使用状況をチェック。</p>
<pre>
# ps aux --sort -rss| lv

USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
apache   28879  0.1  6.6 269404 33728 ?        S    Mar01   9:40 /usr/sbin/httpd
apache   28966  0.1  6.5 269072 33484 ?        S    Mar01   9:54 /usr/sbin/httpd
apache   30532  0.1  6.5 272696 33412 ?        S    Mar02   4:48 /usr/sbin/httpd
apache   30542  0.1  6.2 274072 31680 ?        S    Mar02   4:41 /usr/sbin/httpd
apache   29002  0.1  6.0 274684 30804 ?        S    Mar01   9:31 /usr/sbin/httpd
apache   28868  0.2  5.9 273812 30556 ?        S    Mar01  10:30 /usr/sbin/httpd
apache   28750  0.2  5.8 274168 29964 ?        S    Mar01  10:56 /usr/sbin/httpd
apache   30543  0.1  5.7 274040 29212 ?        S    Mar02   4:23 /usr/sbin/httpd
apache   30529  0.1  5.2 273992 26952 ?        S    Mar02   4:33 /usr/sbin/httpd
apache   28580  0.2  4.8 274152 24852 ?        S    Mar01  10:36 /usr/sbin/httpd
apache   27032  0.1  4.3 273148 22224 ?        S    Mar01   6:06 /usr/sbin/httpd
apache    8773  0.1  3.9 268900 19944 ?        S    Mar03   2:44 /usr/sbin/httpd
apache   27108  0.1  3.7 274016 19224 ?        S    Mar01   6:03 /usr/sbin/httpd
apache   30530  0.1  3.3 273152 17020 ?        S    Mar02   4:45 /usr/sbin/httpd
apache   18789  0.1  2.7 269340 14000 ?        S    Mar01   5:38 /usr/sbin/httpd
apache   28998  0.1  2.6 274204 13616 ?        S    Mar01   9:11 /usr/sbin/httpd
apache    8767  0.1  2.1 273544 10756 ?        S    Mar03   2:53 /usr/sbin/httpd
apache   28976  0.1  1.9 273572  9740 ?        S    Mar01   9:32 /usr/sbin/httpd
apache   28877  0.2  1.4 274668  7308 ?        S    Mar01  10:14 /usr/sbin/httpd
apache   30506  0.1  1.1 273528  5688 ?        S    Mar02   4:12 /usr/sbin/httpd
　：
　：
</pre>
<p>　見て分かるように、httpdのプロセスが多数走っており、メモリを食いつぶしていることが分かる。そこで、httpdで使用するプロセス数を調整することで対処を図る。</p>
<pre>
&lt;IfModule prefork.c&gt;
StartServers       8
MinSpareServers    5
# MaxSpareServers   20
MaxSpareServers   16
#ServerLimit      256
ServerLimit      32
#MaxClients       256
MaxClients       32
MaxRequestsPerChild  4000
&lt;/IfModule&gt;
</pre>
<p>　MaxSpareServersとMaxClientsを減らしてとりあえず様子見。</p>
<p>　なお、@ITの「<a href="http://www.atmarkit.co.jp/flinux/rensai/apache2_03/apache03a.html">httpd.confによるWebサーバの最適化</a>」という記事などを参考にした。</p>
]]></content:encoded>
			<wfw:commentRss>http://hylom.net/2011/03/04/apache-prefork-config/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>EmacsでC#（csharp-mode）を使う</title>
		<link>http://hylom.net/2011/03/04/emacs-sharp-mode/</link>
		<comments>http://hylom.net/2011/03/04/emacs-sharp-mode/#comments</comments>
		<pubDate>Fri, 04 Mar 2011 09:31:48 +0000</pubDate>
		<dc:creator>hylom</dc:creator>
				<category><![CDATA[Docs]]></category>
		<category><![CDATA[csharp]]></category>
		<category><![CDATA[emacs]]></category>

		<guid isPermaLink="false">http://hylom.net/?p=1381</guid>
		<description><![CDATA[　EmacsでC#コードをいじりたかったのでcsharp-modeを導入した。 　csharp-modeはGoogle Codeのcsharpmodeプロジェクトから入手可能。 　とりあえず現時点での最新版「csharp-mode-0.7.6.el」をダウンロードし、csharp-mode.elにリネームして~/.emacs.d/lisp/以下にコピーし、.emacs.elに下記を追加した。 ;; C# mode (require 'csharp-mode) (setq auto-mode-alist (append '(("\\.cs$" . csharp-mode)) auto-mode-alist)) 　ちなみに私の場合、.emacs.el中で下記のように~/.emacs.d/lispをロードパスに追加しているが、そうでない場合はsite-lispディレクトリなどロードパスに含まれている場所にcsharp-mode.elを置く必要がある。 ;; add ~/.emacs.d/lisp to load-path (add-to-list 'load-path "~/.emacs.d/lisp") 　最後にEmacs上でM-x byte-compile-fileを実行してcsharp-mode.elを指定してバイトコンパイルして完了。これでC#のコードが色付き（ハイライト）で表示されるようになる。]]></description>
			<content:encoded><![CDATA[<p>　EmacsでC#コードをいじりたかったのでcsharp-modeを導入した。</p>
<p>　csharp-modeは<a href="http://code.google.com/p/csharpmode/">Google Codeのcsharpmodeプロジェクト</a>から入手可能。</p>
<p>　とりあえず現時点での最新版「csharp-mode-0.7.6.el」をダウンロードし、csharp-mode.elにリネームして~/.emacs.d/lisp/以下にコピーし、.emacs.elに下記を追加した。</p>
<pre>
;; C# mode
(require 'csharp-mode)
(setq auto-mode-alist
      (append '(("\\.cs$" . csharp-mode)) auto-mode-alist))
</pre>
<p>　ちなみに私の場合、.emacs.el中で下記のように~/.emacs.d/lispをロードパスに追加しているが、そうでない場合はsite-lispディレクトリなどロードパスに含まれている場所にcsharp-mode.elを置く必要がある。</p>
<pre>
;; add ~/.emacs.d/lisp to load-path
(add-to-list 'load-path "~/.emacs.d/lisp")
</pre>
<p>　最後にEmacs上でM-x byte-compile-fileを実行してcsharp-mode.elを指定してバイトコンパイルして完了。これでC#のコードが色付き（ハイライト）で表示されるようになる。</p>
]]></content:encoded>
			<wfw:commentRss>http://hylom.net/2011/03/04/emacs-sharp-mode/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Maildirでの日本語フォルダの扱い（procmailで日本語フォルダへ振り分ける）</title>
		<link>http://hylom.net/2011/03/03/maildir-with-japanese-folder-name/</link>
		<comments>http://hylom.net/2011/03/03/maildir-with-japanese-folder-name/#comments</comments>
		<pubDate>Thu, 03 Mar 2011 09:43:01 +0000</pubDate>
		<dc:creator>hylom</dc:creator>
				<category><![CDATA[Docs]]></category>
		<category><![CDATA[japanese]]></category>
		<category><![CDATA[mail]]></category>
		<category><![CDATA[maildir]]></category>
		<category><![CDATA[procmail]]></category>

		<guid isPermaLink="false">http://hylom.net/?p=1377</guid>
		<description><![CDATA[　最近procmailでメールの振り分けをしているのだが、日本語のフォルダへの振り分けってどうするの？　と悩んだので調べた。 　命名規則自体はRFC2060に記載されているとおり、UTF-7の修正版が用いられている（UTF-7についてはIT用語辞典を参照のこと）。 　Maildir以下のサブフォルダはこの規則に従って作成されるので、目的となる日本語フォルダ名を修正版UTF-7（IMAP-UTF7などとも呼ばれる）に変換し、それを振り分け先として指定すれば良い。 　IMAP-UTF7への変換は文字コード変換ツールがあるので、こちらを使えばお手軽。たとえば「登録関係」というフォルダなら、「.&#038;dnuTMpWiT8I-」というフォルダ名となる。]]></description>
			<content:encoded><![CDATA[<p>　最近procmailでメールの振り分けをしているのだが、日本語のフォルダへの振り分けってどうするの？　と悩んだので調べた。</p>
<p>　命名規則自体は<a href="http://www.lins.jp/~obata/imap/rfc/rfc2060ja.html#s5.1">RFC2060</a>に記載されているとおり、UTF-7の修正版が用いられている（<a href="http://www.sophia-it.com/content/UTF-7">UTF-7についてはIT用語辞典を参照のこと</a>）。</p>
<p>　Maildir以下のサブフォルダはこの規則に従って作成されるので、目的となる日本語フォルダ名を修正版UTF-7（IMAP-UTF7などとも呼ばれる）に変換し、それを振り分け先として指定すれば良い。</p>
<p>　IMAP-UTF7への変換は<a href="http://e-zackie.com/tool/code_convert.php">文字コード変換ツール</a>があるので、こちらを使えばお手軽。たとえば「登録関係」というフォルダなら、「.&#038;dnuTMpWiT8I-」というフォルダ名となる。</p>
]]></content:encoded>
			<wfw:commentRss>http://hylom.net/2011/03/03/maildir-with-japanese-folder-name/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Gitでブランチを間違えて作業した上にpushまでしちゃった場合の対処方法</title>
		<link>http://hylom.net/2011/03/01/how-to-reset-remote-git-branch/</link>
		<comments>http://hylom.net/2011/03/01/how-to-reset-remote-git-branch/#comments</comments>
		<pubDate>Tue, 01 Mar 2011 11:01:39 +0000</pubDate>
		<dc:creator>hylom</dc:creator>
				<category><![CDATA[Docs]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[git]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://hylom.net/?p=1374</guid>
		<description><![CDATA[　時間ができたのでHandBrake 0.9.5の日本語化に着手しているのだが、うっかり0.9.4のブランチで作業してしまったあげく、SourceForge.JP上のリポジトリにpushしてしまい途方に暮れる。 　まあこういうミスをやる人は少ないだろうが、何かのヒントになるかもしれないので対処法をメモしておく。まず、「git log」コマンドで巻き戻したいcommitのハッシュを調べる。 $ git log 　： 　： commit 1485a5a2bbbb43eedbe131c919b7d604bcbd506d Author: unknown Date: Tue Jan 5 19:19:44 2010 +0900 update Installer, changelog, readme 　今回は、この「1485a5a2bbbb43eedbe131c919b7d604bcbd506d」というcommitまで巻き戻すことにする。作業中のブランチが操作したいものであることを確認したうえで、「git reset ＜対象のハッシュ＞」を実行する。 $ git branch -a jp-0.9.3 * jp-0.9.4 master original remotes/origin/HEAD -> origin/master remotes/origin/jp-0.9.3 remotes/origin/jp-0.9.4 remotes/origin/master remotes/origin/original $ git reset 1485a5 Unstaged changes after reset: M Jamrules M Makefile [...]]]></description>
			<content:encoded><![CDATA[<p>　時間ができたのでHandBrake 0.9.5の日本語化に着手しているのだが、うっかり0.9.4のブランチで作業してしまったあげく、SourceForge.JP上のリポジトリにpushしてしまい途方に暮れる。</p>
<p>　まあこういうミスをやる人は少ないだろうが、何かのヒントになるかもしれないので対処法をメモしておく。まず、「git log」コマンドで巻き戻したいcommitのハッシュを調べる。</p>
<pre>
$ git log
　：
　：
commit 1485a5a2bbbb43eedbe131c919b7d604bcbd506d
Author: unknown <hirom@.(none)>
Date:   Tue Jan 5 19:19:44 2010 +0900

    update Installer, changelog, readme
</pre>
<p>　今回は、この「1485a5a2bbbb43eedbe131c919b7d604bcbd506d」というcommitまで巻き戻すことにする。作業中のブランチが操作したいものであることを確認したうえで、「git reset ＜対象のハッシュ＞」を実行する。</p>
<pre>
$ git branch -a
  jp-0.9.3
* jp-0.9.4
  master
  original
  remotes/origin/HEAD -> origin/master
  remotes/origin/jp-0.9.3
  remotes/origin/jp-0.9.4
  remotes/origin/master
  remotes/origin/original

$ git reset 1485a5
Unstaged changes after reset:
M       Jamrules
M       Makefile
M       win/C#/Installer/Installer.nsi
M       win/C#/InstallerJp/doc/AUTHORS
M       win/C#/InstallerJp/doc/CREDITS
M       win/C#/InstallerJp/doc/NEWS
M       win/C#/Properties/AssemblyInfo.cs
M       win/C#/frmAbout.Designer.cs
</pre>
<p>　「git log」で巻戻ったことを確認する。</p>
<pre>
$ git log
commit 1485a5a2bbbb43eedbe131c919b7d604bcbd506d
Author: unknown <hirom@.(none)>
Date:   Tue Jan 5 19:19:44 2010 +0900

    update Installer, changelog, readme
</pre>
<p>　バージョン管理されているファイル自体は巻き戻されていないので、「-f」オプション付きでチェックアウトしてファイルも巻き戻す。</p>
<pre>
$ git checkout -f
</pre>
<p>　ここまでの作業でローカルブランチの巻き戻しは完了。続いてリモートブランチの巻き戻しを行う。ただし、当然ながらそのままpushすることはできない。</p>
<pre>
$ git push
Enter passphrase for key '/d/Users/hirom/.ssh/id_dsa':
To hylom@git.sourceforge.jp:/gitroot/handbrake-jp/handbrake-jp.git
 ! [rejected]        jp-0.9.4 -> jp-0.9.4 (non-fast-forward)
error: failed to push some refs to 'hylom@git.sourceforge.jp:/gitroot/handbrake-
jp/handbrake-jp.git'
To prevent you from losing history, non-fast-forward updates were rejected
Merge the remote changes (e.g. 'git pull') before pushing again.  See the
'Note about fast-forwards' section of 'git push --help' for details.
</pre>
<p>　そこで、いったんリモートブランチを削除した上で再度pushする。まずは削除。リモートブランチの削除は、「git push ＜リモートリポジトリ＞ :＜対象リモートブランチ＞」で行える。</p>
<pre>
$ git push origin :jp-0.9.4
Enter passphrase for key '/d/Users/hirom/.ssh/id_dsa':
To hylom@git.sourceforge.jp:/gitroot/handbrake-jp/handbrake-jp.git
 - [deleted]         jp-0.9.4
</pre>
<p>　あとは再度ローカルリポジトリをpushするだけ。</p>
<pre>
$ git push origin jp-0.9.4:jp-0.9.4
Enter passphrase for key '/d/Users/hirom/.ssh/id_dsa':
Counting objects: 233, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (146/146), done.
Writing objects: 100% (165/165), 30.48 KiB, done.
Total 165 (delta 119), reused 0 (delta 0)
To hylom@git.sourceforge.jp:/gitroot/handbrake-jp/handbrake-jp.git
 * [new branch]      jp-0.9.4 -> jp-0.9.4
</pre>
<p>　お粗末様でした。</p>
]]></content:encoded>
			<wfw:commentRss>http://hylom.net/2011/03/01/how-to-reset-remote-git-branch/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Pythonコード中のSQL文インデントを考える</title>
		<link>http://hylom.net/2011/02/21/python-sql-indenting/</link>
		<comments>http://hylom.net/2011/02/21/python-sql-indenting/#comments</comments>
		<pubDate>Mon, 21 Feb 2011 09:03:46 +0000</pubDate>
		<dc:creator>hylom</dc:creator>
				<category><![CDATA[Docs]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[sql]]></category>

		<guid isPermaLink="false">http://hylom.net/?p=1370</guid>
		<description><![CDATA[　Pythonコード内にSQL文を書くときどうすれば良いのか、いまいち答えが探せなかったのでググって見た話。 　ちなみに、今までは下記のような感じのコードを書いていた訳だが、これ見るからに分かりにくい。 # コード例その1 cur.execute(""" create table count ( sid text, count int);""") # コード例その2 try: cur.execute("""insert into count ( sid, count ) values ( :sid, :count );""", d) except sqlite3.IntegrityError: cur.execute("""update count set sid = :sid, count = : count where sid = :sid;""", d) # コード例その3 cmd = """select sid, title, date from [...]]]></description>
			<content:encoded><![CDATA[<p>　Pythonコード内にSQL文を書くときどうすれば良いのか、いまいち答えが探せなかったのでググって見た話。</p>
<p>　ちなみに、今までは下記のような感じのコードを書いていた訳だが、これ見るからに分かりにくい。</p>
<pre>
# コード例その1
        cur.execute("""
          create table count (
            sid text,
            count int);""")

# コード例その2
        try:
            cur.execute("""insert into count ( sid, count )
                           values ( :sid, :count );""", d)
        except sqlite3.IntegrityError:
            cur.execute("""update count set sid = :sid, count = : count
                           where sid = :sid;""", d)
# コード例その3
        cmd = """select sid, title, date from stories where date >= ? and date < ? and sid in (
                 select sid from topics where topic == ? and sid in (
                 select sid from topics where topic == ? )) order by date
        """
        cur.execute(cmd, (begin_t, end_t, t1, t2))
</pre>
<p>　「SQL インデント」でググると、そういう話のネタが一杯出てくるでてくる。その中から拾ってみたのが下記。</p>
<ul>
<li><a href="http://www.geocities.jp/mickindex/database/db_manner.html">SQLプログラミング作法</a></li>
<li><a href="http://d.hatena.ne.jp/r_ikeda/20090630/indent">SQL のコーディングスタイル（インデント） - 集中力なら売り切れたよ</a></li>
<li><a href="http://www.popxpop.com/archives/2007/05/sqlsql_in_form.html">SQL文をきれいにフォーマットしてくれる『SQL in Form』 - POP*POP ～ 世界のニュースをクオリティ重視で</a></li>
<li><a href="http://shinshu.fm/MHz/88.44/archives/0000049011.html">VB.NETで作る！ | SQL文の字下げ目安</a></li>
<li><a href="http://kamoland.com/wiki/wiki.cgi?SQL%A4%CE%C0%B0%B7%C1%A5%C4%A1%BC%A5%EB%A1%C1%C0%B0%B7%C1%B7%EB%B2%CC%A4%CE%CE%E3">SQLの整形ツール～整形結果の例 - プログラマー'sペイジ</a></li>
</ul>
<p>　異端だけど、<a href="http://d.zeromemory.info/2007/01/19/coding-rule-sql.html">俺的コーディングルール SQL編 – suVeneのアレ</a>というのも参考になった。</p>
<p>　で、この辺をまとめたところ、だいたい以下のようなルールに落ち着いた。</p>
<ul>
<li>SQLキーワードは大文字で</li>
<li>括弧挟まれた部分はインデントレベルを+1する</li>
<li>カンマ、ANDの直後で改行</li>
<li>カンマやANDでつなげられている部分はなるべくキーワード部分でそろえる</li>
</ul>
<p>　このルールで書いたコードは下記のような感じ。</p>
<pre>
# コード例その1
        cur.execute("""
            CREATE TABLE count (
                sid text,
                count int
            );
        """)

# コード例その2
        try:
            cur.execute("""
                INSERT INTO count (
                     sid,
                     count
                )
                VALUES (
                    :sid,
                    :count
                )
            """, d)
        except sqlite3.IntegrityError:
            cur.execute("""
                UPDATE count
                SET sid = :sid,
                    count = : count
                WHERE sid = :sid
            """, d)
# コード例その3
        cmd = """
            SELECT sid,
                   title,
                   date
            FROM stories
            WHERE date >= ? AND
                  date < ? AND
                  sid IN (
                      SELECT sid
                      FROM topics
                      WHERE topic == ? AND
                      sid IN (
                          SELECT sid
                          FROM topics
                          WHERE topic == ?
                      )
                  )
            ORDER BY DATE
        """
        cur.execute(cmd, (begin_t, end_t, t1, t2))
</pre>
<p>　<br />
　本当にこれで良いのかはまだ自信がないが、おおむね間違ってはいないと思う。ていうかコード内にSQL文を直書きせずO/Rマッパー使え、という話もあるが……。</p>
]]></content:encoded>
			<wfw:commentRss>http://hylom.net/2011/02/21/python-sql-indenting/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

