最近の更新

関連


その他いろいろ

多機能フィードリーダSynapse製作中

2月までは目が回るほど忙しい

MODxでつくる! 最強のCMSサイト カバー
MODxでつくる! 最強のCMSサイト

Python

Python2.5
初出:2007年04/29 更新:2007年05/02

この文書のキーワード: Python, Programming

Pythonのプログラミング情報。このページではよく使うモジュールのインストール方法を簡潔にまとめています。それぞれのサンプルやコードスニペットetc.は右のメニューからどうぞ。

自分のメモをまとめる場所として使っているので、横断的なプラットフォームやバージョンの調査はしていません。随時更新していますが、古い内容が含まれているかもしれないので注意してください。

EasyInstall

Linux編

.eggをインストールするため、easy_installを導入

$ wget http://peak.telecommunity.com/dist/ez_setup.py
$ sudo python ez_setup.py

Windows編

EasyInstallからez_setup.pyをDL

> ez_setup.py

これだけ。

SQLObject

インストール

$ sudo easy_install SQLObject

SQLAlchemy

インストール

$ sudo easy_install SQLAlchemy

#!/usr/bin/python
from sqlalchemy import *

db = create_engine('mysql://user:pass@localhost/database')
metadata = BoundMetaData(db)
metadata.engine.echo = True

# create table
if False:
  dummyposts_table = Table('dummyposts', metadata,
    Column('id', Integer, primary_key=True),
    Column('title', String(80)),
    Column('description', String(80)),
    )
  dummyposts_table.create()

# autoload
posts_table = Table('posts', metadata, autoload=True)
print list(posts_table.columns)

その他サンプルやチュートリアルはSQLAlchemy

OpenGL + Freetype2 + FTGLで日本語文字列表示

Windowsで日本語文字列をOpenGLで使うため、FTGLを使う。

  1. Visual C++ Express EditionでFreetype2のbuilds\win32\visualc\freetype.dspを開き、ビルド。
  2. 同じく、FTGLのwin32_vcpp\ftgl.dswを開き、追加のインクルードディレクトリにFreetype2のinclude, 追加のライブラリディレクトリにFreetype2のobjsを加え、リンクするライブラリ(freetype???MT_D.lib)を適合するバージョンの物に書き換えて、ビルド。

PyOenGL

Windows編

PyOpenGLからソースをダウンロード。Win用バイナリパッケージはないらしく、setup.pyを走らせてもsetuptoolsを要求される場合はEasyInstallのインストールを先に行う。

GLUT修正

\OpenGL\GLUT\special.pyの_base_glutDestroyWindowを設定している行のインデントが狂っていて、そのままビルド・インストールするとfrom OpenGL.GLUT import *

File "build\bdist.win32\egg\OpenGL\GLUT\special.py", line 73, in <module>
AttributeError: 'NoneType' object has no attribute 'glutDestroyWindow'

とかいってこけるので、修正。(73行目付近。インデントレベルを上の_base_glutInitと同じに)

> setup.py build
> setup.py install

これでインストール完了。

glut32.dll

glut32.dllが%WINDIR%system32またはカレントディレクトリにないと、一見関係なさそうなエラー

OpenGL.error.NullFunctionError: Attempt to call an undefined function __glutInitWithExit
, check for bool(__glutInitWithExit) before calling

が発生することがある(DLL中のエントリポイントのロードに失敗している)。GLUT for Win32からGLUTバイナリをダウンロードし、system32にdllを放り込めばOK

NumPy

Python用数値演算ライブラリ(主にPyOpenGLの行列計算に使う)をインストール。

Windows編

NumPyからWindows用バイナリをダウンロード、インストール。

PyGame

Python用ゲームライブラリ(入力・2D描画などの管理)PyGame。Python2.5版がやっと(少し前まで2.3版しかなかった)出たので、入れてみます。

Windows

pygame DownloadからWindows, Python2.5用バイナリをダウンロード、インストール。

Python Imaging Library (PIL)

Windows編

PILからバイナリをダウンロードしてインストール。

日本語文字列をImageFontで使う

スクリプトを適切な文字エンコーディング(utf-8など)で保存し、# -*- coding: utf-8 -*-などを指定し、

image = Image.new("RGB", (1000,500))
ix = image.size[0]
iy = image.size[1]
draw = ImageDraw.Draw(image)
draw.rectangle((0,0,ix,iy), fill=0xffffff)

font = ImageFont.truetype("C:\\Windows\\Fonts\\MSUIgothic.ttf", 72)#, encoding="unic") これはなくてもOK
text = u"てすと"
draw.text((0,0), text, font=font, fill=0)

などとすると、日本語文字列も扱えます。

RSS, Atomを使う: Universal Feed Parser

Universal Feed Parserを用いると、RSS,Atomなどを簡単にPythonから扱うことができる。当然日本語も扱える

