python

Pitインスコつまずきメモ

Pit入れてみる。 最初はeasy_installで入れたけどエラー発生。 $ sudo easy_install pit ... $ python >>> from pit import Pit >>> conf = Pit.get('test', {'require':{'user':'', 'pass':''}}) Traceback (most recent call last): File "<stdin>", line 1, in <module> </module></stdin>…

python_selectで簡単バージョン切り替え

Pythonのバージョンを切り替えてくれるpython_selectを入れてみた。 インストール sudo port install python_select 切り替え % python_select usage: python_select [-n] [-d] [-r] [-h] [-v] version -n Show commands to do selection but do not execute…

Python Challenge(11)

続いてレベル11。 わからないのでフォーラムを見ると、 Look very very closely at the image. http://www.pythonchallenge.com/forums/viewtopic.php?t=38 よく見ると、顔のような絵が浮かび上がる様に見える。 タイトルの「odd even」からピクセルの座標が…

Python Challenge(10)

続いてレベル10。牛をクリックすると、以下の数列が現れる。 a = [1, 11, 21, 1211, 111221, 5分ぐらい眺めてたら法則が分かった。 1⇒1個の1 11⇒2個の1 21⇒1個の2、1個の1 1211⇒1個の1、1個の2、2個の1 1112221⇒まずは愚直に、 ans = ['1'] for n in range(0…

Python Challenge(9)

続いてレベル9。タイトルは「connect the dots」。おそらく画像の抜け部分を線で結べばいいんだろう。 ソースを見てみると、

Python Challenge(8)

続いてレベル8。 画像の蜂の部分をクリックすると、ユーザ名とパスワードを聞かれて、「"inflate"に対するユーザ名とパスワード」とある。 インフレ?イミフなのでソースを見てみると、

Python Challenge(7)

続いてレベル7。 特にヒントとか無いので、画像の灰色の部分をデコードすると文章になるんだろう。 PILは3.0対応してないようなので今回は2.6で。といっても灰色の部分を抜粋するとか、どうやんのか分からないので色々ググってみると、 RGB (128, 128, 128) …

Python Challenge(6)

続いてレベル6。 画像からすぐzipが浮かんだけどzip()を使うんだと思ってしまい進まず。 フォーラムを見たら、 It's not that function that's a clue, but something else with the same name.とあったので、channel.zipをダウンロード。 解凍後のreadme.tx…

Python Challenge(5)

続いてレベル5。 「peak hell -> pickle」はすぐわかった。ソースにあったbanner.pをpickleで読み込んでみる。 import pickle f = open('banner.p', 'rb') print(pickle.load(f)) $ python challenge05.py [[(' ', 95)], [(' ', 14), ('#', 5), (' ', 70), (…

Python Challenge(4)

続いて第4問。 謎はページタイトル「follow the chain」と画像リンク先からすぐに解けた。 from urllib import request url = 'http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing=12345' count = 0 while (count < 400): print(count) res = re…

Python Challenge(3)

続いて第3問。 写真の通り、ちょうど3つの大文字に囲まれてる小文字。 import re def challenge003(str): return "".join(re.findall('[^A-Z][A-Z]{3}([a-z])[A-Z]{3}[^A-Z]', str)) if __name__ == '__main__': print(challenge003(open('challenge3.txt').…

Python Challenge(2)

続いて第2問。 ソースに「find rare characters in the mess below」とあるので文字毎の出現回数を数えてみる。 def count(str): counter = {} for s in str: counter[s] = counter.get(s, 0) + 1 print(counter) if __name__ == '__main__': count(open('ch…

Python Challenge(1)

PythonChallenge始めてみる。 問0は単純な計算なので問1から。まず思いつくのは単純にord()で文字コードを求めて2を足す。3.0.1で実行。 def convert(str): return "".join([chr(ord(s)+2) if ord('a') <= ord(s) <= ord('x') else (chr(ord(s)+1-(ord('z')-…

GoogleAppEngine再入門(4) -RSS Readerを作る-

前回:GoogleAppEngine再入門(3) -Datastore API 2- - souta-bot log 日経ソフトウェア200901号のPart3に習い簡易RSS Readerを作る! feedparser.py(http://code.google.com/p/feedparser/downloads/list)を使用 RSSアイコンをここから入手 ディレクトリ構成…

GoogleAppEngine再入門(3) -Datastore API 2-

前回:GoogleAppEngine再入門(2) -Datastore API 1- - souta-bot log DatastoreAPIの続き 動的なproperty作成 db.Modelを継承したdb.Expandoを使えばpropertyとして定義していない形式のデータも格納できる 早速日経ソフトウェア2009/1月号リスト5のコードを…

GoogleAppEngine再入門(2) -Datastore API 1-

前回:GoogleAppEngine再入門(1) -webapp Framework- - souta-bot log今回も引き続き日経ソフトウェア2009年1月号特集のPart2でテーマはDatastoreAPI AppEngineはファイルシステムにアクセスできないのでデータの永続化にはDatastore APIを使用する Datastor…

GoogleAppEngine再入門(1) -webapp Framework-

前回:GoogleAppEngine再入門(0) - souta-bot log Cmd-Dでmywikiをデプロイ -> エラーログが表示される。 unexpected error occurred. Aborting. Error 403: --- begin server output --- You do not have permission to modify this app (app_id=u'mywiki')…

GoogleAppEngine再入門(0)

日経ソフトウェア2009年1月号でGoogleAppEngineに再入門記事がWindowsオンリーなのでMac特有の部分は以下を参考に。 参考:http://d.hatena.ne.jp/tenkao/20080601/p1 GoogleAppEngineLauncher(Ver1.1.8)を起動 Cmd-Nで新規にアプリケーション作成 Applicati…

sys.pathまとめ

モジュールの検索パスを示す文字列のリスト(環境によって変わる) リストの順番で検索される。 環境変数PYTHONPATHとその他環境依存の値等でインタプリタ起動時に決定される。 sys.path[0]は実行したスクリプトがあるディレクトリになる。 $ python display_s…

メソッド名をtypoした場合

インポートした自作モジュールのメソッド名をtypoしていた場合、以下のようなエラーなる。 >>> import mymodule >>> mymodule.hoge() Traceback (most recent call last): File "<stdin>", line 1, in <module> AttributeError: 'module' object has no attribute 'hoge' 便</module></stdin>…

みんなのPython読了

みんなのPython読了。 プログラミング始めての人はみんPy、経験者はPythonチュートリアルって感じ。みんなのPython作者: 柴田淳出版社/メーカー: ソフトバンククリエイティブ発売日: 2006/08/22メディア: 単行本購入: 11人 クリック: 624回この商品を含むブ…

範囲終端指定比較

直接指定型 (2, 4) |0|1|2|3|4|5|結果:(2, 3, 4) 左端指定型 左側の切れ目をさす。(-1より覚えやすい?) (2, 4) |0|1|2|3|4|5| 結果:(2, 3) Java 左端指定型 public static void main(String[] args) { String str = "foobar"; S…

Python2.6インストール

MacにMac用バイナリでPython2.6をインストールしてみた。 ターミナルでバージョンを確認。 $ python -V Python 2.5.1 $ python2.6 -V Python 2.6 インストールはうまくいってる。パスが通ってない。 $ which python /usr/bin/python 2.5は/usr/binに、2.6は/…

属性の追加

Python クラスを定義し直すと生成済みのインスタンスは以前のクラス仕様に準じ、以降のインスタンス生成は新しいクラス仕様のみに準ずる。 >>> class Hoge(object): ... def __init__(self, greeting): ... self.greeting = greeting ... def say(self): ...…