Cython 文法メモ

# ライブラリのインポート
from libc.math cimport sin
from libc.string cimport strcmp
from libc.stdio cimport fopen,fclose,fgets,fseek,ftell,fwrite

# 関数宣言
def main():

    #変数宣言
    cdef int a=1
    cdef char* s = "Hello マリオさん。こんにちは"
    cdef list l = [1,2,3,4,5]
    cdef dict d = {"one":1,"two":2}
    cdef tuple t = (1,2)
    cdef set se = {"one","two","three"}
    
    # fopenでのファイル読み込み
    fin = fopen("add.pyx","r")
    if not fin:
        print "読み込みミスった"
    cdef char str[1000] 
    fseek(fin, 0L, 1); # ファイルシーク
    while fgets(str,256,fin) != NULL:
        # print ftell(fin)
        print str
    fclose(fin) # ファイル閉じる

    # ファイル書き込み
    fout = fopen("output.txt","w")
    if not fout:
        print "読み込みミスった"
    fwrite(s, sizeof(char), len(s), fout);
    fclose(fout)

参考URL

Cython ドキュメント(和訳) — Cython 0.17.1 documentation