rabbitfoot530's diary

読んだ本と、プログラムに関することのメモです。好きな言語は、C++, Python, Golang, TypeScript。数学・物理・学習理論も好きです。

python

PythonによるLinuxプログラミングインタフェース(5章 mkstemp)

第5章のP115のテンポラリファイル作成のpythonコード。 #/usr/bin/env #-*- encoding: utf-8 -*- import tempfile import os import time # don't use # os.tmpnam (see: http://docs.python.org/2/library/os.html#os.tmpnam) # os.tempnam (see: http://do…

PythonによるLinuxプログラミングインタフェース(5章 truncate)

第5章P109のtruncateについて。 #/usr/bin/env #-*- encoding: utf-8 -*- import os def pythonic(): path = "./p109_truncate.txt" file = open(path, 'w', encoding='utf-8') file.write("abc") # write "abc" and offset move to 3 is end of file. print…

PythonによるLinuxプログラミングインタフェース(5章 pread)

第5章のP105のpreadのサンプル。 #/usr/bin/env #-*- encoding: utf-8 -*- import os def pythonic(): file = open(__file__, encoding='utf-8') orig = file.tell() print("before offset: {0}".format(file.tell())) offset = 10 file.seek(offset) lines …

PythonによるLinuxプログラミングインタフェース(5章 dup)

5章P103のdupのpythonコード。 #/usr/bin/env #-*- encoding: utf-8 -*- import os def pythonic(): def run(): py_dup() py_dup2() def py_dup(): newfd = os.dup(1) print("newfd: " + str(newfd)) os.close(2) newfd = os.dup(1) print("newfd: " + str(n…

PythonによるLinuxプログラミングインタフェース(4章 read, write, lseek)

4章のP90ページのread, write, lseekのプログラム。 #/usr/bin/env #-*- encoding: utf-8 -*- import sys def pythonic(): try: file = open(sys.argv[1], 'r+', encoding='utf-8') except IOError as e: print("err: " + e) else: try: ap = 2 argc = len(s…

PythonによるLinuxプログラミングインタフェース(4章 copy)

4章P75のファイルをopenしてread、readした内容をwriteしてファイルをコピーするというプログラム。 pythonで書くと下記。 #/usr/bin/env #-*- encoding: utf-8 -*- import sys def usage_error(filename): print("{0} old-file new-file".format(filename)…

PythonによるLinuxプログラミングインタフェース(3章 stderror)

3章のstderrorに対するコード。 pythonだとエラーをキャッチしてから、e.strerrorで文字列としてエラーメッセージを取得できる。 #include <string.h> char *strerror(int errnum); #/usr/bin/env #-*- encoding: utf-8 -*- def my_open(path): """ os.strerror() is </string.h>…

PythonによるLinuxプログラミングインタフェース(3章 perror)

the linux programming interfaceというバイブルに載ってるコードをpythonで書くとどうなるのか、個人的なメモ。 ただ、同じコードをos.xxxとかで書くのは面白くなさそうなので、pythonならどうやるのかを調べながら書いていこうと思う。 というわけで、早速…

ipython notebookインストール

matplotlibのグラフを出しながら、コーディングするのはnotebookが便利と知りましたので、notebookをインストール! virtualenvで環境作ってから、下記のパッケージをpipでインストールするだけ。 Pygments==1.6 distribute==0.6.27 ipython==0.13.2 pyzmq==…

pyplot.showが動かない

pyplot.show()が動作しないって結構記事を見る。たぶんlibjpeg62が入ってないと思われる。下記でインストールして解決。 sudo aptitude install libjpeg62

YAMLから設定ファイル読み込み

tornadoも設定ファイルを読み込む機能を提供してくれてるけど、YAML形式で勝手に読み込むやり方でもいい。 import yaml obj = yaml.load(file("config.yaml")) print(obj["key"]["key2"]) YAML自体はこんな感じ key: key1: value1 key2: value2 これをtornad…

configファイルから設定値を読む

tornadoのコード内で、設定ファイルから設定値を読み込むdefineで使いたい設定のkeyを設定する。デフォルト値を設定することもできる。その他の設定をすることも可能。 define("port", default="8888") configファイルのパスを設定して、パースする。 option…

pexpectでinteractは使わない

pexpectでinteract()を使用する記事を結構みるけど、そのまま使ったらクラッシュしてしまったので、interactは使えない?みたい。interactの代わりにexpect(pexpect.EOF)を使うとうまくいく。 import pexpect SERVER = "local" USER = "root" PASSWORD = "Ra…

ユークリッドの互除法

ユークリッドの互除法が読んでる本で出てきたので、軽く実装。 def euclid(a, b): numerator = max(a, b) denominator = min(a, b) if numerator == 0 or denominator == 0: return None times = numerator / denominator remainder = numerator - denominat…

Cross origin requests are only supported for HTTP

Flaskで別のページのデータをXHRで読もうとしたら、エラー。 Cross origin requests are only supported for HTTP ご覧の通り、Google Chromeは、エラーだしてきます。なので、routeのところに、pathを追加してやって解決!

MacのPythonのバージョンの変更方法

port selectってコマンドでできるみたい。 インストールされてるpythonリスト port select --list python 現在のpythonのバージョン port select --show python pythonのバージョン変更(python3.2に変更の例) sudo port select --set python python32

has_key() is deprecated in

has_key()は、文字通り廃止予定みたいなので、inを使う。has_keyとの違いは、順序ぐらい? a = {'a': 1} if a.has_key('a'): print 'True' if 'a' in a: print 'True'

web2pyインストールから起動まで

インストール sudo pip install web2py アプリケーション作成 w2p_apps このまま、起動すると、tkがないっていわれるので、インストールしてない場合は、tkをインストールする。 sudo aptitude install python-tk 起動 w2p_run

emacs-for-pythonの\C-oについて

emacs-for-pythonを設定すると、\C-oがopen-next-lineという関数のデフォルトのキーバインドに設定されてしまう。これは、elscreenのprefixを\C-oに割り当てている人にとっては困る。。。特に、open-next-lineの関数を使わないのなら、epy-editing.elの中の…

EmacsでPythonを楽に設定する方法

EmacsのPythonの設定は、いろいろあってどれがいいのが、どの組み合わせがちゃんと動くのかが、複雑なので、それをお手軽に済ませるかつ、使いやすい環境を構築するには、emacs-for-pythonを使うhttp://gabrielelanaro.github.com/emacs-for-python/ (load-f…

ac-clear-variable-every-10-minutes ERROR

ac-clear-variable-every-10-minutes ERROR このエラーがemacs --debug-initででたら、auto-completeのファイルを削除してみる。