2013-04-03
Windows7にIPythonをインストールする
tips | |
主にLinuxでPythonを使っているが、Windowsでも使ってみたくなって、ついにインストールした。
Pythonのインストール
http://www.python.org/download/
ここから自分の環境に合ったものをインストールして下さい。
自分は、
- Python 2.7.3 Windows Installer (Windows binary -- does not include source)
をインストールしました。
IPythonのインストール
http://epd-free.enthought.com/
ここから自分の環境に合ったものをインストールして下さい。
自分は、
をインストールしました。
pyreadlineのインストール
https://pypi.python.org/pypi/pyreadline
ここから自分の環境に合ったものをインストールして下さい。
IPythonを実行したらこのライブラリも必要だと言われたため、インストールしました。
自分は、
- pyreadline-1.7.1.win32.exe (md5)
をインストールしました。
動作確認
$ipython Python 2.7.3 (default, Apr 10 2012, 23:31:26) [MSC v.1500 32 bit (Intel)] Type "copyright", "credits" or "license" for more information. IPython 0.13.1 -- An enhanced Interactive Python. ? -> Introduction and overview of IPython's features. %quickref -> Quick reference. help -> Python's own help system. object? -> Details about 'object', use 'object??' for extra details. In [1]: print "Hello,World!" Hello,World! In [2]:
ちゃんと動いたっぽい。
2012-12-04
Python哲学 The Zen of Python
tips | |
最近知ったのだけれども、裏コマンドみたいな?やつ。
Pythonの哲学が好きでPythonで書いているんだけど、そのオリジナルのドキュメント。
>>> import this The Zen of Python, by Tim Peters Beautiful is better than ugly. Explicit is better than implicit. Simple is better than complex. Complex is better than complicated. Flat is better than nested. Sparse is better than dense. Readability counts. Special cases aren't special enough to break the rules. Although practicality beats purity. Errors should never pass silently. Unless explicitly silenced. In the face of ambiguity, refuse the temptation to guess. There should be one-- and preferably only one --obvious way to do it. Although that way may not be obvious at first unless you're Dutch. Now is better than never. Although never is often better than *right* now. If the implementation is hard to explain, it's a bad idea. If the implementation is easy to explain, it may be a good idea. Namespaces are one honking great idea -- let's do more of those!
Pythonの組み込み関数一覧(builtin functions)
tips | |
意外と簡単に網羅出来た。
一応バージョンは以下
Python 2.7.3 (default, Apr 10 2012, 23:31:26) [MSC v.1500 32 bit (Intel)] on win32 Type "copyright", "credits" or "license()" for more information. まず、locals()関数を呼び出す。
>>> locals() {'__builtins__': <module '__builtin__' (built-in)>, '__name__': '__main__', '__doc__': None, '__package__': None}
'__builtins__'の中に組み込み関数や予約語の定義がある。
dir()で見る。
Pythonの命名規則で、定義は大文字からはじまり、関数は小文字から始まるようになっている様子。(たぶん・・・)
>>> for spam in dir(__builtins__): print spam ArithmeticError AssertionError AttributeError BaseException BufferError BytesWarning DeprecationWarning EOFError Ellipsis EnvironmentError Exception False FloatingPointError FutureWarning GeneratorExit IOError ImportError ImportWarning IndentationError IndexError KeyError KeyboardInterrupt LookupError MemoryError NameError None NotImplemented NotImplementedError OSError OverflowError PendingDeprecationWarning ReferenceError RuntimeError RuntimeWarning StandardError StopIteration SyntaxError SyntaxWarning SystemError SystemExit TabError True TypeError UnboundLocalError UnicodeDecodeError UnicodeEncodeError UnicodeError UnicodeTranslateError UnicodeWarning UserWarning ValueError Warning WindowsError ZeroDivisionError _ __debug__ __doc__ __import__ __name__ __package__ abs all any apply basestring bin bool buffer bytearray bytes callable chr classmethod cmp coerce compile complex copyright credits delattr dict dir divmod enumerate eval execfile exit file filter float format frozenset getattr globals hasattr hash help hex id input int intern isinstance issubclass iter len license list locals long map max memoryview min next object oct open ord pow print property quit range raw_input reduce reload repr reversed round set setattr slice sorted staticmethod str sum super tuple type unichr unicode vars xrange zip >>>
Heather2013/03/15 16:24That's really thininkg of the highest order
kqljbqczdig2013/03/17 10:46hFKdyd , [url=http://bsfrfqwkvlph.com/]bsfrfqwkvlph[/url], [link=http://dihwrdrcbnip.com/]dihwrdrcbnip[/link], http://kepbyvbburai.com/
ojznuo2013/03/18 21:09UxNW0Z <a href="http://sfwavnxqxycb.com/">sfwavnxqxycb</a>
nldvshapclk2013/03/19 07:09Ciqxgl , [url=http://qwtefnzopzhp.com/]qwtefnzopzhp[/url], [link=http://julzfsaydoxt.com/]julzfsaydoxt[/link], http://oprebholqtnw.com/
2012-11-18
Pythonも文字列と数値を+演算子で連結しようとするとエラーが発生。TypeError: cannot concatenate 'str' and 'int' objects
tips | |
HTMLの勉強をしようと、いろいろとタグ付けしてみて動作を見ようとしていてPythonに書かせていたときにつまづいたところ。
よくやっちまうんだけど、文字列と数値を連結して表示させようとすると怒られる。
Sample Code(サンプルコード)
print'<FORM>' for size in range(1,21): size *= 10 print '<INPUT type="text" size="' + size + '" name="' + 'textboxid'+ size +'"><br>' print'</FORM>'
Result(実行結果)
Traceback (most recent call last): File "C:/Users/One-shot life/Desktop/html.py", line 3, in <module> print '<INPUT type="text" size="' + size + '" name="' + 'textboxid'+ size +'"><br>' TypeError: cannot concatenate 'str' and 'int' objects
自分が悪いっちゃ悪いんだけど、いまいち釈然としない。
Pythonは、変数使用時、変数の型が自動的に決定される。変数を使用するのに、いちいち宣言をせねばならんのは面倒だ!と思っている私には、Pythonの言語仕様は気に入っている。
>>> digit =10 >>> string ='10' >>> digit + string Traceback (most recent call last): File "<pyshell#6>", line 1, in <module> digit + string TypeError: unsupported operand type(s) for +: 'int' and 'str' >>> >>> digit 10 >>> string '10'
とこうなるわけですよね?この例で言うと、変数digitとstringの型は自動的に決定されている。じゃぁ、+演算子使ったときにも自動的に決定してくれないか?と私は思うわけです。もちろん、いつでも勝手にやってくれと思うわけではなく、明らかに文字列だとわかる場合は文字列として扱ってくれよというレベル。printの引数なんだから、表示したいっていうのはこちらからすれば明確。Pythonからすれば不明確と言われてしまえばそれまでだけど・・・。
数値と文字列をプログラマが区別しないと、勝手に動作して暴走して困るというシーンは無いわけではないけれども、もうちょっと融通利かないもんかなと思う。
数値を明示的に文字列にキャストすれば問題ないのだけれども・・・。
Sample Code(サンプルコード)
for size in range(1,21): size *= 10 print '<INPUT type="text" size="' + str(size) + '" name="' + 'textboxid'+ str(size) +'><br>'
Result(実行結果)
<FORM> <INPUT type="text" size="10" name="textboxid10"><br> <INPUT type="text" size="20" name="textboxid20"><br> <INPUT type="text" size="30" name="textboxid30"><br> <INPUT type="text" size="40" name="textboxid40"><br> <INPUT type="text" size="50" name="textboxid50"><br> <INPUT type="text" size="60" name="textboxid60"><br> <INPUT type="text" size="70" name="textboxid70"><br> <INPUT type="text" size="80" name="textboxid80"><br> <INPUT type="text" size="90" name="textboxid90"><br> <INPUT type="text" size="100" name="textboxid100"><br> <INPUT type="text" size="110" name="textboxid110"><br> <INPUT type="text" size="120" name="textboxid120"><br> <INPUT type="text" size="130" name="textboxid130"><br> <INPUT type="text" size="140" name="textboxid140"><br> <INPUT type="text" size="150" name="textboxid150"><br> <INPUT type="text" size="160" name="textboxid160"><br> <INPUT type="text" size="170" name="textboxid170"><br> <INPUT type="text" size="180" name="textboxid180"><br> <INPUT type="text" size="190" name="textboxid190"><br> <INPUT type="text" size="200" name="textboxid200"><br> </FORM>
2012-10-24
hatena記法(スーパーpre記法:シンタックス・ハイライト)でPython Shellから書いたときに、コピペすればブログに転機出来る形式で出力したい。
tips | |
こんな感じのファイルを用意してPython ShellでRUNする。
def start(): print'>|python|' def end(): print'||<' def h(): t =raw_input("Title:") print "**%s" %t
こんな感じで出力される。
>>> h()
Title:spam
spam
>>> start()
>>> print "Hello world." Hello world. >>> end()
なんだか醜いなぁ。
これだったら、Linuxシェル使って、出力と実効の間にはてな記法挟んだほうがすっきりするかな。でも、Python Shellを使いたい・・・。何か上手い方法ないですかね?
2012-10-23
MS-DOSのプロンプト表示をPython Shell風にする
tips | |
C:\Users\One-shot life>prompt $g$g$g >>>
$gというのが>を意味するエスケープ文字。Python Shellっぽいでしょ?見た目に騙されないようにw
>>>type hello.pl use strict; use warnings; print"Hello Python" >>>perl hello.pl Hello Python >>>
こんな感じで使います。
DenisseYeah, that's the tckiet, sir or ma'am
qcmhzrvdvwchKp12K <a href="http://wktsrnaiqdtm.com/">wktsrnaiqdtm</a>
cgryzk4f3wEg , [url=http://jpzfogwuxfbo.com/]jpzfogwuxfbo[/url], [link=http://bgthczgxtwsd.com/]bgthczgxtwsd[/link], http://bhjyemojipvs.com/
cgdhucwcfpdw3apE <a href="http://jyyqnrddxfjg.com/">jyyqnrddxfjg</a>
cgdhucwcfpdw3apE <a href="http://jyyqnrddxfjg.com/">jyyqnrddxfjg</a>