2014年7月29日 星期二

Python - 自訂訊息格式

Python有很好用的logging函式庫,並不必要自己去定義打印訊息的方法。下面僅是提供一個簡單的例子,說明如果想要取得呼叫來源的檔名和行數,可以利用traceback的堆疊訊息。
import traceback
import os, time

def __formalizeMessage(message):
    FILE, LINE, FUNCTION, TEXT = (0, 1, 2, 3)
    stack = traceback.extract_stack(limit = 3)
    return "{file}({line}) - {message}".format(file = os.path.split(stack[0][FILE])[-1], line = stack[0][LINE], message = message)

def printError(message):
    print("{0} [Error] {1}".format(time.asctime(), __formalizeMessage(message)))

def main():
    printError("Hello")  # The line number is 12.

if __name__ == "__main__":
    main()

打印出來的訊息為:
Wed Jul 30 14:56:05 2014 [Error] test.py(12) - Hello

2014年7月24日 星期四

Python - 序數轉換

忘了是在哪裡看到的,不常用,不過是一個滿有趣的寫法,利用python的dict,能將數字1變成1st,數字2變成2nd,數字3變成3rd,數字4變成4th等。
## Returns the ordinal number of a given integer, as a string. eg. 1 -> 1st, 2 -> 2nd, 3 -> 3rd, etc.
def ordinal(num):
    num = int(num)
    if 10 <= num % 100 < 20:
        return "{0}th".format(num)
    else:
        ords = {1 : "st", 2 : "nd", 3 : "rd"}.get(num % 10, "th")
        return "{0}{1}".format(num, ords)

Python - 移除目錄和檔案

不管用什麼程式語言,都會遇到一個狀況,就是要刪除檔案或目錄時,沒有一個簡易型函式能一起處理檔案和目錄。沒辦法,只好自己用已有的函式拼湊一個符合目標的函式。
import os, shutil

## Remove a file or directory.
def remove(path) :
    retval = False
    try:
        if os.path.exists(path):
            if os.path.isdir(path):
                shutil.rmtree(path, ignore_errors = True)
                retval = True
            elif os.path.isfile(path):
                os.remove(path)
                retval = True
        else:
            retval = True
    except OSError as e:
        print(e)
    except Exception as e:
        print(e)
    return retval

Python - URL判斷

Python的urllib能夠容易地處理request和response,也能夠協助URL的拆裝,但似乎少了一個檢查是不是URL的函式,在這裡提供一個簡易型函式,可以分辨是否帶有通訊協定的URL。(不限於http或https,只要有通訊協定的語法都可以)
import os
import urllib.parse

def isURL(path):
    return not os.path.isdir(path)\
        and not os.path.isfile(path)\
        and urllib.parse.urlparse(path).scheme != ""

Python - 檔案IO

Python對於檔案IO很友好,擁有一些常用的函式庫,最基本的莫過於open這個內建函式。
with open("text.txt", mode = "w") as file:
    file.write("Hello")
如果單純使用open,勢必要記得呼叫close關閉檔案串流,而且還要配合一些exception使用try except;但如果配合with ... as ...使用,則close的動作可以交給python,即使有exception發生,python也會呼叫close。

有些文章會說使用with ... as ...等於try ... except ... finally ...,但事實上使用with ... as ...還是有可能會發生exception,關鍵就在於檔名,如果open一個空字串,則整個with ... as ...還是會拋出exception,所以最好習慣上在with外頭加一層try ... except ...。

2014年7月23日 星期三

Python - namedtuple

C++中有struct型態可以快速建立一個輕型類別,在該類別中可以含有一些屬性,並用object.attribute的方式存取。在Python則能使用namedtuple做到這件事情。
from collections import namedtuple

## Component definition
Component_t = namedtuple("Component", ["data", "size", "source"])

## Create a component.
def Component(data, size, source):
    return Component_t(data, size, source)

## Convert a list of parameters into a component of module.
#  @param   array is a list.
def toComponent(array):
    return Component_t._make(array)


## Usage
comp1 = Component(data = "hello", size = 5, source = "test.txt")
print(comp1.data)

array = ["world", 5, "test.txt"]
comp2 = toComponent(array)
print(comp2.data)

Python - 有限狀態機

在撰寫模組時,因為使用有限狀態機來記錄每個模組的運作情況,所以寫了一個有限狀態機的類別供繼承用。
"""""""""""""""""""""""""""""""""""
Library
"""""""""""""""""""""""""""""""""""
from abc import ABCMeta, abstractmethod  # abstract class
from enum import Enum  # constant

