大家好?
本次我们将实现上次提到的功能,即从 URL 获取 feature 图像。
以下是来自大田 Membership 的南斗贤提供的有关正则表达式的资料。
他在本次功能实现中给予了很大帮助。
阅读更多
Python 作业 5
作者 : 2009135046, 都贤南(音译)
• [第1题] 请说明类(Class)和模块(Module)的共同点与区别。
类和模块的共同点是它们都用于收集并存储相关功能或常量值,且都拥有独立的名字空间(Namespace)。这样做的目的是为了提高代码的可重用性和可维护性。区别在于,模块以文件为单位定义名字空间,而类则在类空间内构建名字空间。
• [第2题] 请说明多态性(Polymorphism),并给出一段展现多态性的 Python 代码示例。
# -*- 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
()
[解答] 指在继承关系内,不同类的实例对同一个成员函数调用作出不同反应的功能。运算符重载也是支持多态的重要技术。
[示例] 这是使用了设计模式中策略模式(Strategy Pattern)的示例。对于继承了 FlyBehavior 和 QuackBeHavior 的各个行为,在运行时每个对象都支持不同的响应。在上述示例中,相比继承的 quack 或 squack,与之处于组合关系的 duck 对象更为重要。根据 duck 对象初始化时所持有的对象不同,可以采取不同的行动。
• [第3题] 请编写满足以下所有要求的 Counter 类(不需要针对每个要求输入答案,只需提交一个满足第3题所有要求的类定义代码即可)。
# -*- 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
[解答]
- 需求 1:在 __init__ 函数中将参数默认初始化为 1 来实现。
- 需求 2:使用 __str__ 函数可以达到与 Java 的 toString 相同的效果。
- 需求 3:按照要求增加了数值。
- 需求 4:使用 __call__ 函数,通过实例调用 incr 函数。
- 需求 5:使用 __add__ 和 __sub__ 函数开发。为了考虑到可扩展性,引入了 operatorHelper() 方法来应用模板方法模式(Template Method Pattern),使得运算符可以动态确定。开发时确保了运算不仅支持整数,也支持其他类型。
- 需求 6:使用了 cmp 函数。同样为了能够兼容不同类型进行了开发。使用 cmp 函数可以无需复杂的算法直接进行处理。
• [第4题] 以下是继承内置数据类型 list 后创建的 MySet 类定义。请尝试以教导他人的方式,解释上述类定义中 __init__(), __str__(), eliminate_duplicate() 这三个方法的代码内容。
• [第5题] 请为第4题定义的 MySet 类添加方法,以满足以下所有要求(不需要针对每个要求输入答案,只需提交一个满足第5题所有要求的 MySet 类定义代码即可)。
• [第6题] 对第5题定义的 MySet 类执行以下示例,可以确认其能够正确运行且无错误。请说明为什么示例中 len()、bool() 内置函数和 in 关键字的使用在没有进行特殊方法定义的情况下也能正确执行。 - 解说已将第4、5、6题合并整理。
# -*- 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 作者..."
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
作者 : 2009135046, Doohyun Nam
• [问题 1] 请说明类(Class)和模块(Module)的共同点和区别。
类和模块的共同点是它们都用于收集并存储功能相似或相关的函数和常量值,并且拥有独立的名字空间(namespace)。其目的主要在于可重用性和维护性。区别在于,模块以文件为单位定义名字空间,而类在类空间中构建名字空间。
• [问题 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
()
[解答] 同一继承体系下,不同类的实例对相同的成员函数调用做出不同响应的功能;运算符重载也是支持多态的重要技术。
[示例] 这是使用设计模式中策略模式(Strategy Pattern)的示例。对于继承了 FlyBehavior 和 QuackBeHavior 的各个行为,对象在运行时支持不同的响应。在以下示例中,相比于被继承的 quack 或 squack,与之处于组合关系的 duck 对象更为重要。Duck 对象初始化时,根据其持有的对象不同,可以采取不同的行为。
• [问题 3] 请编写一个满足以下所有要求的 Counter 类(无需按要求分别输入,只需提交 3 题的 1 个类定义代码即可)。
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
[解答]
-Needs1: 在 `__init__` 函数中将参数默认值设为 1 进行了满足。
-Needs2: 使用 `__str__` 函数可以达到类似 Java 中 `toString` 的效果。
-Needs3: 按照要求增加了数值。
-Needs4: 使用 `__call__` 函数,通过实例调用了 `incr` 函数。
-Needs5: 使用 `__sum__` 和 `__sub__` 函数开发。考虑到扩展性,设置了 `operatorHelper()` 方法以应用模板方法模式(Template Method Pattern),动态决定运算符。运算开发为不仅支持整数,还支持其他类型。
-Needs6: 使用了 `cmp` 函数。同样制作了可以响应不同类型的功能。使用 `cmp` 函数即可无需复杂算法。
• [问题 4] 以下是继承内置数据类型 list 所创建的 MySet 类定义内容。请以教别人的心态,解释类定义中 `__init__()`, `__str__()`, `eliminate_duplicate()` 这三个方法代码的内容。
• [问题 5] 请提交满足问题 4 中定义的 MySet 类添加方法后满足以下各要求的代码(无需按要求分别输入,只需提交 5 题的 1 个 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