Hello?
This time, we will implement the feature we decided to work on last time: extracting a feature image from a URL.
Below are materials on regular expressions from Doohyun Nam of the Daejeon Membership.
They were a great help in implementing this feature.
Read more
Python Assignment 5
Author : 2009135046, Doohyun Nam
• [Problem 1] Explain the similarities and differences between classes and modules.
The commonality between classes and modules is that they both aggregate and store functions or constant values that perform similar or related tasks, and they have separate namespaces. The reason for this is to focus on reusability and maintainability. The difference is that modules define namespaces at the file level, while classes construct namespaces within the class scope.
• [Problem 2] Explain polymorphism and provide your own Python code example that demonstrates it.
# -*- coding: utf-8 -*-
class
FlyBehavior
:
def
fly
(
self
):
pass
class
FlyWithWings
(
FlyBehavior
):
def
fly
(
self
):
print
"Flying"
class
FlyNoWay
(
FlyBehavior
):
def
fly
(
self
):
print
"Cannot fly"
class
QuackBeHavior
:
def
quack
(
self
):
pass
class
Quack
(
QuackBeHavior
):
def
quack
(
self
):
print
"Quack"
class
Squack
(
QuackBeHavior
):
def
quack
(
self
):
print
"Squeak"
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
()
[Solution] The ability for instances of different classes within an inheritance relationship to react differently to the same member function call; operator overloading is also an important technique supporting polymorphism.
[Example] This is an example using the Strategy Pattern among design patterns. For each behavior inheriting from FlyBehavior or QuackBeHavior, the objects support different reactions at runtime. In this example, the Duck object containing them is more important than the inherited quack or squack. Depending on what object the Duck object holds when initialized, it can exhibit different behaviors.
• [Problem 3] Code a Counter class that satisfies all the following requirements (You don't need to enter answers for each requirement; just provide one class definition code for Problem 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
)
#Requirement 1
c
=
Counter
(
10
)
d
=
Counter
(
10
,
2
)
#Requirement 2
print
"Requirement 2 output"
print
c
,
d
#Requirement 3
c
.
incr
()
d
.
incr
()
print
"Requirement 3 output"
print
c
,
d
#Requirement 4
c
()
d
()
print
"Requirement 4 output"
print
c
,
d
#Requirement 5
c
=
c
+
5
d
=
d
-
5
print
"Requirement 5 output"
print
c
,
d
#Requirement 6
print
"Requirement 6 output"
print
c
>
10
print
d
>
10
print
c
<
10
print
d
<
10
print
c
==
"17"
print
d
!=
"9"
print
c
>
d
[Solution]
-Needs1 : Initialized the parameter as default to 1 in the __init__ function.
-Needs2 : Using the __str__ function produces the same effect as java's toString.
-Needs3 : Increased the value according to the requirements.
-Needs4 : Used the __call__ function to call the incr function as an instance.
-Needs5 : Developed using __sum__ and __sub__ functions. Thinking of extensibility, I included a method called operatorHelper() to apply the Template Method pattern, allowing operators to be determined dynamically. I developed it so that types other than integers can also be input.
-Needs6 : Used the __cmp__ function. Fabricated it to handle other types similarly to the above. Using the cmp function allows it to proceed without a special algorithm.
• [Problem 4] The following is the definition content of the MySet class created by subclassing the built-in list data type. Explain the code content of the three methods: __init__(), __str__(), and eliminate_duplicate() in the following class definition as if you were teaching it to someone else.
• [Problem 5] Add methods to the MySet class defined in Problem 4 to provide coding that satisfies all the following requirements (You don't need to enter answers for each requirement; just provide one MySet class definition code for Problem 5).
• [Problem 6] When the following example is executed for the MySet class defined in Problem 5, it can be confirmed that it operates correctly without errors. Explain why the use examples of len(), bool() built-in functions and the in keyword within the following example are performed correctly even though no special method definitions were made. - The explanation was written for problems 4, 5, and 6 together.
# -*- 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
)
#Requirement 1
print
"Requirement 1 output"
s
=
MySet
([
1
,
2
,
2
,
3
])
t
=
MySet
([
2
,
3
,
4
,
5
,
6
,
7
,
8
,
8
,
8
,
8
,
8
,---
layout: post
title: "[Short-term] 04. Feature IMAGE Extraction"
description: "Hello? This time, we will implement the feature of extracting a feature image from a URL, which we decided to do last time. Below is the material on regular expressions by Doohyun Nam from the Daejeon Membership. He helped a lot with this feature implementation. See more 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"
---
Hello?
In this session, we will implement the feature of extracting a feature image from a URL, which we planned to do last time.
Below is the material regarding regular expressions provided by Doohyun Nam from the Daejeon Membership.
He provided a great deal of help in implementing this feature.
See more
Python Assignment 5
Author : 2009135046, Doohyun Nam
• [Question 1] Explain the similarities and differences between classes and modules.
The commonality between classes and modules is that they both collect and store functions or constant values that perform similar or related tasks, and they each have their own separate namespace. The reason for this focus is on reusability and maintainability. The difference is that a module defines a namespace at the file level, while a class constructs a namespace within its own class space.
• [Question 2] Explain polymorphism and provide your own Python code example that demonstrates polymorphism.
In [23]:
# -*- coding: utf-8 -*-
class
FlyBehavior
:
def
fly
(
self
):
pass
class
FlyWithWings
(
FlyBehavior
):
def
fly
(
self
):
print
"Flying"
class
FlyNoWay
(
FlyBehavior
):
def
fly
(
self
):
print
"Cannot fly"
class
QuackBeHavior
:
def
quack
(
self
):
pass
class
Quack
(
QuackBeHavior
):
def
quack
(
self
):
print
"Quack"
class
Squack
(
QuackBeHavior
):
def
quack
(
self
):
print
"Squeak"
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
()
[Solution] The ability that allows instances of different classes within an inheritance relationship to respond differently to the same member function call; operator overloading is also an important technique that supports polymorphism.
[Example] This is an example using the Strategy Pattern among design patterns. For each behavior inheriting from FlyBehavior and QuackBeHavior, each object supports a different response at runtime. In the following example, the Duck object, which contains these as part of its composition, is more important than the inherited quack or squack methods. Depending on which object it holds when initialized, the Duck object can exhibit different behaviors.
• [Question 3] Code a Counter class that satisfies all of the following requirements (You don't need to enter the answer for each requirement separately; you can provide one class definition code for Question 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
)
#Requirement 1
c
=
Counter
(
10
)
d
=
Counter
(
10
,
2
)
#Requirement 2
print
"Requirement 2 output"
print
c
,
d
#Requirement 3
c
.
incr
()
d
.
incr
()
print
"Requirement 3 output"
print
c
,
d
#Requirement 4
c
()
d
()
print
"Requirement 4 output"
print
c
,
d
#Requirement 5
c
=
c
+
5
d
=
d
-
5
print
"Requirement 5 output"
print
c
,
d
#Requirement 6
print
"Requirement 6 output"
print
c
>
10
print
d
>
10
print
c
<
10
print
d
<
10
print
c
==
"17"
print
d
!=
"9"
print
c
>
d
[Solution]
-Needs1: Initialized the parameter as 1 by default in the __init__ function.
-Needs2: Using the __str__ function produces the same effect as toString in Java.
-Needs3: Increased the value as per the requirement.
-Needs4: Used the __call__ function to invoke the incr function as an instance.
-Needs5: Developed using the __add__ and __sub__ functions. Considering scalability, I included a method called operatorHelper() to apply the Template Method Pattern, allowing the operator to be determined dynamically. The operations were developed to accept other types as well, not just integers.
-Needs6: Used the cmp function. Similar to above, it was built to handle other types as well. Using the cmp function allows processing without special algorithms.
• [Question 4] The following is the definition of a MySet class created by subclassing the built-in list type. Explain the code content of the three methods __init__(), __str__(), and eliminate_duplicate() in this class definition, thinking as if you are teaching someone else.
• [Question 5] Present coding that satisfies all the requirements by adding methods to the MySet class defined in Question 4 (You don't need to enter the answer for each requirement separately; you can provide one MySet class definition code for Question 5).
• [Question 6] If you perform the following example on the MySet class defined in Question 5, you can confirm that it works correctly without errors. Explain why the len() and bool() built-in functions and the 'in' keyword usage example in the following example perform correctly even though no separate method definitions were made. - The commentary was written for questions 4, 5, and 6 together.
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
)
#Requirement 1
print
"Requirement 1 output"
s
=
MySet
([
1
,
2
,
2
,
3
])
t
=
MySet
([
2
,
3
,
4
,
5
,
6
,
7
,
8
,
8
,
8
,
8
,