Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
P
python
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Florian Burger
python
Commits
940048a6
Commit
940048a6
authored
3 years ago
by
Florian Burger
Browse files
Options
Downloads
Patches
Plain Diff
added solution 4
parent
7966e963
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
solutions/Day-1/solution-4.py
+67
-0
67 additions, 0 deletions
solutions/Day-1/solution-4.py
with
67 additions
and
0 deletions
solutions/Day-1/solution-4.py
0 → 100644
+
67
−
0
View file @
940048a6
import
random
class
ShapeFactory
:
factories
=
{}
@staticmethod
def
add_factory
(
shape_id
,
shape_factory
):
assert
issubclass
(
shape_factory
,
FactoryCreatable
),
"
Not creatable!
"
# we test that this is the right type
ShapeFactory
.
factories
[
shape_id
]
=
shape_factory
.
create
# we know that it has a create
@staticmethod
def
create_shape
(
id
):
shape_id
=
id
.
__name__
# class name as id
if
id
not
in
ShapeFactory
.
factories
.
keys
():
ShapeFactory
.
add_factory
(
shape_id
,
id
)
# we pass in the class again
return
ShapeFactory
.
factories
[
shape_id
]()
# we can simply call it with ()
class
Shape
(
object
):
pass
class
FactoryCreatable
:
@classmethod
def
create
(
cls
):
return
cls
()
class
Circle
(
Shape
,
FactoryCreatable
):
def
draw
(
self
):
print
(
"
Circle.draw
"
)
def
erase
(
self
):
print
(
"
Circle.erase
"
)
class
Square
(
Shape
,
FactoryCreatable
):
def
draw
(
self
):
print
(
"
Square.draw
"
)
def
erase
(
self
):
print
(
"
Square.erase
"
)
class
Triangle
(
Shape
,
FactoryCreatable
):
def
draw
(
self
):
print
(
"
Triangle.draw
"
)
def
erase
(
self
):
print
(
"
Triangle.erase
"
)
# what follows is the client code, we want to randomly create shape type objects
def
shape_name_gen
(
n
):
types
=
Shape
.
__subclasses__
()
for
i
in
range
(
n
):
yield
random
.
choice
(
types
)
# NOTE: We now pass in the Shape sub-class itself
shapes
=
[
ShapeFactory
.
create_shape
(
i
)
for
i
in
shape_name_gen
(
5
)]
for
shape
in
shapes
:
shape
.
draw
()
shape
.
erase
()
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment