secretbase.log

.NET/C#/Pythonなど

ifconfig コマンドの導入

debian GNU/Linux 7.0 への ifconfig インストール方法のメモ。

インストール方法

$ sudo aptitude install net-tools

実行方法

/sbin なので通常のユーザではPATHが通っていないことに注意。

$ /sbin/ifconfig

インストールパスの確認方法

$ dpkg -L net-tools | grep ifconfig
...
...
/sbin/ifconfig

local time から始めるプログラミング言語入門

プログラミング入門としては、Hello World を標準出力に出力することがよくあります。初めて触る言語で行うサンプルとして、Hello World より少しだけ進んで local time の現在時刻を表示するサンプルコードをいくつかつくってみました。
[2013/01/24 12:34:56] という現在時刻をメッセージやログに日付付きで記録したいときに役立つフォーマットです。

扱うプログラミング言語は、TIOBE で上位にランクされる人気の言語から C Java C++ C# Python Perl JavaScript Ruby とあと いくつかのプログラミング言語や環境で試してみます。

環境としては、主にWindows上で、いくつかMSYSで行なっています*1


C による実装

clock_gettime() を用います。time.h を include することで使用可能です。

#include <stdio.h>
#include <time.h>

#define TIME_MAXSIZE 64

void nowtime(char *str)
{
	time_t timer = time(NULL);
	strftime(str, TIME_MAXSIZE, "[%Y/%m/%d %H:%M:%S]", localtime(&timer));
}

int main()
{
	char timestr[TIME_MAXSIZE];
	
	nowtime(timestr);
	printf("%s\n", timestr);

	return 0;
}

$ gcc -Wall time.c;./a
[2013/02/05 20:38:41]





Java による実装

Date 型で取得する関数 Date()があり、SimpleDateFormatクラスのformatメソッドで指定したString型に変換可能です。

import java.util.*;
import java.text.*;

public class NowTime {
	public static void main(String[] args){
		System.out.println(getNowTime());
	}
	static String getNowTime(){
		Date now = new Date();
		SimpleDateFormat fmt = new SimpleDateFormat("[YYYY/MM/dd HH:mm:ss]");
		return fmt.format(now);
	}
}

$ javac NowTime.java ; java NowTime
[2013/02/08 19:36:16]




C++ による実装

boost を用いてみたいと思います。ですが私の頭では Date_Time の仕様がよく理解できず、stackoverflow の回答をおもいっきり参考にさせて頂きました。
http://stackoverflow.com/questions/1904317/c-boost-date-with-format-dd-mm-yyyy

#include <iostream>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/date_time/posix_time/posix_time_io.hpp>

using namespace boost::posix_time;
using namespace std;

void nowtime(void)
{
	time_facet *facet = new time_facet("[%Y/%m/%d %H:%M:%S]");
	cout.imbue(locale(cout.getloc(), facet));
	cout << second_clock::local_time() << endl;
}

#ifdef DEBUG
int main(int argc, char **argv)
{
	nowtime();
}
#endif

$ g++ -Wall -I/usr/include/boost -DDEBUG nowtime.cpp ;./a
[2013/02/16 23:16:32]





C# による実装

DateTimeオブジェクトを取得し ToString で文字列に変換します。

using System;

class Datetime
{
	public static void Main()
	{
		Console.WriteLine(DateTime.Now.ToString("[yyyy/MM/dd HH:mm:ss]"));			
	}
}

Visual Studio コマンドプロンプトを開くことで、csc.exe(コンパイラ)にパスが通ります。

> csc nowtime.cs にてコンパイルを行いましょう。
> nowtime
[2013/02/23 22:28:45]




Python による実装

標準ライブラリ datetime が使いやすいです。import しましょう。

import datetime

print datetime.datetime.today().strftime('[%Y/%m/%d %H:%M:%S]')

python nowtime.py
[2013/01/24 21:25:11]




Perl による実装

localtime 関数にて現在の時刻を示す9個の時刻情報が返却されますので、書式を整えましょう。

#!/usr/bin/perl

sub nowtime {
	my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);

	my $date = sprintf("[%04d/%02d/%02d %02d:%02d:%02d]\n", 
					   $year+1900,$mon+1,$mday,$hour,$min,$sec);
	return $date;
}

print nowtime();

$ perl nowtime.pl
[2013/02/17 00:41:47]




JavaScript による実装

Dateオブジェクトにて現在時刻が取得できます。

var today = new Date();

var fmt = "["+ today.getFullYear()+"/"+today.getMonth()+1
        +"/"+today.getDay()+" "
        +today.getHours()+":"+today.getMinutes()+":"+today.getSeconds()+"]"

WScript.echo(fmt);

Windows 上で explorer nowtime.js という形で起動します(またはnowtime.js ファイル)をダブルクリックします。



Ruby による実装

#!/usr/bin/env ruby

def nowtime
  return Time.now.strftime("[%Y-%m-%d %H:%M:%S]")
end

puts nowtime

$ ruby nowtime.rb
[2013-02-17 01:36:04]



bash による実装

date コマンド(GNU date) に頼ることにします。

echo "["`date +"%Y/%m/%d %k:%M:%S"`"]"

[2013/01/24 20:24:25]




コマンドプロンプト(Windowsバッチファイル)による実装

echo [%date% %time:~,-3%]

[2013/04/08 22:30:52]




Windows Power Shell による実装

Get-Date コマンドレットを使用します。

PS C:\> Get-Date -format ["yyyy/MM/dd hh:mm:ss"]

[2013/04/09 12:13:36]





Emacs Lisp による実装

M-x にて ielm にてインタラクティブシェルを起動します。
(スクラッチバッファにて C-j でも実行することができます)

ELISP> (format-time-string "[%Y/%m/%d %H:%M:%S]" (current-time))
"[2013/03/01 10:50:44]"

さいごに

表示された日付時刻がまちまちなのは、それぞれのプログラミング言語で実装した時刻の差です。いろいろな言語を気長に試した結果なので気にしないでくださいね。



追記。その他いくつかの言語による実装を行われた方がいましたのでリンクをご紹介します。

*1:C,C++perlbashはMSYSで行いました。

virtualenv 環境で sphinx を試してエラーが出て先生に尋ねたら解決した

Python のパッケージをインストールする場合、virtualenv を使うのが常識のよう なので、sphinxやその拡張もシステムに直接インストールせずに、virtualenv環境でやってみようと考えていました。

  • sphinxのビルド環境を virtualenv で用意する(ドキュメント毎にsphinxの拡張の導入も楽になることも期待)
  • virtualenv 自体も 隠しディレクトリとして sphinx のビルド環境に含めたい


そこで実験をするために、下記手順を行いました。
(環境は、Windows 7です)

適当なディレクトリに移動 ( %HOME%\work\temp とか ) に移動し下記コマンドを実行。

virtualenv .env
.env\Scripts\activate.bat
pip install sphinx
sphinx-quickstart
make.bat html

make html でエラーが出る

ERROR: Error in "currentmodule" directive: というエラーが出ます。
う~ん。なぜだろうと思い、 @ 先生に聞いてみました。

(.env) C:\Users\tkondou\work\temp>make.bat html
Making output directory...
Running Sphinx v1.1.3
loading pickled environment... not yet created
building [html]: targets for 4 source files that are out of date
updating environment: 4 added, 0 changed, 0 removed
reading sources... [ 25%] .env/Lib/site-packages/sphinx/ext/autosummary/templat
s/autosummary/base
reading sources... [ 50%] .env/Lib/site-packages/sphinx/ext/autosummary/templat
s/autosummary/class
reading sources... [ 75%] .env/Lib/site-packages/sphinx/ext/autosummary/templat
s/autosummary/module
reading sources... [100%] index

C:\Users\tkondou\work\temp\.env\Lib\site-packages\sphinx\ext\autosummary\templa
es\autosummary\base.rst:4: ERROR: Error in "currentmodule" directive:
maximum 1 argument(s) allowed, 3 supplied.

.. currentmodule:: {{ module }}
C:\Users\tkondou\work\temp\.env\Lib\site-packages\sphinx\ext\autosummary\templa
es\autosummary\class.rst:4: ERROR: Error in "currentmodule" directive:
maximum 1 argument(s) allowed, 3 supplied.

...

原因

.env 以下にインストールしたsphinxに含まれている rstファイルもビルド対象になってしまっていることが原因でした。

sphinx の挙動として、ソースディレクトリを分離しない場合は、カレントディレクトリがソースディレクトリに指定されます*1
その場合、 .env もソースフォルダとして指定されてしまい、.envの奥深くに存在する .rstファイルをビルドしようとして、エラーが出るとのことです。

解決策

.env をビルド対象から外します。

conf.py の exclude_patterns に .env を追加します。文字列として追加するので '' で囲んでくださいね。

# List of patterns, relative to source directory, that match files and
# directories to ignore when looking for source files.
exclude_patterns = ['_build', '.env']

今後の予定

virtualenv のフォルダはドキュメントフォルダに含めないほうがいいのかもしれない。それとも、sourceフォルダを分けたほうがいいのか、ベストプラクティスの模索は続きます。

*1:> Separate source and build directories (y/N) [n]: でそのままリターンキーを押した場合

分散バージョン管理の機能比較まとめ

DVCSで有名なものですと、 Git , Mercurial, Bazaar があります。
最近では、Veracity という分散バージョン管理も出てきました。どのように違ってどういう基準で選ぶと良いか悩みますね。これらの違いはなんでしょうか?
そこで、比較資料をまとめてみました。

バージョン管理ソフトウェアの比較資料

従来の集中型を含めたバージョン管理ソフトウェアの比較資料

Gitポケットリファレンス

Gitポケットリファレンス

入門TortoiseHg + Mercurial

入門TortoiseHg + Mercurial

入門TortoiseHg+Mercurial 書評

Mercurialコミッタのフジワラさん(id:flying-foozy)から、"入門TortoiseHg+Mercurial"を頂きましたのでこのエントリで紹介させていただきます。

入門TortoiseHg + Mercurial

