Skip to main content

97 posts tagged with "python"

View All Tags

· 3 min read

你 / 妳是否是去死去死團的一員?在這個充滿閃光與柴可夫斯基 1812 序曲的節日,有種暫時失明的感覺?

為了對抗這個節日,我們就來自製閃光卡吧!

這個閃光卡製作的方法,就是寫個單詞記憶程式,並把這個單詞記憶程式命名為 閃光卡 (FlashCard) (* 註) XD

寫一個簡單的記憶程式不用花太多時間:我們只需要一個詞庫,與一個用作檢視與複習詞條的介面。

> #!/usr/bin/python
> # -*- coding: utf-8 -*-
>
> deck = [("a","apple"),
> ("b", "boy"),
> ("c", "cat"),
> ("d","dog")] #詞庫,包含4筆詞條
>
> import random
>
> help = "commands: Fail/Lucky/Quit" #支援的命令
>
> while True:
> card = random.choice(deck) # 從詞庫裡挑一個詞條,如 ('d', 'dog')
> which = random.choice(card) # 從詞條裡挑一個詞,如 ('d', 'dog') 中挑 'd' 或 'dog'
>
> print which #顯示詞
> print card[0], card[1] #印出完整答案
>
> action = raw_input("> ").lower() #輸入命令
> if action == "q": #按其他鍵繼續,按q就跳出迴圈
> break
> if "f" in action or "l" in action: #忘記時的處理策略
> print "...recorded."
> deck.append(card) #多加本詞條入詞庫中,以提升被選中的頻率
> if "h" in action:
> print help #印出說明
>

進一步修改參考:

  • 若要用作英文學習的話,不只前項 (英文)、後項 (中文解釋),也許還要一欄音標。

  • 詞庫應該要可以抽換

  • 詞庫應該要可以合併使用 (應該支援多詞庫)

  • 要用更有效的學習模式,如sm2

  • 應該可以用圖片代替文字,以做到支援「音標」等效果。參考資料

  • flashcard

  • Simple flashcard program in Python

* 註:英文中, flashcard 就是單詞記憶卡的意思。

· 3 min read

繼上一篇裡我們製作好閃光卡後,這裡再附送一個補充包: 平假名 / 片假名詞彙庫,讓閃光卡真正能運用於日常生活中 XD

> Hiragana = [("あ","a"), ("い", "i"), ("う", "u"), ("え","e"), ("お", "o"),
> ("か", "ka"), ("き", "ki"), ("く", "ku"), ("け", "ke"), ("こ", "ko"),
> ("さ", "sa"), ("し", "shi"), ("す", "shu"), ("せ", "se"), ("そ", "so"),
> ("た", "ta"), ("ち", "chi"), ("つ", "tsu"), ("て", "te"), ("と", "to"),
> ("な", "na"), ("に", "ni"), ("ぬ", "nu"), ("ね", "ne"), ("の", "no"),
> ("は", "ha"), ("ひ", "hi"), ("ふ", "hu"), ("へ", "he"), ("ほ", "ho"),
> ("ま", "ma"), ("み", "mi"), ("む", "mu"), ("め", "me"), ("も", "mo"),
> ("や", "ya"), ("ゆ", "yu"), ("よ", "yo"),
> ("ら", "ra"), ("り", "ri"), ("る", "ru"), ("れ", "re"), ("ろ", "ro"),
> ("わ", "wa"), ("ゐ", "wi"), ("ゑ", "we"), ("を", "wo"),
> ("が", "ga"), ("ぎ", "gi"), ("ぐ", "gu"), ("げ", "ge"), ("ご", "go"),
> ("ざ", "za"), ("じ", "ji"), ("ず", "zu"), ("ぜ", "ze"), ("ぞ", "zo"),
> ("だ", "da"), ("ぢ", "(ji)"), ("づ", "(zu)"), ("で", "de"), ("ど", "do"),
> ("ば", "ba"), ("び", "bi"), ("ぶ", "bu"), ("べ", "be"), ("ぼ", "bo"),
> ("ぱ", "pa"), ("ぴ", "pi"), ("ぷ", "pu"), ("ぺ", "pe"), ("ぽ", "po")
> ] #平假名詞庫
>
> Katakana = [("ア", "a"), ("イ", "i"), ("ウ", "u"), ("エ","e"), ("オ", "o"),
> ("カ", "ka"), ("キ", "ki"), ("ク", "ku"), ("ケ", "ke"), ("コ", "ko"),
> ("サ", "sa"), ("シ", "shi"), ("ス", "shu"), ("セ", "se"), ("ソ", "so"),
> ("タ", "ta"), ("チ", "chi"), ("ツ", "tsu"), ("テ", "te"), ("ト", "to"),
> ("ナ", "na"), ("ニ", "ni"), ("ヌ", "nu"), ("ネ", "ne"), ("ノ", "no"),
> ("ハ", "ha"), ("ヒ", "hi"), ("フ", "hu"), ("ヘ ", "he"), ("ホ", "ho"),
> ("マ", "ma"), ("ミ", "mi"), ("ム", "mu"), ("メ", "me"), ("モ", "mo"),
> ("ヤ", "ya"), ("ユ", "yu"), ("ヨ", "yo"),
> ("ラ", "ra"), ("リ", "ri"), ("ル", "ru"), ("レ", "re"), ("ロ", "ro"),
> ("ワ", "wa"), ("ヰ", "wi"), ("ヱ", "we"), ("ヲ", "wo"),
> ("ン", "n"),
> ("ガ", "ga"), ("ギ", "gi"), ("グ", "gu"), ("ゲ", "ge"), ("ゴ", "go"),
> ("ザ", "za"), ("ジ", "ji"), ("ズ", "zu"), ("ゼ", "ze"), ("ゾ", "zo"),
> ("ダ", "da"), ("ヂ", "(ji)"), ("ヅ", "(zu)"), ("デ", "de"), ("ド", "do"),
> ("バ", "ba"), ("ビ", "bi"), ("ブ", "bu"), ("ベ", "be"), ("ボ", "bo"),
> ("パ", "pa"), ("ピ", "pi"), ("プ", "pu"), ("ペ", "pe"), ("ポ", "po")
> ] #片假名詞庫
>
> deck = Hiragana

使用時,依照需求將 deck 改成等於 "Hiragana"、"Katakana",或 "Hiragana+Katakana" 即可。

· One min read

scapy 是個相當好用的封包產生工具, 只是因為有幾個用到的函式庫需要手動尋找並安裝,還蠻不方便的. Windows 下使用的限制更多.

scapy 雖然只有一個檔案卻可以產生各種 Layer 2 以上的封包. 封包中有用到時會自行填入 mac, ip, checksum 等訊息,實在太省事了. scapy 允許自訂封包欄位來設計自己的協定,或改變封包的內容. 裝上一些圖形處理函式庫後還可以輸出 protocol 欄位圖形等等.

在此列出需求函式庫的連結. (Mac 環境下需安裝)

pcap http://code.google.com/p/pypcap/ libdnet http://sourceforge.net/projects/libdnet/

gnuplot http://sourceforge.net/projects/gnuplot/ gnuplot-py http://sourceforge.net/projects/gnuplot-py/ pyx http://sourceforge.net/projects/pyx/