[短期] 04. Feature IMAGE 擷取

你好?

這次我們要來實作上次預定要做的...從 URL 擷取 feature 圖片的功能。

下方是大田 Membership 南斗賢提供的正規表示式相關資料。

這次的功能實作得到了許多幫助。

顯示更多


Python Assignment 5

Author : 2009135046, Doohyun Nam

• [第 1 題] 請解釋類別 (Class) 與模組 (Module) 的共同點與差異。

類別與模組的共同點在於,它們都是將功能相似或相關的函式與常數值收集起來儲存,並擁有獨立的命名空間。這麼做是為了專注於程式碼的再利用性與維護性。若要說差異,模組是以檔案為單位定義命名空間,而類別則是在類別空間中構成命名空間。

• [第 2 題] 請解釋多型 (Polymorphism),並提出一個你自己撰寫的 Python 程式碼範例來展示多型。

In [23]:

# -*- coding: utf-8 -*-


class


FlyBehavior


:


def


fly


(


self


):


pass


class


FlyWithWings


(


FlyBehavior


):


def


fly


(


self


):


print


"飛行中"


class


FlyNoWay


(


FlyBehavior


):


def


fly


(


self


):


print


"無法飛行"


class


QuackBeHavior


:


def


quack


(


self


):


pass


class


Quack


(


QuackBeHavior


):


def


quack


(


self


):


print


"呱呱"


class


Squack


(


QuackBeHavior


):


def


quack


(


self


):


print


"唧唧"


class


MuteQuack


(


QuackBeHavior


):


def


quack


(


self


):


print


""


class


Duck


:


def


__init__


(


self


,


quack


,


fly


):


self


.


quackBeHavior


=


quack


self


.


flyBehavior


=


fly


def


quack


(


self


):


self


.


quackBeHavior


.


quack


()


def


fly


(


self


):


self


.


flyBehavior


.


fly


()


mallardDuck


=


Duck


(


Quack


(),


FlyWithWings


())


rubberDuck


=


Duck


(


Squack


(),


FlyNoWay


())


mallardDuck


.


fly


()


rubberDuck


.


fly


()


mallardDuck


.


quack


()


rubberDuck


.


quack


()

飛行中
無法飛行
呱呱
唧唧

[解答] 指的是在繼承關係中,不同類別的實例對於相同的成員函式呼叫能做出不同的反應。運算子多載 (Operator Overloading) 也是支援多型的重要技術。
[範例] 這是使用設計模式中策略模式 (Strategy Pattern) 的範例。對於繼承自 FlyBehavior 與 QuackBeHavior 的各個行為,在執行時期各個物件都能支援不同的反應。在下方的範例中,比起繼承來的 quack 或 squack,由它們組成的 duck 物件更為重要。當 duck 物件初始化時,可以根據它持有的物件而採取不同的行動。

• [第 3 題] 請撰寫一個 Counter 類別,滿足以下所有需求。(不需要針對每個需求分別輸入答案,只需針對第 3 題提供一個定義類別的程式碼即可。)

In [21]:

# -*- coding: utf-8 -*-


class


Counter


(


object


):


def


__init__


(


self


,


value


,


step


=


1


):


self


.


step


=


step


self


.


value


=


value


def


incr


(


self


):


self


.


value


+=


self


.


step


def


__str__


(


self


):


return


str


(


self


.


value


)


def


__call__


(


self


):


self


.


incr


()


def


__add__


(


self


,


other


):


return


self


.


operatorHelper


(


other


,


"+"


)


def


__sub__


(


self


,


other


):


return


self


.


operatorHelper


(


other


,


"-"


)


def


operatorHelper


(


self


,


other


,


code


):


try


:


if


type


(


self


)


==


type


(


other


):


exec


(


compile


(


"self.value


%s


= other.value"


%


code


,


'<string>'


,


'single'


))


exec


(


compile


(


"self.step


%s


= other.step"


%


code


,


'<string>'


,


'single'


))


else


:


exec


(


compile


(


"self.value


%s


= int(other)"


%


code


,


'<string>'


,


'single'


))


except


:


pass


return


self


def


__cmp__


(


self


,


other


):


value


=


other


;


try


:


if


type


(


self


)


==


type


(


other


):


value


=


other


.


value


else


:


value


=


int


(


other


)


