Javaとアーチェリーと


9月 2006
      1  2 
 3  4  5  6  7  8  9 
 10  11  12  13  14  15  16 
 17  18  19  20  21  22  23 
 24  25  26  27  28  29  30 
8  |  今日  |  10






はてなブックマーク数

WebLogic Server 9.x には Java ベースの Python 実装、Jython が含まれています。
Jython は Python のコードを Java のバイトコードに変換して実行します。結果的には Python コードが (JVM の実装、判断次第では)ネイティブコードで動くことになります。
使い方は簡単、setEnv.sh/cmd で環境変数を整えて weblogic.WLST クラスを起動するだけ。引数を指定しなければインタラクティブモードになり、Pythonコードが書かれたファイルを指定すれば直接起動します。

~/bea92/weblogic92/samples/domains/wl_server$ . bin/setDomainEnv.sh 
-bash: Don't know how to set the shared library path for Darwin.
~/bea92/weblogic92/samples/domains/wl_server$ java weblogic.WLST

Initializing WebLogic Scripting Tool (WLST) ...

Welcome to WebLogic Server Administration Scripting Shell

Type help() for help on available commands

wls:/offline> 1+2
3
wls:/offline> _
3
wls:/offline> hello = "hello"
wls:/offline> hello[0:2]
'he'
wls:/offline> ^D
~/bea92/weblogic92/samples/domains/wl_server$ cat hello.py
print "hello"
~/bea92/weblogic92/samples/domains/wl_server$ java weblogic.WLST hello.py

Initializing WebLogic Scripting Tool (WLST) ...

Welcome to WebLogic Server Administration Scripting Shell

Type help() for help on available commands

hello$ cat hello.py
print "hello"
~/bea92/weblogic92/samples/domains/wl_server$ java weblogic.WLST hello.py

Initializing WebLogic Scripting Tool (WLST) ...

Welcome to WebLogic Server Administration Scripting Shell

Type help() for help on available commands

hello
~/bea92/weblogic92/samples/domains/wl_server$


Jython について詳しくはこちら。
http://www.jython.jp/
http://www.jython.org/Project/index.html
http://sourceforge.net/projects/jython/

タグ :

今回は 3. An Informal Introduction to Python
斬新な文法だらけでほとんど丸写しに近いメモになってしまいました。

インタラクティブモードでは式を評価した結果が即座に表示されます。

$ python
Python 2.3.5 (#1, Mar 20 2005, 20:38:20)
[GCC 3.3 20030304 (Apple Computer, Inc. build 1809)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> 1+1
2
>>> "hello"
'hello'
>>>


最後に評価された値は "_" で参照できます。
>>> 2*3
6
>>> _*4
24
>>> "hello"
'hello'
>>> _+" world"
'hello world'
>>>


ダブル(シングル)クオーテーション3つで囲むと改行やダブル(シングル)クオーテーションをエスケープせずに記述できます。
>>> """
... This
... is
... "a"
... sentence
... """
'\nThis\n is\n "a"\nsentence\n'
>>> _
'\nThis\n is\n "a"\nsentence\n'
>>> print _

This
is
"a"
sentence

>>>


ダブルクオーテーションで囲むとシングルクオーテーションをエスケープせずに掛けます。逆も然り。
>>> "It's a SONY"
"It's a SONY"
>>> 'It\'s a "SONY"'
'It\'s a "SONY"'
>>> print _
It's a "SONY"


r" で始めると Raw text として認識され、エスケープが行われません。
>>> hello = r"This is a raw text\
... '\n' won't be converted."
>>> print hello
This is a raw text\
'\n' won't be converted.
>>>


サブストリングの抽出はこんな感じ。
>>> hello = "hello"
>>> hello[0]
'h'
>>> hello[10]
Traceback (most recent call last):
File "", line 1, in ?
IndexError: string index out of range
>>> hello[0:2]
'he'
>>> hello[:2]
'he'
>>> hello[2:]
'llo'
>>> hello[-2]
'l'
>>> hello[-2:]
'lo'
>>> hello[:-2]
'hel'
>>>

タグ :

Python に手をつけてみました。
とりあえず本家のチュートリアルベースに勉強しながら忘れそうなことをメモしていくことにします。
・Python Tutorial
http://docs.python.org/tut/tut.html

- Mac OS X の Python
Mac OS X は標準で Python がインストールされてます。

$ py[tab]
pydoc pydoc2.4 python2.3 pythonw
pydoc2.3 python python2.4 pythonw2.3
$ whereis python
/usr/bin/python
$ ls -la /usr/bin/python
lrwxr-xr-x 1 root wheel 9 May 1 2005 /usr/bin/python -> python2.3
$ python2.3
Python 2.3.5 (#1, Feb 19 2006, 00:26:06)
[GCC 4.0.1 (Apple Computer, Inc. build 5250)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> quit
'Use Ctrl-D (i.e. EOF) to exit.'
>>> ^D
$ python2.4
Python 2.4.2 (#1, Feb 18 2006, 23:37:11)
[GCC 4.0.1 (Apple Computer, Inc. build 5250)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> ^D

どうやら Python2.3.5 / 2.4.2 がインストールされていて、python は python2.3 へリンクが張られてるらしい。

というわけで 2. Using the Python あたりを実践。

- Python の実行方法
・インタラクティブモード
コマンドラインから python とうつだけ
$ python
Python 2.3.5 (#1, Mar 20 2005, 20:38:20)
[GCC 3.3 20030304 (Apple Computer, Inc. build 1809)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> print "hello"
hello
>>> ^D

・ファイルに保存して
$ cat hello.py 
print "hello"
$ python hello.py
hello

・直接実行
$ cat hello.py 
#! /usr/bin/env python
print "hello"
$ chmod +x hello.py
$ ./hello.py
hello


- エンコーディングの指定方法
$ cat hello.py 
#! /usr/bin/env python
# -*- coding: Shift_JIS -*-
print "こんにちは"
$ ./hello.py
File "./hello.py", line 2
SyntaxError: 'unknown encoding: Shift_JIS'

そんなエンコーディングは知らないとエラーがでた。
"SyntaxError" ってのはなんかニュアンスがおかしいけど・・。
調べてみるとJapaneseCodecsっていうパッケージ(?)が必要らしい。
日本語環境でのPython (for Python 2.3 or later)
Python2.4 ではデフォルトで含まれているそう。
また、エンコーディングの指定はもうちょっとシンプルに、
# coding: エンコーディング名
で良いらしい。
使えるエンコーディングの一覧はこちら
http://docs.python.org/lib/standard-encodings.html

というわけで Python2.4 で動かしてみる。
$ cat hello.py 
#! /usr/bin/env python
# -*- coding: shiftjis -*-
print "日本語"
$ python2.4 hello.py
Bus error

バスエラーが発生。なんとなく euc にしてみたら・・
$ cat hello.py 
#! /usr/bin/env python
# -*- coding: eucjp -*-
print "日本語"
$ python2.4 hello.py
日本語

動いた。

タグ :