入門TortoiseHg + Mercurial

  • 第1部 1人での作業
  • 第2部 チームでの作業
  • 第3部 より高度な使い方
  • 第4部 補遺
http://www.shuwasystem.co.jp/products/7980html/3710.html

どんな人を対象としているか

この本の対象読者は、(1)履歴管理が始めてな人 (2)CVS/Subversionを使用している人 (3)Mercurialを使用している人 です。

私自身は(2)CVS/Subversionを使用している人に該当していました。

TortoiseHg を使うひとつの理由

SubversionをWindowsで使っている方 (TortoiseSVNが多いでしょう)で、流行りの分散バージョン管理が気になっている方も多いと思います。
Mercurialに出会い会社に導入した大きな理由として、Windows上での快適な操作を目的としたところがあります。
分散バージョン管理の中で最も有名なGitは、コマンドライン前提の設計でエンジニア寄りであり、履歴管理は”手段”と捉えた場合に導入コストも安くないです。そういった理由でMercurial(TortoiseHg)を選ぶ人も多いです。

TortoiseHgとTortoiseSVNの違い

TortoiseHgとTortoiseSVNの違いはなんでしょうか。もちろんSVNMercurialという大きな違いはあるのですが、GUIを使用する際の心構えとしてTortoiseSVNは、エクスプローラ統合の右クリックメニューからの操作が主だと思います。
これはファイルやフォルダに対する操作を行う考え方です。一方、TortoiseHgでは、”ワークベンチ”といって、一つ(または複数)のリポジトリに対する操作を包括的に行うことができるGUIをどーんとメインに起動してそこから操作を行うことが多いです。
コミットツリーも視覚的に見れます。このあたりのメンタルモデルの違いは念頭に置くとよいと思います。*1

また、Windowsの場合、TortoiseHgのみインストールすれば、Mercurialコマンドも同梱されますので便利です(最近のTortoiseSVNではコマンドラインをインストールすることもできます)

本書の読み方

補遺にインストール手順が書いてあります。まずはTortoiseHgをインストールしましょう。最低限の初期設定をしてあとは一人から始めましょう。
本書籍では、そういった最初の導入を一人で始め、チームで行うやり方、頭から順番に読むことで、少しずつ身につけることができます。
もちろんTortoiseHgはGUIですので、豊富な画面もついていますので、理解しやすいです。

またファイルを間違って登録してしまったことを取り消したいといったやりたい状況別に読むこともできます。
"第2章 うっかりミスを取り消す"はあらかじめ読んでおくと安心ですね。

慣れてきたら、ひとりでも、デスクトップPCとノートPC間や、会社と自宅間での履歴反映をやってみると、マージの経験値があがるでしょう。

本書で、ぜひ TortoiseHgとMercurial の世界に足を踏み入れてみてください。良い意味で戻ることはできなくなるでしょうから。

関連記事

*1:SVNでいうRapidSVNに近いですね

.NET Framework のバージョンを確認する

調べたらバッチファイルで確認する方法 があるので、Windows8 で試してみました。

実行結果

f:id:cointoss1973:20130227095819p:plain

Windows 8 は、.NET Framework 4.5 が同梱されていますが、その下位バージョンもインストールされていることがわかりました。

svnversion で subversion の リビジョンが取得できる

svnversion を使えば、シンプルに revision だけ取得できます。

Windows 7 32-bit / TortoiseSVN 1.7.10 / msysgit の環境で実行しています。

$ svnversion
212

$ which svnversion
/c/Program Files/TortoiseSVN/bin/svnversion


svnversion -h で helpがみられます。

$ svnversion -h
usage: svnversion [OPTIONS] [WC_PATH [TRAIL_URL]]

  Produce a compact 'version number' for the working copy path
  WC_PATH.  TRAIL_URL is the trailing portion of the URL used to
  determine if WC_PATH itself is switched (detection of switches
  within WC_PATH does not rely on TRAIL_URL).  The version number
  is written to standard output.  For example:

    $ svnversion . /repos/svn/trunk
    4168

  The version number will be a single number if the working
  copy is single revision, unmodified, not switched and with
  an URL that matches the TRAIL_URL argument.  If the working
  copy is unusual the version number will be more complex:

   4123:4168     mixed revision working copy
   4168M         modified working copy
   4123S         switched working copy
   4123P         partial working copy, from a sparse checkout
   4123:4168MS   mixed revision, modified, switched working copy

  If WC_PATH is an unversioned path, the program will output
  'Unversioned directory' or 'Unversioned file'.  If WC_PATH is
  an added or copied or moved path, the program will output
  'Uncommitted local addition, copy or move'.

  If invoked without arguments WC_PATH will be the current directory.

Valid options:
  -n [--no-newline]        : do not output the trailing newline
  -c [--committed]         : last changed rather than current revisions
  -h [--help]              : display this help
  --version                : show program version information
  -q [--quiet]             : no progress (only errors) to stderr

参考

Subversion実践入門:達人プログラマに学ぶバージョン管理(第2版)

Subversion実践入門:達人プログラマに学ぶバージョン管理(第2版)