except


:


pass


return


cmp


(


self


.


value


,


value


)


#需求 1


c


=


Counter


(


10


)


d


=


Counter


(


10


,


2


)


#需求 2


print


"需求 2 輸出"


print


c


,


d


#需求 3


c


.


incr


()


d


.


incr


()


print


"需求 3 輸出"


print


c


,


d


#需求 4


c


()


d


()


print


"需求 4 輸出"


print


c


,


d


#需求 5


c


=


c


+


5


d


=


d


-


5


print


"需求 5 輸出"


print


c


,


d


#需求 6


print


"需求 6 輸出"


print


c


>


10


print


d


>


10


print


c


<


10


print


d


<


10


print


c


==


"17"


print


d


!=


"9"


print


c


>


d

需求 2 輸出
10 10
需求 3 輸出
11 12
需求 4 輸出
12 14
需求 5 輸出
17 9
需求 6 輸出
True
False
False
True
True
False
True

[解答]
-Needs1 : 在 **init** 函式中將參數預設初始化為 1 來達成。
-Needs2 : 使用 **str** 函式可以達到與 Java 的 toString 相同的效果。
-Needs3 : 依照需求增加了數值。
-Needs4 : 使用 **call** 函式,以實例形式呼叫了 incr 函式。
-Needs5 : 使用 **sum**, **sub** 函式來開發。考慮到擴充性,設定了一個名為 operatorHelper() 的方法,以便應用模板方法模式,讓運算子動態決定。開發時不僅支援整數,還支援其他類型的運算。
-Needs6 : 使用了 **cmp** 函式。同樣地,為了應對其他類型,這樣設計了。使用 cmp 函式可以無需額外的演算法即可進行。

• [第 4 題] 下面是將內建資料型態 list 進行子類別化 (Subclassing) 所製成的 MySet 類別定義。請嘗試以教導他人的方式,解釋該類別定義中 **init**(), **str()**(), eliminate_duplicate() 這三個方法的程式碼內容。

• [第 5 題] 請為第 4 題定義的 MySet 類別新增方法,並提供滿足以下所有需求的程式碼(不需要針對每個需求分別輸入答案,只需針對第 5 題提供一個 MySet 類別定義程式碼即可。)

• [第 6 題] 針對第 5 題定義的 MySet 類別,若執行下方的範例,可以確認其能正確運作且無錯誤。請解釋為何範例中的 len(), bool() 內建函式以及 in 關鍵字在使用時,即便沒有定義額外的方法也能正確執行。 - 解析的部分將 4、5、6 題寫在一起。

In [1]:

# -*- coding: utf-8 -*-


import


copy


class


MySet


(


list


):


def


__init__


(


self


,


l


):


for


e


in


l


:


self


.


append


(


e


)


MySet


.


eliminate_duplicate


(


self


)


def


__str__


(


self


):


result


=


"MySet: {"


for


e


in


self


:


result


=


result


+


str


(


e


)


+


" ,"


result


=


result


[


0


:


len


(


result


)


-


2


]


+


"}"


return


result


def


__or__


(


self


,


other


):


return


MySet


(


self


+


other


)


def


__and__


(


self


,


other


):


s


=


[]


for


e


in


other


:


if


e


in


self


:


s


.


append


(


e


)


return


MySet


(


s


)


def


__sub__


(


self


,


other


):


s


=


copy


.


deepcopy


(


self


)


for


e


in


self


:


if


e


in


other


:


s


.


remove


(


e


)


return


MySet


(


s


)


@staticmethod


def


eliminate_duplicate


(


l


):


s


=


[]


for


e


in


l


:


if


e


not


in


s


:


s


.


append


(


e


)


l


[:]


=


[]


for


e


in


s


:


l


.


append


(


e


)


#需求 1


print


"需求 1 輸出"


s


=


MySet


([


1


,


2


,


2


,


3


])


t


=


MySet