"""""""""""""""""""""""""""""""""""
Class
"""""""""""""""""""""""""""""""""""
## Avoids having to specify the value for each enumeration member.
class AutoNumber(Enum):
   
    ## Override.
    def __new__(cls):
        value = len(cls.__members__) + 1
        obj = object.__new__(cls)
        obj._value_ = value
        return obj
   
## Finite state machine.
class FSM(object, metaclass = ABCMeta):
   
    ## State definition.
    class States(AutoNumber):
        Initial = ()
        Ready = ()
        Idle = ()
        Start = ()
        Pause = ()
        Stop = ()
        SendingRequest = ()
        SentRequest = ()
        ReceivingResponse = ()
        ReceivedResponse = ()
        Creating = ()
        Created = ()
        Storing = ()
        Stored = ()
        Reading = ()
        Read = ()
        Writing = ()
        Wrote = ()
        Removing = ()
        Removed = ()
        Parsing = ()
        Parsed = ()
        Finished = ()
        _Error = () # This is not public state.
        _Except = () # This is not public state.

    ## Constructor.
    def __init__(self):
        self._stateProcess = self.StateProcess
        self.__stateIndex = 0
        self._state = self._stateProcess[0]

    ## Get state process definition.
    #  @details  The state process could be a cycle. For example: [FSM.State.Initial, FSM.State.SendingRequest, FSM.State.ReceivingResponse, FSM.State.SendingRequest, FSM.State.Finished].
    #  @note     If there are two cycles with the same states, it is a wrong state process definition.
    #  @return   A list of state.
    @property
    @abstractmethod   
    def StateProcess(self):
        """Get state process definition."""
        return [FSM.States.Initial, FSM.States.Finished]

    ## Get state.
    #  @return   Current state.
    @property
    def State(self):
        """Get current state."""
        return self._state

    ## Get state before except.
    #  @return   The last state.
    @property
    def StateBeforeExcept(self):
        """Get previous state before except/error."""
        return self._stateProcess[self.__stateIndex]

    ## Process definition
    #  @param   skip - number of state to be passes. (optional)
    #  @param   end - if true, the latest state sets the ending state; otherwise, it sets the next state. (optional)
    #  @param   error - if true, the state sets the error state. (optional)
    def nextState(self, skip = 0, end = False, error = False):
        # avoid to reboot
        if self.isStopped():
            return
       
        # The highest priority
        if error is True:
            self._state = FSM.States._Error
            return
       
        # check parameters
        if skip < 0:
            self._state = FSM.States._Except
            return
       
        if end is True:
            self.__stateIndex = len(self._stateProcess) - 1
            self._state = self._stateProcess[self.__stateIndex]
            return
       
        self.__stateIndex = min(self.__stateIndex + 1 + skip, len(self._stateProcess) - 1)
       
        # check cycle
        i = self._stateProcess.index(self._stateProcess[self.__stateIndex])
        if i != self.__stateIndex:
            self.__stateIndex = i
       
        self._state = self._stateProcess[self.__stateIndex]

    ## Reset state to the initial process.
    def resetState(self):
        self.__stateIndex = 0
        self._state = self._stateProcess[0]

    ## Check if state is end or not.
    #  @retval  true if the state is the end.
    #  @retval  false otherwise.
    def isEndState(self):
        return self._state == self._stateProcess[-1]

    ## Check if state is except or not.
    #  @retval  true if the state is the except state.
    #  @retval  false otherwise.
    def isExceptState(self):
        return self._state == FSM.States._Except

    ## Check if state is error or not.
    #  @retval  true if the state is the error state.
    #  @retval  false otherwise.
    def isErrorState(self):
        return self._state == FSM.States._Error

    ## Check if the FSM is stopped or not.
    #  @retval  true if the FSM is stopped.
    #  @retval  false otherwise.
    def isStopped(self):
        return self.isEndState() or self.isExceptState() or self.isErrorState()
有限狀態機裡的巢狀類別States可以定義所有的狀態。繼承這個有限狀態機的類別需要覆寫StateProcess方法,該方法就是回傳狀態機的所需狀態清單。如此一來,使用上僅需要呼叫nextState方法,就能轉換狀態。

Python - 抽象類別

Python的library中,有東西能協助建立抽象類別與抽象方法。
from abc import ABCMeta, abstractmethod  # abstract class

class base(metaclass = ABCMeta):
   
    @abstractmethod
    def foo(self):
        pass
