Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
LOFAR
Manage
Activity
Members
Labels
Plan
Issues
Wiki
Jira issues
Open Jira
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Deploy
Releases
Package registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Code review analytics
Insights
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
RadioObservatory
LOFAR
Commits
b1efc5c4
Commit
b1efc5c4
authored
3 years ago
by
Jorrit Schaap
Browse files
Options
Downloads
Patches
Plain Diff
TMSS-745
: fixed dict_with_overrides for nested lists in the dict(s)
parent
ab601b1f
No related branches found
Branches containing commit
No related tags found
Tags containing commit
3 merge requests
!634
WIP: COBALT commissioning delta
,
!504
TMSS-745: Responsive Telescope
,
!481
Draft: SW-971 SW-973 SW-975: Various fixes to build LOFAR correctly.
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
LCS/PyCommon/test/t_util.py
+29
-1
29 additions, 1 deletion
LCS/PyCommon/test/t_util.py
LCS/PyCommon/util.py
+19
-5
19 additions, 5 deletions
LCS/PyCommon/util.py
with
48 additions
and
6 deletions
LCS/PyCommon/test/t_util.py
+
29
−
1
View file @
b1efc5c4
...
...
@@ -14,9 +14,10 @@ def setUpModule():
def
tearDownModule
():
pass
@unit_test
class
TestUtils
(
unittest
.
TestCase
):
@unit_test
def
test_is_iterable
(
self
):
#list
self
.
assertTrue
(
is_iterable
([]))
...
...
@@ -36,6 +37,33 @@ class TestUtils(unittest.TestCase):
self
.
assertFalse
(
is_iterable
(
1
))
self
.
assertFalse
(
is_iterable
(
None
))
def
test_merge_nested_dicts
(
self
):
dict_a
=
{
'
a
'
:
1
,
'
b
'
:
{
'
c
'
:
3
,
'
d
'
:
[
4
,
5
,
6
]},
'
e
'
:
[{
'
foo
'
:
1
},
{
'
bar
'
:
2
}]}
dict_b
=
{
'
a
'
:
2
}
merged
=
dict_with_overrides
(
dict_a
,
dict_b
)
self
.
assertEqual
({
'
a
'
:
2
,
'
b
'
:
{
'
c
'
:
3
,
'
d
'
:
[
4
,
5
,
6
]},
'
e
'
:
[{
'
foo
'
:
1
},
{
'
bar
'
:
2
}]},
merged
)
dict_b
=
{
'
a
'
:
1
,
'
b
'
:
2
}
merged
=
dict_with_overrides
(
dict_a
,
dict_b
)
self
.
assertEqual
({
'
a
'
:
1
,
'
b
'
:
2
,
'
e
'
:
[{
'
foo
'
:
1
},
{
'
bar
'
:
2
}]},
merged
)
dict_b
=
{
'
b
'
:
{
'
c
'
:
4
}}
merged
=
dict_with_overrides
(
dict_a
,
dict_b
)
self
.
assertEqual
({
'
a
'
:
1
,
'
b
'
:
{
'
c
'
:
4
,
'
d
'
:
[
4
,
5
,
6
]},
'
e
'
:
[{
'
foo
'
:
1
},
{
'
bar
'
:
2
}]},
merged
)
dict_b
=
{
'
e
'
:
[{
'
foo
'
:
2
},
{
'
bar
'
:
3
}]}
merged
=
dict_with_overrides
(
dict_a
,
dict_b
)
self
.
assertEqual
({
'
a
'
:
1
,
'
b
'
:
{
'
c
'
:
3
,
'
d
'
:
[
4
,
5
,
6
]},
'
e
'
:
[{
'
foo
'
:
2
},
{
'
bar
'
:
3
}]},
merged
)
with
self
.
assertRaises
(
AssertionError
):
dict_b
=
{
'
e
'
:
[]}
#AssertionError should be raised cause list is not of same length as original
dict_with_overrides
(
dict_a
,
dict_b
)
def
main
(
argv
):
unittest
.
main
()
...
...
This diff is collapsed.
Click to expand it.
LCS/PyCommon/util.py
+
19
−
5
View file @
b1efc5c4
...
...
@@ -28,7 +28,8 @@ import sys
import
os
,
os
.
path
import
time
from
copy
import
deepcopy
import
logging
logger
=
logging
.
getLogger
(
__name__
)
def
check_bit
(
value
,
bit
):
"""
...
...
@@ -235,13 +236,26 @@ def dict_search_and_replace(d: dict, key, value):
dict_search_and_replace
(
i
,
key
,
value
)
def
dict_with_overrides
(
org_dict
:
dict
,
overrides
_dict
:
dict
)
->
dict
:
def
dict_with_overrides
(
org_dict
:
dict
,
overrides
:
dict
)
->
dict
:
'''
return a copy of the original (nested) org_dict with all (nested) key/value pairs in the overrides_dict applied
'''
new_dict
=
deepcopy
(
org_dict
)
for
override_key
,
override_value
in
overrides_dict
.
items
():
# recurse
for
override_key
,
override_value
in
overrides
.
items
():
if
isinstance
(
override_value
,
dict
):
new_dict
[
override_key
]
=
dict_with_overrides
(
org_dict
.
get
(
override_key
,{}),
override_value
)
sub_dict
=
new_dict
.
get
(
override_key
,
{})
new_dict
[
override_key
]
=
dict_with_overrides
(
sub_dict
,
override_value
)
elif
isinstance
(
override_value
,
list
):
sub_list
=
new_dict
.
get
(
override_key
,
[])
assert
isinstance
(
sub_list
,
list
)
assert
len
(
sub_list
)
==
len
(
override_value
)
for
i
in
range
(
len
(
override_value
)):
org_list_item
=
sub_list
[
i
]
override_list_item
=
override_value
[
i
]
if
isinstance
(
org_list_item
,
dict
)
and
isinstance
(
override_list_item
,
dict
):
new_dict
[
override_key
][
i
]
=
dict_with_overrides
(
org_list_item
,
override_list_item
)
else
:
new_dict
[
override_key
][
i
]
=
override_list_item
else
:
new_dict
[
override_key
]
=
override_value
...
...
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