Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
Q
quality-kpi
Manage
Activity
Members
Labels
Plan
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Quiring, Ole
quality-kpi
Commits
f1ec1b14
Commit
f1ec1b14
authored
2 years ago
by
Hock, Benedikt
Browse files
Options
Downloads
Patches
Plain Diff
Refactor LegoComponent
parent
1700b68a
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
functions/lego_classes.py
+27
-38
27 additions, 38 deletions
functions/lego_classes.py
with
27 additions
and
38 deletions
functions/lego_classes.py
+
27
−
38
View file @
f1ec1b14
...
...
@@ -28,49 +28,38 @@ class LegoItem:
def
__repr__
(
self
):
return
f
"
Lego Item [
{
self
.
id
}
]
"
class
LegoComponent
:
def
__init__
(
self
,
items
=
None
|
LegoItem
|
list
[
LegoItem
],
components
=
None
)
->
None
:
# TODO: type hinting
self
.
id
=
uuid
.
uuid4
()
if
(
isinstance
(
items
,
list
)
and
items
and
isinstance
(
items
[
0
],
LegoItem
)):
self
.
items
=
items
elif
isinstance
(
items
,
LegoItem
):
self
.
items
=
[
items
]
else
:
self
.
items
=
[]
# must be a list for now,
if
components
is
None
:
self
.
components
=
[]
elif
isinstance
(
components
,
list
):
self
.
components
=
components
else
:
self
.
components
=
[
components
]
# Not checking correct type
self
.
parent_id
=
None
# This will be set when added to a component
def
add_item
(
self
,
item
:
LegoItem
|
List
[
LegoItem
])
->
None
:
if
isinstance
(
item
,
list
):
self
.
items
.
extend
(
item
)
else
:
self
.
items
.
append
(
item
)
class
LegoComponent
:
def
__init__
(
self
,
**
kwargs
)
->
None
:
self
.
id
:
uuid
.
UUID
=
uuid
.
uuid4
()
self
.
properties
:
dict
=
kwargs
self
.
items
:
List
[
LegoItem
]
=
[]
self
.
components
:
List
[
LegoComponent
]
=
[]
self
.
parent_id
:
None
|
uuid
.
UUID
=
None
def
add_component
(
self
,
component
:
LegoComponent
|
List
[
LegoComponent
]
)
->
None
:
if
isinstance
(
component
,
list
):
self
.
components
.
extend
(
component
)
else
:
self
.
components
.
append
(
component
)
def
add_item
(
self
,
item
:
LegoItem
)
->
None
:
if
not
isinstance
(
item
,
LegoItem
):
raise
TypeError
(
f
"'
item
'
should be of type LegoPart, got
{
type
(
item
).
__name__
}
instead.
"
)
item
.
parent_id
=
self
.
id
self
.
items
.
append
(
item
)
def
add
(
self
,
element
)
->
None
:
if
isinstance
(
element
,
LegoItem
):
self
.
items
.
append
(
element
)
elif
isinstance
(
element
,
LegoComponent
):
self
.
components
.
append
(
element
)
else
:
raise
TypeError
(
f
"
added elements should be of type LegoItem or
"
f
"
LegoComponent not
{
type
(
element
)
}
"
)
def
add_component
(
self
,
component
:
LegoComponent
)
->
None
:
if
not
isinstance
(
component
,
LegoComponent
):
raise
TypeError
(
f
"'
component
'
should be of type LegoComponent, got
{
type
(
component
).
__name__
}
instead.
"
)
component
.
parent_id
=
self
.
id
self
.
components
.
append
(
component
)
def
children
(
self
)
->
Dict
[
str
,
List
[
LegoItem
]
|
List
[
LegoComponent
]]:
return
{
'
items
'
:
self
.
items
,
'
components
'
:
self
.
components
}
def
get_item_list
(
self
)
->
List
[
LegoItem
]:
item_list
=
[]
item_list
.
extend
(
self
.
items
)
for
component
in
self
.
components
:
item_list
.
extend
(
component
.
get_item_list
())
return
item_list
def
__repr__
(
self
):
return
f
"
Lego Component [
{
self
.
id
}
]
"
# TODO: Adjust default output when printing an item or component
\ No newline at end of file
# TODO: Adjust default output when printing an item or component
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