([


2


,


3


,


4


,


5


,


6


,


7


,


8


,


8


,


8


,


8


,


8

<span---
layout: post
title: "[短期] 04. Feature IMAGE 擷取"
description: "大家好?這次要來實作上次預定要做的功能.. 從 URL 取得 feature 圖片。以下是大田 Membership 南斗賢(Doohyun Nam)提供的正規表示式相關資料。這次功能實作中得到了很多幫助。展開 Python Assignment 5 Author..."
date: 2015-02-06 02:09:08 +0900
section: blog
category: projects
lang: ko
ref: 2015-02-06-legacy-11-projects-04-feature-image
tags:
  - "TJSSM"
  - "projects"
---

大家好?

這次要來實作上次預定要做的功能.. 從 URL 取得 feature 圖片。

以下是大田 Membership 南斗賢(Doohyun Nam)提供的正規表示式相關資料。

這次功能實作中得到了很多幫助。

展開


Python Assignment 5

Author : 2009135046, Doohyun Nam

• [第 1 題] 請說明類別(class)與模組(module)的共同點與差異。

類別與模組的共同點在於,它們都是將執行相似或相關工作的函式或常數值收集並儲存起來,並擁有獨立的命名空間。這樣做的原因是為了專注於重複使用性與維護性。差異在於,模組是以檔案為單位定義命名空間,而類別則是在類別空間中構成命名空間。

• [第 2 題] 請說明多型(polymorphism),並提供您自己編寫的 Python 程式碼範例來展示多型。

In [23]:

# -*- coding: utf-8 -*-


class


FlyBehavior


:


def


fly


(


self


):


pass


class


FlyWithWings


(


FlyBehavior


):


def


fly


(


self


):


print


"飛行"


class


FlyNoWay


(


FlyBehavior


):


def


fly


(


self


):


print


"無法飛行"


class


QuackBeHavior


:


def


quack


(


self


):


pass


class


Quack


(


QuackBeHavior


):


def


quack


(


self


):


print


"呱呱"


class


Squack


(


QuackBeHavior


):


def


quack


(


self


):


print


"嗶嗶"


class


MuteQuack


(


QuackBeHavior


):


def


quack


(


self


):


print


""


class


Duck


:


def


__init__


(


self


,


quack


,


fly


):


self


.


quackBeHavior


=


quack


self


.


flyBehavior


=


fly


def


quack


(


self


):


self


.


quackBeHavior


.


quack


()


def


fly


(


self


):


self


.


flyBehavior


.


fly


()


mallardDuck


=


Duck


(


Quack


(),


FlyWithWings


())


rubberDuck


=


Duck


(


Squack


(),


FlyNoWay


())


mallardDuck


.


fly


()


rubberDuck


.


fly


()


mallardDuck


.


quack


()


rubberDuck


.


quack


()

飛行
無法飛行
呱呱
嗶嗶

[解析] 這是一種功能,讓繼承關係內不同類別的實例,對於相同的成員函式呼叫做出不同的反應;運算子多載(operator overloading)也是支援多型的關鍵技術。
[範例] 這是使用設計模式中策略模式(Strategy Pattern)的範例。針對繼承自 FlyBehavior、QuackBeHavior 的各個行為,在執行時期各個物件支援不同的反應。此範例中,比起繼承來的 quack 或 squack,組成這些關係的 duck 物件更為重要。根據 duck 物件初始化時所持有的物件,可以採取不同的行為。

• [第 3 題] 請編寫一個滿足以下所有要求的 Counter 類別(不需要針對每個要求分別輸入答案,只需針對第 3 題提供一個類別定義程式碼即可)。

In [21]:

# -*- coding: utf-8 -*-


class


Counter


(


object


):


def


__init__


(


self


,


value


,


step


=


1


):


self


.


step


=


step


self


.


value


=


value


def


incr


(


self


):


self


.


value


+=


self


.


step


def


__str__


(


self


):


return


str


(


self


.


value


)


def


__call__


(


self


):


self


.


incr


()


def


__add__


(


self


,


other


):


return


self


.


operatorHelper


(


other


,


"+"


)


def


__sub__


(


self


,


other


):


return


self


.


operatorHelper


(


other


,


"-"


)


def


operatorHelper


(


self


,


other


,


code


):


try


:


if


type


(


self


)


==


type


(


other


):


exec


(


compile


(


"self.value


%s


= other.value"


%


code


,


'<string>'


,


'single'


))


exec


(


compile


(


"self.step


%s


= other.step"


%


code


,


'<string>'


,


'single'


))


else


:


exec


(


compile


(


"self.value


%s


= int(other)"


%


code


,


'<string>'


,


'single'


))


except


:


pass


return


self


def


__cmp__


(


self


,


other


):


value


=


other


;


try


:


if


type


(


self


)


==


type


(


other


):


value


=


other


.


value


else


:


value


=


int


(


other


)


except


:


pass


return


cmp


(


self


.


value


,


value


)


#需求 1


c


=


Counter


(


10


)


d


=


Counter


(


10


,


2


)


#需求 2


print


"需求 2 輸出"


print


c


,


d


#需求 3


c


.


incr


()


d


.


incr


()


print


"需求 3 輸出"


print


c


,


d


#需求 4


c


()


d


()


print


"需求 4 輸出"


print


c


,


d


#需求 5


c


=


c


+


5


d


=


d


-


5


print


"需求 5 輸出"


print


c


,


d


#需求 6


print


"需求 6 輸出"


print


c


>


10


print


d


>


10


print


c


<


10


print


d


<


10


print


c


==


"17"


print


d


!=


"9"


print


c


>


d

需求 2 輸出
10 10
需求 3 輸出
11 12
需求 4 輸出
12 14
需求 5 輸出
17 9
需求 6 輸出
True
False
False
True
True
False
True

[解析]
-Needs1 : 在 `__init__` 函式中將參數預設初始化為 1 來滿足。
-Needs2 : 使用 `__str__` 函式,即可達到與 Java 的 `toString` 相同的效果。
-Needs3 : 依照需求增加了數值。
-Needs4 : 使用 `__call__` 函式,將 incr 函式以實例呼叫出來。
-Needs5 : 使用 `__add__`、`__sub__` 函式進行開發。為了考慮擴充性,設置了名為 `operatorHelper()` 的方法來應用模板方法模式(Template Method Pattern),讓運算子動態決定。開發時不僅考慮了整數,也考慮了其他類型進來的可能。
-Needs6 : 使用 `__cmp__` 函式。如同上述,製作成能夠應對其他類型。使用 `cmp` 函式的話,無需額外的演算法即可進行。

• [第 4 題] 以下是將內建資料型態 list 進行子類別化(subclassing)所製作的 MySet 類別定義內容。請試著向別人解說該類別定義中的 `__init__()`、`__str__()`、`eliminate_duplicate()` 這三個方法代碼的內容。

• [第 5 題] 請針對第 4 題中定義的 MySet 類別新增方法,並提供滿足以下各項要求的編碼(不需要針對每個要求分別輸入答案,只需針對第 5 題提供一個 MySet 類別定義程式碼即可)。

• [第 6 題] 針對第 5 題定義的 MySet 類別執行以下範例,可以確認其能正確運作而無錯誤。請說明為什麼在範例中使用的內建函式 `len()`、`bool()` 與 `in` 關鍵字,在沒有特別定義方法的情況下也能正確執行。 - 解析已將 4、5、6 題寫在一起。

In [1]:

# -*- coding: utf-8 -*-


import


copy


class


MySet


(


list


):


def


__init__


(


self


,


l


):


for


e


in


l


:


self


.


append


(


e


)


MySet


.


eliminate_duplicate


(


self


)


def


__str__


(


self


):


result


=


"MySet: {"


for


e


in


self


:


result


=


result


+


str


(


e


)


+


" ,"


result


=


result


[


0


:


len


(


result


)


-


2


]


+


"}"


return


result


def


__or__


(


self


,


other


):


return


MySet


(


self


+


other


)


def


__and__


(


self


,


other


):


s


=


[]


for


e


in


other


:


if


e


in


self


:


s


.


append


(


e


)


return


MySet


(


s


)


def


__sub__


(


self


,


other


):


s


=


copy


.


deepcopy


(


self


)


for


e


in


self


:


if


e


in


other


:


s


.


remove


(


e


)


return


MySet


(


s


)


@staticmethod


def


eliminate_duplicate


(


l


):


s


=


[]


for


e


in


l


:


if


e


not


in


s


:


s


.


append


(


e


)


l


[:]


=


[]


for


e


in


s


:


l


.


append


(


e


)


#需求 1


print


"需求 1 輸出"


s


=


MySet


([


1


,


2


,


2


,


3


])


t


=


MySet


([


2


,


3


,


4


,


5


,


6


,


7


,


8


,


8


,


8


,


8


,


8


,

<span class
AD