2014年10月10日 星期五

012 python cProfile (part 1)


Using tool cProfile to take a look: how to measure and judge performance.

More detail reference: https://docs.python.org/2/library/profile.html
Example: https://github.com/vash-hsu/python/blob/master/python_test/brute_force.py

2014年8月11日 星期一

011 Twisted Network Programming, Hock httpd server for testing http status code


https://github.com/vash-hsu/python/tree/master/python_twisted

python_twisted>python httpd_mock_status_code.py
Usage: httpd_mock_status_code.py [Port Number]
  i.e. httpd_mock_status_code.py 8000
       hosting httpd on *:8000

2014年5月4日 星期日

010 steps to build a free email analysis tool chain

  一定有 bug,不解釋;等補完 unittest 再來 refactor。

|~~~~~~~~~|     |------|     |--------|     |--------|
| mailbox | ==> | pop3 | ==> | reader | ==> | trying |
|~~~~~~~~~|     |------|     |--------|     |--------|

  • 先從自己的信箱取得 mail sample
  • 使用 python 的 email 跟 google solution (也就是搜尋別人的解法)






  •  意思意思補三個單元測試



https://github.com/vash-hsu/python/tree/master/anti_spam

2014年4月20日 星期日

009 test coverage in python

  寫扣一起寫單元測試,就像炒菜要加鹽,接下來會問的問題就是,鹽加的夠不夠,會不會加太多,鈉含量會不會過高。

  • 鹽加的不夠,菜的鮮度提不上來;測試涵蓋率不高,扣的品質無人知道。
  • 鹽加的太多,傷腎傷胃血壓飆升;測試涵蓋度追高,傷肝傷心是否划算。
  • 鈉含量太高,影響智力骨質流失;搭謎測試衝業績,勝了比例輸了徵兆。
進一段廣告,讓我們來看看 python 怎麼做程式碼涵蓋率測試。

008 unittest in python

  寫扣不寫單元測試,感覺就像炒菜不加鹽,不是大問題,只是怪怪的。

  Let's have a try with unittest in python. There are threes examples mentioned below, one is for "__name__", another is by "import unittest", and the other is reserved for someone who would like to customize his/her unittest via command line.

2014年4月8日 星期二

007 Group Keyword and Counting for Top N

#!/usr/bin/env python

# http://katazen.blogspot.com/2014/04/006-python-cookbook-01-09-finding-common.html
# http://katazen.blogspot.com/2014/04/006-python-cookbook-01-12-most.html

# http://stackoverflow.com/questions/743806/split-string-into-a-list-in-python
# http://www.saltycrane.com/blog/2007/09/how-to-sort-python-dictionary-by-keys/

Requirements:

  • There is one text section and it needs to select Top 10 keywords, which occurs at least thrice.
  • Even there is two text sections, it is still easy to merge the meta statistic.
  • It should be friendly to redirect to formatted output, such as CSV for Weka.


006 Python Cookbook 01-12 Most Frequently Items

#!/usr/bin/env python

# http://chimera.labs.oreilly.com/books/1230000000393/ch01.html#_determining_the_most_frequently_occurring_items_in_a_sequence
# 1.12 determining the most frequently occurring items in a sequence

006 Python Cookbook 01-09 Finding Common

#/usr/bin/env python

# http://chimera.labs.oreilly.com/books/1230000000393/ch01.html#_finding_commonalities_in_two_dictionaries

2014年3月11日 星期二

006 Python Cookbook 01-04 Finding the Largest N

#!/usr/bin/env python

# http://chimera.labs.oreilly.com/books/1230000000393/ch01.html#findingthelargestorsmallest
# Finding the Largest or Smallest N Items

2014年3月7日 星期五

006 Python Cookbook 01-03 Last N Items

#!/usr/bin/env python

# http://chimera.labs.oreilly.com/books/1230000000393/ch01.html#_keeping_the_last_n_items

# Keeping the Last N Items

2014年3月4日 星期二

006 Python Cookbook 01-01 Unpacking a Sequence

#!/usr/bin/env python


# http://chimera.labs.oreilly.com/books/1230000000393/ch01.html#iterableunpack

# Unpacking a Sequence into Separate Variables




2014年1月26日 星期日

005 整數陣列搜尋大法 (參考解)

  回應的人太多了,就如同我當年的仰慕者可以從總統府排隊排到西門町那樣的多,篇幅有限,以下僅列出前四大回應
1. 拼錯字:WARM,WARN傻傻分不清
2. 沒寫註解
3. Code很臭,斷行縮排不照規矩
4. 變數命名不依照團隊編碼規範
科科,Code Review這樣就結束了?所以,你也滿意了嗎?真有那麼簡單,我還需要特別寫一篇網誌來屁嗎?

2014年1月14日 星期二

005 整數陣列搜尋大法 (找碴篇)

大家來找碴之 Code Review 好好玩

  試想一個情境,同事A被要求撰寫一個小模組,規格如下
在一個整數陣列中搜尋特定整數;
若尋得,返回該值位於陣列中的位移值;
若無得,返回-1
  因此,同事A花了幾分鐘,寫出了以下這幾段程式碼,並且很貼心的提供兩種search實作方式,給一般人使用的while 版本 (binarySearch_int) 或給神人使用的 recursive 版本  (binarySearch_int_R).

  現在問題來了,本團隊有採用 Code Review + Code Inspection 的習慣;現在請模擬你正處在會議室中,你是否有能力透過人腦編譯器你的專業能力,找出程式碼中可能有的問題或給予其他實質的建議?