Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
N
NTRfC
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
Service Desk
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
TFD - Institut für Turbomaschinen und Fluid-Dynamik
tools
NTRfC
Commits
fa237995
Commit
fa237995
authored
3 years ago
by
Malte Nyhuis
Browse files
Options
Downloads
Patches
Plain Diff
additional dict_utils copied from ntr
parent
8eb87298
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
ntrfc/utils/dictionaries/dict_utils.py
+72
-2
72 additions, 2 deletions
ntrfc/utils/dictionaries/dict_utils.py
tests/test_ntrfc.py
+17
-0
17 additions, 0 deletions
tests/test_ntrfc.py
with
89 additions
and
2 deletions
ntrfc/utils/dictionaries/dict_utils.py
+
72
−
2
View file @
fa237995
from
functools
import
reduce
from
functools
import
reduce
import
operator
import
operator
def
delete_keys_from_dict
(
dictionary
,
key_list
):
"""
delets set of keys from dictionary
"""
dictionary
=
reduce
(
dict
.
get
,
key_list
[
0
:
-
1
],
dictionary
)
del
dictionary
[
key_list
[
-
1
]]
def
nested_dict_pairs_iterator
(
dict_obj
):
def
nested_dict_pairs_iterator
(
dict_obj
):
'''
This function accepts a nested dictionary as argument
'''
and iterate over all values of nested dictionaries
This function accepts a nested dictionary as argument
and iterate over all values of nested dictionaries
'''
'''
# Iterate over all key-value pairs of dict argument
# Iterate over all key-value pairs of dict argument
for
key
,
value
in
dict_obj
.
items
():
for
key
,
value
in
dict_obj
.
items
():
...
@@ -28,3 +35,66 @@ def setInDict(dataDict, mapList, value):
...
@@ -28,3 +35,66 @@ def setInDict(dataDict, mapList, value):
def
getFromDict
(
dataDict
,
mapList
):
def
getFromDict
(
dataDict
,
mapList
):
#todo: test-method
#todo: test-method
return
reduce
(
operator
.
getitem
,
mapList
,
dataDict
)
return
reduce
(
operator
.
getitem
,
mapList
,
dataDict
)
def
merge
(
a
,
b
,
path
=
None
):
"
merges b into a
"
if
path
is
None
:
path
=
[]
for
key
in
b
:
if
key
in
a
:
if
isinstance
(
a
[
key
],
dict
)
and
isinstance
(
b
[
key
],
dict
):
merge
(
a
[
key
],
b
[
key
],
path
+
[
str
(
key
)])
elif
a
[
key
]
==
b
[
key
]:
pass
# same leaf value
else
:
raise
Exception
(
'
Conflict at %s
'
%
'
.
'
.
join
(
path
+
[
str
(
key
)]))
else
:
a
[
key
]
=
b
[
key
]
return
a
def
appendInDictList
(
dataDict
,
mapList
,
value
):
"""
appends value to nested dict with list-values
"""
getFromDict
(
dataDict
,
mapList
[:
-
1
])[
mapList
[
-
1
]].
append
(
value
)
def
nested_val_set
(
dic
,
keys
,
value
):
for
key
in
keys
[:
-
1
]:
dic
=
dic
.
setdefault
(
key
,
{})
dic
[
keys
[
-
1
]]
=
value
def
compare_dictionaries
(
dict_1
,
dict_2
,
path
=
""
):
"""
Compare two dictionaries recursively to find non mathcing elements
https://stackoverflow.com/questions/27265939/comparing-python-dictionaries-and-nested-dictionaries
Args:
dict_1: dictionary 1
dict_2: dictionary 2
Returns:
"""
key_err
=
0
value_err
=
0
old_path
=
path
for
k
in
dict_1
.
keys
():
path
=
old_path
+
"
[%s]
"
%
k
if
not
k
in
dict_2
.
keys
():
key_err
+=
1
else
:
if
isinstance
(
dict_1
[
k
],
dict
)
and
isinstance
(
dict_2
[
k
],
dict
):
ke
,
ve
=
compare_dictionaries
(
dict_1
[
k
],
dict_2
[
k
],
path
)
key_err
+=
ke
value_err
+=
ve
else
:
if
dict_1
[
k
]
!=
dict_2
[
k
]:
value_err
+=
1
for
k
in
dict_2
.
keys
():
path
=
old_path
+
"
[%s]
"
%
k
if
not
k
in
dict_1
.
keys
():
key_err
+=
1
return
key_err
,
value_err
This diff is collapsed.
Click to expand it.
tests/test_ntrfc.py
+
17
−
0
View file @
fa237995
...
@@ -7,6 +7,7 @@ import pyvista as pv
...
@@ -7,6 +7,7 @@ import pyvista as pv
import
numpy
as
np
import
numpy
as
np
from
ntrfc.utils.dictionaries.dict_utils
import
nested_dict_pairs_iterator
from
ntrfc.utils.dictionaries.dict_utils
import
nested_dict_pairs_iterator
from
utils.dictionaries.dict_utils
import
delete_keys_from_dict
,
compare_dictionaries
@pytest.fixture
@pytest.fixture
...
@@ -148,3 +149,19 @@ def test_nested_dict_pairs_iterator():
...
@@ -148,3 +149,19 @@ def test_nested_dict_pairs_iterator():
testdict
=
{
"
0
"
:{
"
0
"
:
0
,
"
1
"
:
1
},
"
1
"
:
1
}
testdict
=
{
"
0
"
:{
"
0
"
:
0
,
"
1
"
:
1
},
"
1
"
:
1
}
check
=
[(
'
0
'
,
'
0
'
,
0
),
(
'
0
'
,
'
1
'
,
1
),
(
'
1
'
,
1
)]
check
=
[(
'
0
'
,
'
0
'
,
0
),
(
'
0
'
,
'
1
'
,
1
),
(
'
1
'
,
1
)]
assert
check
==
list
(
nested_dict_pairs_iterator
(
testdict
)),
"
error
"
assert
check
==
list
(
nested_dict_pairs_iterator
(
testdict
)),
"
error
"
def
test_delete_keys_from_dict
():
dict
=
{
"
a
"
:
0
,
"
b
"
:
1
}
delete_keys_from_dict
(
dict
,
"
a
"
)
assert
"
a
"
not
in
dict
.
keys
()
assert
"
b
"
in
dict
.
keys
()
assert
dict
[
"
b
"
]
==
1
def
test_compare_dictionaries
():
d1
=
{
"
name
"
:{
"
param_1
"
:
0
,
"
param_2
"
:
3
}}
d2
=
{
"
name
"
:{
"
param_1
"
:
4
,
"
param_2
"
:
1
}}
ke
,
ve
=
compare_dictionaries
(
d1
,
d2
)
assert
ke
==
0
assert
ve
==
2
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