2013-05-01から1ヶ月間の記事一覧

型リテラルの確認

type()で型リテラルの取得 >>> print type(10) <type 'int'> >>> print type("abc") <type 'str'> isinstance()で型リテラルを確認 >>> print isinstance(100, int) True >>> print isinstance(100, str) False</type></type>

dictionaryのkeyを取得する

dictionaryのキーを取得するにはiteritems() を使用する >>> dict = {"apple":2, "banana":3} >>> (apple,banana) =dict.iteritems() >>> apple ('apple', 2) >>> banana ('banana', 3) PHPのforeachのような使い方 >>> for key, value in dict.iteritems():…

listのindexを取得する

enumerateを使用する PHPでいうとforeach >>> list = ["apple", "orange", "banana"] >>> for index, value in enumerate(list): ... print index ... print value 0 apple 1 orange 2 banana

配列の逆順処理 reverse, reversed

listの逆順処理 reverseは入れ替え reversedは逆から読み出し #! /usr/bin/python # -*- conding: utf-8 -*- list = [] list.append("apple") list.append("banana") list.append("orange") #listを逆順配置 list.reverse() for value in list: print value …

ConfigParser package

外部設定ファイルとそのparserがある。 [section] var = value または var:value のように記述できる。config.ini [global] company = hoge [local] name = letitride url = d.hatena.ne.jp/letitride github : https://github.com/letitride読み出しは Conf…

haldが起動しない

haldaemonのメモリ使用量が異常だったので、再起動しようとすると起動しない。 どうやら、messagebusに依存しているようなので、 # /etc/init.d/messagebus start # /etc/init.d/haldaemon startとして起動する。

lambda式

無名関数のようなもの。 >>> fumc = lambda arg1, arg2 : arg1 + arg2 >>> fumc(2, 5) 7 式なので、以下のように書くこともできる。 >>> func_list = [lambda arg: arg*1, lambda arg: arg*2] >>> for func in func_list: ... func(2) ... 2 4 >>> func_list…

パッケージの作成、読み込み

パッケージの作成 ファイル名がパッケージ名となる$ vi package_sample.py #! /usr/bin/python # -*- coding: utf-8 -*- def sample( arg1, arg2 ): return arg1 + arg2 パッケージの読み込み sys.path.appendでパッケージファイルの配置場所を登録。 import…

python クラスの継承

クラス名の後に継承するクラス名を()で囲んで記述する class WorkClass( SuperClass ): def __init__(self): SuperClass.__init__(self)

テーブル名の変更

初めて使ったのでメモ。 ALTER TABLE old_tablename rename to new_tablename;

pythonのお勉強 classの作成

class keywordで宣言。 __init__は所謂コンストラクタの定義。selfはthisのように自身を指す。self引数は省略不可。 class test: def __init__(self, arg1, arg2): self.name = arg1 self.status = arg2 t = test( "TEST1", 1 ) print t.name

pythonのお勉強 関数の作成

def で宣言、作成する >>> #関数の作成 >>> def my_func(): ... print "my func" ... >>> my_func() my func >>> #引数つき >>> def my_func(arg1, arg2): ... print arg1 + arg2 ... >>> my_func("string1", "string2") string1string2 >>> #default引数 >>…

pythonのお勉強 例外処理

try〜except〜finalyで記述 raise句で呼び出し元に例外を通知 #! /usr/bin/python # coding: utf-8 def test( arg1, arg2 ): ret = 0 try: ret = arg1 + arg2 except: print "exception" #例外を送信 raise finally: print "finaly" return ret try: print t…

pythonのお勉強 loop

お馴染みのfor loopとwhile loopがありforは基本、コレクションをiterateする >>> list = ["itme1","item2","item3"] >>> for value in list: ... print value ... itme1 item2 item3 #dictionaryの場合はkeyが参照される >>> dict = {"k1":"item1","k2":"it…

vimでgrep

vim

先ず、検索対象のディレクトリへ移動して :cd /pathgrepを実行 :vimgrep keyword **/*.txt結果の一覧を見たい場合は :copenで確認可能

pythonのお勉強その5 if条件式

条件式を()で括る必要がない。 { 処理... }を :とインデントで表す。 else ifはelifと記述する。 if value == "letit" and src == "": #インデントで制御する構文の為、「何もしない」を明示的に記述 pass elif value == "letit" and src == "ride": pass el…

pythonのお勉強その5 dictionary

dictionaryは所謂hash。 {}で初期化して扱える。 >>> dictionary = { ... "NAME" : "user name", ... "STATUS" : "1", ... } >>> print dictionary {'STATUS': '1', 'NAME': 'user name'} >>> for i in dictionary: ... print i ... print dictionary[i] ...…

pythonのお勉強その4 list

listは更新可能なコレクション []で囲んで初期化する >>> list = ["letitride", "-", "jp", ".", "net"] >>> print list ['letitride', '-', 'jp', '.', 'net'] >>> for i in list: ... print i ... letitride - jp . net >>> #末尾に値を追加 >>> list = []…

pythonのお勉強その3 tuple

タプルは初期化後に変更不可なコレクション ()で初期化を行う >>> data = ("let", "it", "ride",) >>> print data ('let', 'it', 'ride') >>> print data[1] it >>> data[1] = "is" Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: 'tu</module></stdin>…

pythonのお勉強その2

文字列はオブジェクトとして扱われる。 #! /usr/bin/python # -*- coding: utf-8 -*- # シングルまたは、ダブルクォートで括る 三連続はヒアドキュメントとして扱われる print 'string' print "string" print """ s1 s2 s3 """ # 文字列連結 print "let"+"it…