Pythonのコンストラクタとデストラクタ

__init____del__を定義することで、クラスの生成時および破棄時に実行したい処理を記述できます。

>>> class C:
...   def __init__(self):
...     print '__init__'
...   def __del__(self):
...     print '__del__'
...
>>> c = C()
__init__
>>> del c
__del__

Pythonの__init__はただの関数であって、厳密にはコンストラクタとは呼べないそうです。

GCCのアップデート

Gentoo LinuxでGCCをアップデートする際の手順

# emerge -u gcc
# gcc-config -l
[1] x86_64-pc-linux-gnu-4.4.7
[2] x86_64-pc-linux-gnu-4.5.4 *
[3] x86_64-pc-linux-gnu-4.6.3
# gcc-config 3
 * Switching native-compiler to x86_64-linux-gnu-4.6.3 ...
>>> Regenerating /etc/ld.so.cache...

 * If you intend to use the gcc from the new profile in an already
 * running shell, please remember to do:

 * . /etc/profile
# env-update && source /etc/profile

リンク

GCC, the GNU Compiler Collection – GNU Project – Free Software Foundation (FSF)
https://gcc.gnu.org/

htaccessとhtpasswdを使ったBasic認証の設定

Basic認証は安全性に問題があり本格的に使うことは稀と思いますが、簡単に設置できるので簡易的なアクセス管理に便利です。

.htpasswdの作成

SSHなどで設置先のサーバーにログインできるという前提で説明します。

SSHでログインできない場合は、ローカルPCなどで.htpasswdを作成してアップロードすることになります。

新規作成

以下はlolipopというユーザー名で作成するサンプルです。

パスワードを聞かれますので、入力してください。

$ htpasswd -c .htpasswd lolipop
New password:
Re-type new password:
Adding password for user lolipop

作成される.htpasswdは次のようになります。

$ cat .htpasswd
lolipop:$apr1$hgHnvE.E$NAxrrnRVtg94iwPYHRpZT0

追加

lolipopというユーザー名を追加するサンプルです。

$ htpasswd lolipop
New password:
Re-type new password:
Adding password for user lolipop

.htaccessの設定

アクセス制限をかけたい場所に.htaccessを置きます。 /var/www/localhost/htdocs/.htpasswdは、実際には.htpasswdが置かれているパスに書き換えてください。

AuthUserFile /var/www/localhost/htdocs/.htpasswd
AuthName "example"
AuthType Basic
require valid-user

.htpasswd.htaccessに直接アクセスされないように次のアクセス制限も追加できます。(/etc/apache2/httpd.confなどで設定されている場合は不要です。)

<Files ~ "^.(htpasswd|htaccess)$">
  deny from all
</Files>

ローカルPCにhtpasswd実行環境がない場合は、.htpasswd作成ツール(サーバー移転に伴いサービス終了しました)などでも作成できます。