Python Hack-a-thon で発表してきました!!
今回は資料の作成が後手に回ってしまい、運営の方々や発表者の皆様には大変迷惑をおかけしました。
さて、今回は ctypes 拡張モジュールに関する内容を発表させていただきました。
ctypes拡張モジュール
View more presentations from Moriyoshi Koizumi.
php-in-python の使い方
PHP-in-Pythonには、まだドキュメントがありません。インストール方法を簡潔に説明します。
a. Windowsの場合
b. Linux等Free Unixの場合
- php.netよりPHPのソースコードをダウンロードします。
- ソースコードを configure に --enable-maintainer-zts および --enable-embed=shared を渡す形でビルドします。
- make install します。
- インストールされた ${PREFIX}/lib/libphp5.so が共有ライブラリとして読み込めるように、${PREFIX}/lib が LD_LIBRARY_PATH に含まれるようにします。
以上の手順を踏んだ後で、
import php print php.eval("1 + 2")
のようにすると、1 + 2 が PHP のインタプリタで評価され、その結果が Python に返されます。
import php print php.run("test.php")
のようにすると、test.php が実行されます。
リクエストパラメータを PHP の実行環境に渡したい場合は
import php php.eval("var_dump($_GET['aaa'], $_POST['bbb']);", query_string="aaa=GET", content_type="application/x-www-form-urlencoded", post_data="bbb=POST")
のようにします。
おまけ
Mac OS X 上で、ctypes拡張モジュールを使ってダイアログボックスを出すコード。
import ctypes kCFUserNotificationStopAlertLevel = 0 kCFUserNotificationNoteAlertLevel = 1 kCFUserNotificationCautionAlertLevel = 2 kCFUserNotificationPlainAlertLevel= 3 kCFUserNotificationNoDefaultButtonFlag = (1 << 5) kCFUserNotificationUseRadioButtonsFlag = (1 << 6) kCFStringEncodingUTF8 = 0x08000100 dll = ctypes.CDLL('/System/Library/Frameworks/CoreFoundation.framework/CoreFoundation') dll.CFStringCreateWithCString.restype = ctypes.c_void_p dll.CFStringCreateWithCString.argtypes = ( ctypes.c_void_p, ctypes.c_char_p, ctypes.c_int) dll.CFRelease.argtypes = (ctypes.c_void_p,) dll.CFUserNotificationDisplayNotice.argstype = ( ctypes.c_double, ctypes.c_ulong, ctypes.c_void_p, ctypes.c_void_p, ctypes.c_void_p, ctypes.c_void_p, ctypes.c_void_p, ctypes.c_void_p) dll.CFStringCreateWithCString.restype = ctypes.POINTER(ctypes.c_int) title = dll.CFStringCreateWithCString(None, 'Hey', kCFStringEncodingUTF8) message = dll.CFStringCreateWithCString(None, 'Hello, binary world.', kCFStringEncodingUTF8) try: dll.CFUserNotificationDisplayNotice( ctypes.c_double(0.), kCFUserNotificationNoteAlertLevel, None, None, None, title, message, None) finally: dll.CFRelease(title) dll.CFRelease(message)