如此一來,繼承base類別就一定要覆寫foo方法。

Python - 不確定參數與屬性

Python傳遞不確定的參數值:
def foo1(opt, *args, **kwargs):
    # expressions
在這裡,opt是確定的參數,不確定的參數有args和kwargs,args的資料型態為tuple, kwargs的資料型態為dict。
foo1(1, True, "hello", data = "world", size = 8)
上面這一行呼叫了foo1,並傳遞五個參數值,進入foo1後,參數分別為:

opt = 1
args = (True, "hello")
kwargs = {"data" : "world", "size" : 8}

在使用繼承類別時,如果有多種形態的繼承類別,利用傳遞不確定的參數值,可以做到彈性的呼叫。



Python也可以傳遞不確定的屬性給物件:
class base():

    def setParameters(self, **params):
        for key, value in params.items():
            setattr(self, key, value)

    def run(self, out, **options):
        data = getattr(self, "data", "")
        length = getattr(self, "len", 0)
利用setattr和getattr這兩個內建函式,做到動態賦予物件屬性。這種寫法也是為了彈性設計。

Python - decorator運用(2)

在撰寫函式時,因為python的特性,不會去檢查回傳值的資料型態是否正確,也不會去檢查是否遺漏回傳值,這對於撰寫複雜一點的函式容易有疏忽的地方,這時候就能靠decorator做一些協助,例如遺漏回傳值時補上預設回傳值,或者回傳值型態錯誤時丟出例外錯誤。
import functools

## Check return type and value.
#  @details The return value could be one following situation:
#            1. value type is specified in the \a cls;
#            2. None;
#            3. value type is not specified and not None.
#   
#            In the 1st situation, return original value.
#            In the 2nd situation, if allow_none is true, return None; otherwise, return \a default.
#            In the 3rd situation, raise SyntaxError.
#  @param   default is the default return value.
#  @param   cls is the specified return value type.
#  @param   allow_none - if true, the return value could be None; otherwise, the return value must be assigned.
#  @exception   SyntaxError

def checkReturn(default, allow_type = None, allow_none = False):
   
    def check_decorator(function):
       
        def decorator_wrapper(*args, **kwargs):
            result = function(*args, **kwargs)
           
            cls = allow_type
            try:
                if allow_type is None:
                    cls = (type(default),)
                elif not issubclass(allow_type, tuple):
                    cls = (allow_type,)
            except Exception as e:
                raise
           
            if type(result) in cls:
                pass
            elif result is None:
                if allow_none:
                    pass
                else:
                    result = default
            else:
                raise SyntaxError("The type of return value {0} is not in {1}.".format(result, cls))
            return result
       
        return decorator_wrapper
   
    return check_decorator

Python - decorator運用(1)

在trace程式碼的時候,尤其是多執行緒的程式碼,想知道函式的呼叫情況,此時會記錄函式的進出資訊。在python,可以使用decorator幫助記錄。
import functools
import time

def logFunc(logger = None):
   
    def log_decorator(function):
       
        @functools.wraps(function)
        def decorator_wrapper(*args, **kwargs):
            if logger is None:
                print("{2} [Debug] Enter {0}.{1}".format(function.__module__, function.__name__, time.asctime()))
                result = function(*args, **kwargs)
                print("{2} [Debug] Exit {0}.{1}".format(function.__module__, function.__name__, time.asctime()))
            else:
                logger.debug("Enter {0}.{1}".format(function.__module__, function.__name__))
                result = function(*args, **kwargs)
                logger.debug("Exit {0}.{1}".format(function.__module__, function.__name__))
            return result
       
        return decorator_wrapper
   
    return log_decorator

另外,有時需要記錄函式的執行時間,一樣可以使用decorator幫助記錄。
import functools
import time

def logTime(logger = None):
   
    def time_decorator(function):
       
        def decorator_wrapper(*args, **kwargs):
            start = time.time()
            result = function(*args, **kwargs)
            end = time.time()
            if logger is None:
                print("{3} [Debug] {0}.{1} spent {2:.3f} seconds.".format(function.__module__, function.__name__, end - start, time.asctime()))
            else:
                logger.debug("{0}.{1} spent {2:.3f} seconds.".format(function.__module__, function.__name__, end - start))
            return result
       
        return decorator_wrapper
   
    return time_decorator

在這裡的logger是指logging.logger。

decorator的使用方式為:
@logFunc(logger)
def foo1():
  # expressions

@logTime(logger)
def foo2():
  # expressions