Windowsへインストール

例のごとく

> setup build
> setup install

でインストール。

動画・音声用ライブラリPyMedia

PythonからAVI(各種コーデック), OGG, MP3などを扱うためのライブラリPyMedia.

2007.05.02時点ではWindowsバイナリはPython2.4のものしかないため、MinGWをインストールし、自分でコンパイルします。Current - MinGW - MinGW-x.x.x.exe をダウンロードし、実行。g++は必ず選択します。

Pymediaのソースコードを入手し、そのままコンパイルするとエラーが出るので手を加えます。

audio\acodec\acodec.c

30-31行:
#include 
#include "libavcodec/dsputil.h"
修正後:
#include "../libavcodec/avcodec.h"
#include "../libavcodec/dsputil.h"

250,272行, 423,445行, 843,865行等にに以下の行があれば:
	PyObject_GenericGetAttr,		/* tp_getattro */
	PyObject_Del, /* tp_free */
	PyType_GenericAlloc,			/* tp_alloc */
修正:
	0,		/* tp_getattro */
	0, /* tp_free */
	0,   /* tp_alloc */
initacodec(void)の中に追加:
ACStringType.tp_getattro = PyObject_GenericGetAttr;
ACStringType.tp_free = PyObject_Del;
FrameType.tp_getattro = PyObject_GenericGetAttr;
FrameType.tp_free = PyObject_Del;
FrameType.tp_alloc = PyType_GenericAlloc;
DecoderType.tp_getattro = PyObject_GenericGetAttr;
DecoderType.tp_alloc = PyType_GenericAlloc;
DecoderType.tp_free = PyObject_Del;
EncoderType.tp_getattro = PyObject_GenericGetAttr;
EncoderType.tp_alloc = PyType_GenericAlloc;
EncoderType.tp_free = PyObject_Del;


audio\libavcodec\common.h

28行:
#    include "config.h"
修正後:
#    include "../config.h"

38行:
#    include 
修正後:
//#    include 

71行:
#    include "bswap.h"
修正後:
#    include "../bswap.h"

133行:
/* bit output */
追加:
#define UINT8 uint8_t
#define UINT16 uint16_t
#define UINT32 uint32_t
#define INT8 int8_t
#define INT16 int16_t
#define INT64 int64_t


audio\libavcodec\wmadec.c

488行:
            norm = (1.0 / (float)(1I64 << 31)) * sqrt(3) * s->noise_mult;
修正後:
            norm = (1.0 / (float)((INT64)1 << 31)) * sqrt(3) * s->noise_mult;

audio\libavcodec\mpegaudiodec.c

追加:
#define int64_t_C(c) ((INT64)c)

video\muxer\demuxer.c

追加:
#include "../libavcodec/avcodec.h"
#include "../libavformat/avformat.h"

572行目付近DemuxerType:
	PyObject_GenericGetAttr,		/* tp_getattro */
	PyType_GenericAlloc,			/* tp_alloc */
	PyObject_Del,				/* tp_free */
修正後:
	0,		/* tp_getattro */
	0,			/* tp_alloc */
	0,				/* tp_free */
更にinitmuxer(void)内に追加:
DemuxerType.tp_getattro = PyObject_GenericGetAttr;
DemuxerType.tp_alloc = PyType_GenericAlloc;
DemuxerType.tp_free = PyObject_Del;
MuxerType.tp_alloc = PyType_GenericAlloc;
MuxerType.tp_free = PyObject_Del;

video\libavformat\avformat.h

12,16行:
#include 
#include "common.h"
#include 
修正後:
#include "../libavcodec/avcodec.h"
#include "../common.h"
#include "../libavcodec/rational.h"

video\muxer\muxer.h

5-6行:
#include "libavcodec/avcodec.h"
#include "libavformat/avformat.h"
修正後:
#include "../libavcodec/avcodec.h"
#include "../libavformat/avformat.h"

video\libavformat\avio.h

先頭に追加:
#define UINT8 uint8_t
#define UINT16 uint16_t
#define UINT32 uint32_t
#define UINT64 uint64_t
#define INT8 int8_t
#define INT16 int16_t
#define INT64 int64_t

video\libavcodec\common.h

23,162行:
#    include "config.h"
#    include "bswap.h"
修正後:
#    include "../config.h"
#    include "../bswap.h"

video\muxer\muxer.c

30行:
#include "libavformat/patch.h"
修正後:
#include "../libavformat/patch.h"

625行MuxerType:
	PyType_GenericAlloc,			/* tp_alloc */
	PyObject_Del,				/* tp_free */
修正後:
	0,			/* tp_alloc */
	0,				/* tp_free */
力つきました

まだまだエラーが…

> setup.py build
> setup.py install

でビルド・インストール